I have a function to convert jpg image to tiff image, keep the original size and quality. I used Imagick class to do that. Convert success but image size has increment too much. My original image was taken from iPhone with 2.6Mb after converting, it increment to 36.6Mb.
I had tried to use some PHP image manipulation libraries like Intervention/image but nothing change.
I also test convert original jpg to new jpg and file size increment from 2.6Mb to 3.3Mb.
My code here:
$image = new \Imagick('image_path_file.jpg);
$image->setImageFormat("tiff");
$image->writeImage("path_new_file.tiff");
Have any options that can help Imagick not increment file size too much?
Thanks you 🙇♂️
I'd like to optimize jpg image size by deleting exif metadata from it. I use Imagick and Imagine libraries to achieve this goal:
$image = $this->imagine->open($currentImagePath);
$iccProfile = $image->palette()->profile();
$image->strip();
$image->profile($iccProfile);
$image->save($optimizedImagePath);
But it doesn't work properly, it actually deletes all metadata from image including icc. Is there a way to delete just exif from jpg image using Imagine and Imagick?
CodeIgniter has a nice image manipulation class. I'm setting an image to be resized to a max of 200x200 pixels, with "master_dim" enabled to keep the aspect ratio the same. However, I don't see a way to get the new size of the image after it's done so that I can store it in the database. Does it not do this? Or do I have to use PHP's getimagesize() to do this?
Thanks.
$this->image_lib->resize() returns TRUE or FALSE.
You will need to use getimagesize on the new image to get its size after the resize.
So I am using php/oracle to create a PDF via the FPDF class/plugin. I am using the Mem_Image class/script to add a blob image to my PDF, but I am having trouble determining the size of the image. I am running PHP 5.2 so getimagesizefromstring isn't working.
For the image it will have a fixed height that I am able to set in the FPDF class, but the width since it could be portrait - or - landscape image, I'll need to scale the image proportionally.
Use imagecreatefromstring and imagesx.
getimagesize() does work with stream wrappers. Unfortunately, php://memory doesn't give you a way to reference it by filename. Either create your own stream wrapper that work smarter or use the VariableStream example.
I was using the firebug page speed utility and one of the suggestions given was to compress the images - So I wrote the following code to compress the image
$filename="http://localhost.com/snapshots/picture.png";
$img = imagecreatefrompng($filename);
$this->_response->setHeader('Content-Type', 'image/png');
imagepng($img,null,9);
imagedestroy($img);
Now the actual image size is 154K
So I experimented by giving different quality levels and here is what I found
imagepng($img,null,0); --> Size = 225K
imagepng($img,null,1); --> Size = 85.9K
imagepng($img,null,2); --> Size = 83.7K
imagepng($img,null,3); --> Size = 80.9K
imagepng($img,null,4); --> Size = 74.6K
imagepng($img,null,5); --> Size = 73.8K
imagepng($img,null,6); --> Size = 73K
imagepng($img,null,7); --> Size = 72.4K
imagepng($img,null,8); --> Size = 71K
imagepng($img,null,9); --> Size = 70.6K
Do these results look accurate - I'm not sure why with quality 0 - the image size is larger than the actual size.
Secondly is this the best way to go about in PHP to compress images before rendering them in the browser to improve performance.
Based on the suggestions, that its better to compress the image once at the time of saving - I digged up the code that is called by the flash program to generate the snap shot -
$video = $this->_getParam('video');
$imgContent = base64_decode($this->_getParam('snapshot'));
file_put_contents("snapshots/" . $video . ".png", $imgContent);
EDITED
Based on Alvaro's suggestion, I have made the following modification to the code which generates a much small jpg file
$video = $this->_getParam('video');
$imgContent = base64_decode($this->_getParam('snapshot'));
file_put_contents("snapshots/" . $video . ".png", $imgContent);
$filename="snapshots/".$video.".png";
$img = imagecreatefrompng($filename);
imagejpeg($img,'test.jpg',75);
So now this is a 3 step process
create the initial image using file_put_contents
Use imagecreatefrompng and imagejpeg to compress the file and generate a smaller image
Delete the orig image
Is this the best optimal way to go about it.
Since PNG uses lossless data compression, the only way to achieve a decent compression in a PNG image (edge cases apart) is to save it as palette (rather than true colour) and reduce the number of colours. You appear to be processing some sort of screenshots. You may obtain smaller file sizes is you use a lossy compression, i.e., save as JPEG. In either cases, you reduce both file size and picture quality. You could also try the GIF format, which tends to be smaller for small graphs.
Last but not least, you should compress images once (typically when they get uploaded), not every time they're served. I suppose your code is just a quick test but I mention just in case.
Answer to updated question:
I'm not familiar with PHP image functions but you should probably use a combination of imagecreatefrompng() and imagejpeg(). Also, consider whether you need to keep the original PNG for future reference or you can discard it.
You have not understood the last parameter, that's not quality that's the compression level so increasing it will decrease the image size. Anyway i've used that method before to compress png images and it works well so i think you should continue to use it.
1- The result seems accurate since 0 means no compression
quality
Compression level: from 0 (no
compression) to 9.
It's normal for the 0ed file to be larger than the original (that can be slightly compressed to begin with). You need to understand file compression and PHP GD image constructor.
2- IMHO, the wisest choice would be to compress your png file before uploading them on your server (of course, it states only if you have the choice : static, few files).
Help for that :
http://www.webreference.com/dev/graphics/compress.html
http://www.punypng.com/
http://omaralzabir.com/reduce_website_download_time_by_heavily_compressing_png_and_jpeg/
If it means to be dynamic, the php is the choice.