I have a page where I can upload an image and php will create 2 down-scaled images. One is for the thumbnail which is max width of 125px and max height of 95px and the other for the 'main' display which is max width of 550px and max height of 600px - the original is kept and it used as the high quality one (as a link). Previously I just had the large image but then users need to download a lot of data they don't necessarily want..
I'll try to post my understanding of what my code is doing and hopefully it's not too inaccurate..
They way I'm resizing is to set the width to x (either 125 or 550) and then scale the height keeping the original ratio - if the heigh exceeds my max I re-do the scaling with height done first.
list($image_width, $image_height, $image_type) = getimagesize($path);
$ratio = $image_height / $image_width;
$new_width_thumb = 125;
$new_height_thumb = $new_width_thumb * $ratio;
$new_height_thumb = round($new_height_thumb);
#Check height after scaling; if thumb height is > 95, resize with height as limiting
if($new_height_thumb > 95)
$new_height_thumb = 95;
$new_width_thumb = $new_height_thumb * (1/$ratio);
$new_width_thumb = round($new_width_thumb);
I then repeat this for the second scaled image so I have my two scaled dimentions.
Then I create 'blank images' and copy the image into the newly sized 'blank images'
$blank_image_thumb = imagecreatetruecolor($new_width_thumb, $new_height_thumb);
$blank_image_main = imagecreatetruecolor($new_width_main, $new_height_main);
//After checking the file type..
$source_image_thumb = imagecreatefromjpeg($path);
$source_image_main = imagecreatefromjpeg($path);
imagecopyresampled($blank_image_thumb, $source_image_thumb, 0, 0, 0, 0, $new_width_thumb, $new_height_thumb, $image_width, $image_height );
imagecopyresampled($blank_image_main, $source_image_main, 0, 0, 0, 0, $new_width_main, $new_height_main, $image_width, $image_height );
imagejpeg($blank_image_thumb, $raw_path . "thumb_" . $file_name);
imagejpeg($blank_image_main, $raw_path . "main_" . $file_name);
I'm thinking the imagecreatefromjpeg or the imagecopyresampled is storing the 'big' image in the ram and that is causing my php to run out of memory (smaller res images work fine - less than say 1500px2).
I have read that I can up this limit in my php.ini but I'm not sure if my host will do this and more importantly I read that I'd be better off fixing my code's efficiancy..
So is there a more efficient way to do what I want without removing any automation? (I don't want to be resizing and uploading 3 images!)
I hope I've made sense..