CodeIgniter Forums
How to compress an image without resizing it? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to compress an image without resizing it? (/showthread.php?tid=25951)



How to compress an image without resizing it? - El Forum - 01-01-2010

[eluser]fchristant[/eluser]
Hi developers,

I have a site where users can upload images. I process these images directly and resize them into 5 additional formats using the CI Image Manipulation class. I do this quite efficiently as follow:

- I always resize from the previous format, instead of from the original
- I resize using an image quality of 90% which about halves the file size of jpegs

My test case is a 1.6MB JPEG in RGB mode with a high resolution of 3872 x 2592. For that image, which is kind of borderline case, the resize process in total takes about 2 secs, which is acceptable to me.

Now, only one challenge remains. I want the original file to be compressed using that 90% quality but without resizing it. The idea being that that file too will take half the file size. I figured I could simply resize it to its' current dimensions, but that doesn't seem to do anything to the file or its size. Here's my code, somewhat simplified:

Code:
$sourceimage = "test.jpg";
$resize_settings['image_library'] = 'gd2';
$resize_settings['source_image'] = $sourceimage;
$resize_settings['maintain_ratio'] = false;
$resize_settings['quality'] = '90%';
$this->load->library('image_lib', $resize_settings);

$resize_settings['width'] = $imagefile['width'];
$resize_settings['height'] = $imagefile['height'];
$resize_settings['new_image'] = $filename;
$this->image_lib->initialize($resize_settings);
$this->image_lib->resize();

I tried debugging into the CI class to see why nothing happens and I noticed that the script detects that the dimensions did not change. Next, it simply makes a copy of that file without processing it all. I commented that piece of code to force it to resize but now still nothing happens.

Does anybody know how to compress an image (any image, not just jpegs) to 90% using the CI class without changing the dimensions?


How to compress an image without resizing it? - El Forum - 01-01-2010

[eluser]Aken[/eluser]
Compressing the file size / quality isn't a standard function of the image manipulation class. If I wanted to achieve this, I'd do the following:

1) Try to see if using the Crop function would work, by cropping the original photo to its original dimensions, using the 90% quality.

2) If that didn't work, I'd extend the image manipulation class and add a function designed specifically for reducing file size.

I can't test the first step right now, otherwise I'd do that before even writing this post. But I hope it helps somehow.