JPEG Optimization from PHP running on hosted accounts - php

I am looking for a loss-less JPEG optimization tool that I can include in a PHP-based photo gallery software.
I already checked the following question, but ruled out the tools mentioned there for reasons I'll explain below:
Tools for JPEG optimization?
The command line tools jpegtran and jpegoptim are not available in the average PHP hosting account. The web-based tools (www.smush.it, www.kraken.io) all have a limit of 1 MB for processed images.
The 1 MB limit is a problem, because the gallery software delivers images based on browser-size and I also want to support Full HD and even larger displays. Depending on content, photos prepared for such resolutions can get larger than 1 MB.
Any hints on a solution are appreciated!

OK, I found my answer in another stack overflow question:
JPG File Size Optimization - PHP, ImageMagick, & Google's Page Speed
ImageMagick already does the Huffman optimization. I assumed it doesn't because my ImageMagick files were still larger then the ones from jpegtran & Co. However, that was only because I did not strip the metadata.
Cheers, Robert

There are no solution.
Either use some command-line utility or increase memory limit.
(and, to let you know, it's not target image file size but uploaded image dimensions that puts you into trouble with memory limit.

Related

Compress images with PHP

I process uploaded images by php to save (after resize) by imagejpeg. As I explored, imagejpeg is the best php command to compress jpg images to reduce the file size. However, when I check my website by Google Page Speed, it says all of my images can be compressed 4-10%.
What is the common method to compress images to meet the Google standard?
Googles "standard" is the possible maximum to be expected by google. You need to use highly optimized image compressors that does nothing else than image compressing and therefore get a possible maximum best value here.
You can for example, open you jpeg file in a image editor like adobe photoshop and create the possible maximum best compression to be expected by you while having visual control. Highly recommended.
The GD library provides a standard conform jpeg compression which should match a library users expectation, but which might not satisfy a graphic designer (and/or google by 4-10%).

mass image optimizer

I have a folder with about 5000 images in it.
I can use php, or software on a local version of the images if required.
Whats the best way to go through and optimize/shrink their size/compress them as quickly and efficiently as possible to 20 width and 20 height?
Anything with a GUI that works on XP?
Tools like mogrify or convert (from ImageMagick) will do just fine, when it comes to resizing images.
After, if you want to optimize your images further (reducing their size in bytes), you should take a look at software like :
optipng or pngcrush for PNG,
or jpegtran for JPEG
If your goal is optimization for a website, take a look at Best Practices for Speeding Up Your Web Site - Images and the points which follow (I'm especially thinking about CSS Sprites)
Edit after edit of the OP : hu... a GUI ? No idea, sorry (I have those kind of tools run automatically, from scripts, on Linux servers).
Why not just write a script that will call those tools on all your images, and let it run for the night ?
Use either of imagemagick or GD2 .. GD2 is already bundled in the newer versions (5.0+) of PHP. This way, you won't have to install new extension.
This tutorial is what you are looking for.

Should GD need so much memory when resizing?

I have a resize script i made in PHP that uses GD (my VPS doesn't have imagemagick installed) to resize an image, I have recently started getting memory errors so i have increased the memory_limit up to 50Mb and still get memory error.
The image I am trying to resize is only 2Mb, is this correct for PHP image stuff, something sounds a bit wrong to me.
To resize the image GD has to work on the uncompressed image, which is significantly larger than 2MB, I assume. Imagemagick needs to store the entire image data (pixels * bit_depth) and quite some more for the actual work in memory.
50 Megabytes is not much for working with images. For example, Drupal warns you if you have less than 96MB memory limit, if you have the image resizing, etc.. enabled. For reasonably sized images 64MB are enough in my experience, but if you put full size images from a digital camera you'll run into problems with that memory limit.
On my shared/cloud hosting (2.7£/month) I still did not see any warning/error when resizing images. I set the limit to 200MB (sometimes users need to upload very large images). As Fabian said, I guess 50 is too low.

PNG compression with PHP magickwand

I'm wondering how to compress an PNG image correctly.
The situation is this :
I have a PNG image compressed and color-reduced with Irfanview on Windows. It's about 20KB.
When my portal software resizes (using magickwand 1.0.7) it with default values, it's about 63K (!).
Next try was to call MagickSetImageDepth($this->_imageHandler,8), resulting in a filesize of 34K, which is better, but still it's bigger than the (larger in dimension) original file.
None of the documented functions seem to fit to further compress the image.
Any hint would be greatly appreciated !
Greetz,
Sosa
PNG compression programs and routines use different techniques. I've found out that many times, an image that's already been compressed (or saved efficiently) cannot be compressed further or even has a higher file size as you are experiencing.
In your case I'd say your images cannot be compressed further, at least using MagickWand. You might just want to leave out that step.
Perhaps optimizing your PNGs before runtime would be a solution. There are many options available in this case. I've had luck with PNGGauntlet. You can run a batch job on PNGGauntlet and it will skip over the files that it would've made larger, if any.
Try this tool by Yahoo - it's great!
http://developer.yahoo.com/yslow/smushit/

PHP Thumbnails

I was looking at a way to dynamically create thumbnails using PHP and GD but everytime i select a large image maybe 10MegaPixels about 4-5MB it gives the error
**images/Surabhi_Cow.jpgimages/tn/Surabhi_Cow.jpg
Fatal error: Allowed memory size of 31457280 bytes exhausted (tried to allocate 10368 bytes) in C:\Program Files\xampp\htdocs\MySite\Staff\test.php on line 51**
Changing the memory_limit in php.ini to 60 does the trick but my host only allows the memory_limit to 32M. What other options do I have to generate thumbnails on the fly?
I checked phpThumb() but don't really get it. So any other options are welcome!
You want to use ImageMagick. It is much more efficient in handling large images than GD.
If all you want to do is generate thumbnails. I recommend this nice little script called imagethumb.php. You can download it here:http://www.olivo.net/software/imagethumb/
This script produces excellent thumbnails with absolutely no pixelation. It accepts a height or width argument that you append to the URL that calls the script. It's really really easy to use and comes with documentation (which you'll read for all of 2 minutes).
I tried other thumbnailing scripts such as "ThumbsUp" (for example) before landing on this one. BTW, it also renders .png images and also .gif (if I recall correctly). The cache feature will make it easier on your server if you have large files. Also, I assume that your server has the GD library or ImageMagick installed. Good Luck ;)
As the others have said, if the images are that big it's time to drop GD and switch to ImageMagick. One word of warning though: do it all on the command-line - the class wrappers out there are wheels in need of damn good re-inventing, every last one.
Consider using a command-line based approach. For example, you can invoke ImageMagick from the command-line to resize images.
Other than that, in pure PHP, it's hard to see how you can edit images that are larger (in RGB format) than your RAM...
I was doing some research on the topic and I found Imagick much more efficient for manipulating bigger images. You`ll either pass the allowed memory or the maximum execution time. A better approach would be to use Imagick library. Check the onfo on how to generate thumbnails with php on the fly using Imagick.

Categories