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.
Related
I've spent a lot of time Googling and searching SO for information on image manipulation libraries with little success. Please direct me if this has been answered before and I just can't find it.
Basically I'm trying to resize an arbitrarily sized image to a couple of smaller thumbnail images, say 400px wide and 200px wide while maintaining the original aspect ratio. The original image is being uploaded via php (linux) and I've found that I can use Cairo, GD, GMagick, or ImageMagick but I've been unable to find comprehensive data on which is better suited for image manipulation. I have found comparisons for image creation, but that's functionality I won't be using.
I also have the option of uploading via php then performing the image manipulation via another method (perl/python/etc, for example) if that proves better suited.
Any pointers in the right direction are appreciated. Quality is my primary motivation followed by output image file size then library performance.
GD and ImageMagick are your best bets, so get some representative images and run some tests using:
imagecopyresampled() for GD
Imagick::thumbnailImage() for ImageMagick
Imagick::resizeImage() if you want more control and to try a few filter constants.
Resizing images in PHP with GD and Imagick has some code samples to get you going.
Compare them visually and by filesize. If there's no obvious winner, then run some benchmarks based on your expected needs. How to benchmark efficiency of PHP script is a good reference point.
Also take a look at:
Choosing between Imagemagick & GD for thumbnail creation
Should I use ImageMagick or GD2 with ImageAPI in Drupal?
I need to do the following image manipulations for images uploaded by users on my site:
Resize images (if greater than a certain dimension)
Convert all image formats to jpg's
Add a watermark to the bottom of all images
Do I need to use either the MagickWand or iMagick extensions or can I just get away with running the terminal commands inside PHP's exec function?
Is there a reason why the PHP extensions would be preferred? Which would be faster and better for performance (this site may have lots of users and there would be a ton of image processing at any given time)?
I'd like to make a counterpoint to drew010's answer. In his answer he states:
You would benefit a lot using the PHP extensions instead of using exec
or similar functions. Built in extensions will be faster and use less
memory as you will not have to spawn new processes and read the output
back
For processing images, this is true. Well, I know for a fact that calling on ImageMagick binaries using PHP's exec() function (or similar functions) carries additional overhead above using embedded PHP libraries however I'm not clear on how much overhead there is.
But there's another side of the coin. If you embed the ImageMagick code into PHP via an extension, then every request using PHP takes more memory whether the request processes an image or not. If you're using Apache's mod_php then this becomes even more of an issue because now every request to your server has the ImageMagick libraries in memory even if it's serving an HTML file!
So it really depends. You really want to keep PHP's memory footprint as low as possible. If you're processing a large number of requests which require ImageMagic and you're using FastCGI or php_fpm, then you'll probably see a benefit to using embedded ImageMagick. But if you're only occasionally processing requests using ImageMagick and/or using Apache's mod_php then you may get much better performance calling ImageMagick via exec().
You would benefit a lot using the PHP extensions instead of using exec or similar functions. Built in extensions will be faster and use less memory as you will not have to spawn new processes and read the output back. The image objects will be directly available in PHP instead of having to read file output, which should make the images easier to work with.
If you have a busy site, creating lots of processes to edit images may start to slow things down and consume additional memory.
I always use PHP GD http://php.net/manual/en/book.image.php
You can accomplish resizing, converting to JPG and watermarking your images. I know your post said you have to use MagickWand or iMagick, but I just wanted to present this option in case it would work for you.
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.
I have an image GD script which is currently using around 9MB of memory.
I get a lot of traffic and hence sometimes it using up hell lot of RAM on my server.
Is there any way to reduce memory usage for image gd?
Or at-least make script process faster so that it de-allocates the memory which it is using, faster.
I have tried changing image quality, it had no effect.
I also tried changing image pixel size, it reduced the memory usage, but not much.
Thanks.
It's impossible to tell without seeing the code, but unless it contains any major mistakes, then the answer is probably no.
What you might be able to do is use the external imagemagick binary instead - it runs outside PHP's script memory limit - but that is an entirely different technology, and would require you to rewrite your code.
I assume you are already caching GD's results so not every request causes it to run?
Try avoiding using image GD on the fly if you are concerned about memory limits.
It's hard to solve the problem without seeing code, but i can make a suggestion.
Have a different process handle the images, for example, if you want to resize images, don't resize them everytime the user access a page, instead run a cron or a scheduler with window to resize all the images that needs to be resized periodically and save them. so there will be less overhead.
If you provide more code you will get better help
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/