PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 29, 2022

[FIXED] How to compress an image with codeigniter image_lib?

 July 29, 2022     codeigniter, codeigniter-3, compression, image, php     No comments   

Issue

I am trying to compress size of the image in my codeigniter php website and wrote the following code in my controller:

$this->load->library('image_lib');
$this->load->library('upload');
$image = array();
$ImageCount = count($_FILES['pimage']['name']);
for($i = 0; $i < $ImageCount; $i++){
    $_FILES['file']['name']       = $_FILES['pimage']['name'][$i];
    $_FILES['file']['type']       = $_FILES['pimage']['type'][$i];
    $_FILES['file']['tmp_name']   = $_FILES['pimage']['tmp_name'][$i];
    $_FILES['file']['error']      = $_FILES['pimage']['error'][$i];
    $_FILES['file']['size']       = $_FILES['pimage']['size'][$i];
    
    // File upload configuration
    $uploadPath = './uploads/products/';
    $config['image_library'] = 'gd2';
    
    $config['upload_path'] = $uploadPath;
    $config['allowed_types'] = 'jpg|jpeg|png|gif';
    $config['quality'] = '60%';
    
    // Load and initialize upload library
    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    
    // Upload file to server
    if($this->upload->do_upload('file')){
        // Uploaded file data
        $imageData = $this->upload->data();
        // $uploadImgData[$i]['image_name'] = $imageData['file_name'];
        $uploadImgData[] = $imageData['file_name'];
    }
}

but after upload the image is still saved in the same size, can anyone please tell me what is wrong in here?


Solution

You need to pair the config setting with a library action like resize (R), cropping (C), rotation (X) or watermark (W).

see Processing Methods

and Preferences:

Preference Default Value Options Description Availability
quality 90% 1 - 100% Sets the quality of the image. The higher the quality the larger the file size. R, C, X, W

At the moment you are just setting a bunch of config data and upload the image without a processing method, hence nothing is changed.



Answered By - Vickel
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing