Question

i am using a class written by someone to resize the image and store it in the server while uploading here is the link to the class.

http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php

the above class only worked for low resolution images, as i wanted to upload the images with high resolution roghly(4400px X 3500px) and then crop it to fixed size, i used the below php function to increase the memory size

ini_set ( "memory_limit", "100M");  

although with that i am able to achieve my desired output i would like to know if this has any cons, as i am allotting a larger memory space to it, do i need to free memory by some means after performing the operation.

i am using the following code to resize.

ini_set ( "memory_limit", "100M"); 
$image = new SimpleImage();
$image->load("$targetFile");
$image->resize(940,650);
$image->save("$targetFile");

i would appreciate if someone explains me if i am doing this wrong way, or is it okay to go with this?

thank you

Was it helpful?

Solution

Well, the downside is that, of course, the PHP page can be consuming 100 megabytes of memory to process the request - if this is a commonly used page, or there is a risk of multiple calls to this page at a time, it is possible that server performance could be compromised (since 10 requests to the code running concurrently would consume 1G of memory)

Using ini_set instead of altering this value elsewhere in php.ini or in the webserver configuration (e.g. .htaccess files) does mean the scope of scripts to consume memory is likely smaller - scripts need to explicitly request use of extra memory first.

There isn't really a better way of doing it that is still in PHP - for better performance, though, you might want to consider making use of ImageMagick to resize the image.

OTHER TIPS

Some explanation as to why the class uses so much memory:

The $image->load() method in the class you're using invokes imagecreatefromjpeg() / imagecreatefromgif() / imagecreatefrompng() (depending on the format of the original file) and loads the image into memory. It is my understanding that the GD library stores images in bitmap format while in memory, which are obviously much bigger than the compressed file you loaded from the disk. You can calculate the amount of memory required with the equation:

image width * image height * colour depth / bits per byte

In your example above, this is:

4400 * 3500 * 24 / 8 = 46.2MB (roughly)

And that's just to load the original image in, it gets worse! :p

To perform a resize, the class creates a blank truecolor image with the new dimensions and then resamples the original image in memory onto the new one. This means that at a point in the execution of the script you have two bitmap images being stored in memory. Multiply this by the number of simultaneous users running the script and add the overhead of all your other PHP stuff, and you'll find you're probably using a lot of memory!

See this thread for some examples: http://www.webdeveloper.com/forum/showthread.php?t=182328

Actually you're doing it wrong... init_set() cannot set the memory_limit using the short-hand notation (100M), instead you must use integer values (in bytes). Please see here.

So, you should use

ini_set ( "memory_limit", 100*1024*1024); 

instead.

EDIT

Forget what I wrote above. Even if this is stated in the PHP manual it doesn't seem to apply to recent PHP versions. I tried it with PHP 5.3.3 on Mac OS X and it works even with short-hand notation. Thanks to both posters to point that one out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top