noj75 Posted May 26, 2009 Share Posted May 26, 2009 Greetings all, I am struggling with a script on my server that is using alot of memory. I have upped the max file size and the memory limit to what my hosts will allow me to use. The code below is the main "feature" script of the whole thing. I am wondering if there is a way to optimize it at all. My clients NEED to upload photos, some over 2-3MB. $image = $_FILES["new_image"]["name"]; $uploadedfile = $_FILES['new_image']['tmp_name']; /* START DEALING WITH THE UPLOADED IMAGE */ if ($image) { $filename = stripslashes($_FILES['new_image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); $uploadedfile = $_FILES['new_image']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); list($width,$height) = getimagesize($uploadedfile); /* SET LARGE PHOTO SIZES */ $newwidth=600; $newheight=450; $tmp = imagecreatetruecolor($newwidth,$newheight); /* SET THUMBNAIL SIZES */ $newwidth1=250; $newheight1=188; $tmp1 = imagecreatetruecolor($newwidth1,$newheight1); /* COPY THE IMAGES */ imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height); /* RENAME THE PHOTOS */ $newimagename = $hname.'.'.$extension; $newimagename1 = $hname.'.'.$extension; /* STORE THE IMAGE AND THUMBNAIL */ $filename = "../../****/images/****/". $newimagename; $filename1 = "../../****/images/****/thumbs/". $newimagename1; imagejpeg($tmp,$filename,100); imagejpeg($tmp1,$filename1,100); imagedestroy($src); imagedestroy($tmp); imagedestroy($tmp1); } Any help appreciated as I am working on abit of a deadline. Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/159741-can-this-image-script-be-optimized/ Share on other sites More sharing options...
noj75 Posted May 27, 2009 Author Share Posted May 27, 2009 Anyone have any sort of answer for this? i.e can it be optimized in any way. Sorry to be an annoyance. Regards Quote Link to comment https://forums.phpfreaks.com/topic/159741-can-this-image-script-be-optimized/#findComment-842941 Share on other sites More sharing options...
thebadbad Posted May 27, 2009 Share Posted May 27, 2009 I guess you could spread out the load by creating and storing the large image first, destroy it, and then create the thumbnail (based on the large image). So you aren't handling two images simultaneously. Quote Link to comment https://forums.phpfreaks.com/topic/159741-can-this-image-script-be-optimized/#findComment-842963 Share on other sites More sharing options...
kickstart Posted May 27, 2009 Share Posted May 27, 2009 Hi To extend the above point, if the quality of the smallest version isn't paramount then create the medium size one from the large one, destroy the large copy, then create the small copy from the medium copy. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/159741-can-this-image-script-be-optimized/#findComment-842969 Share on other sites More sharing options...
aschk Posted May 27, 2009 Share Posted May 27, 2009 I agree with "thebadbad". You appear to be creating 3 images in memory (the original, and 2 tmps). If you are deleting the original why don't you just alter that memory image directly and save it before remove the tmp. Also put it all into a function, something like: function createResizedJpeg($src, $dst, $toWidth, $toHeight){ static $srcFile; static $srcResource; static $srcWidth; static $srcHeight; // If we don't have a cached file source create a new one. if(is_null($srcFile) OR $src !== $srcFile){ $srcFile = $src; $srcResource = imagecreatefromjpeg($src); list($srcWidth,$srcHeight) = getimagesize($srcFile); } // Create tmp. $tmp = imagecreatetruecolor($toWidth, $toheight); // Resize imagecopyresized($tmp, $srcResource, 0, 0, 0, 0, $toWidth, $toheight, $srcWidth, $srcHeight); imagejpeg($tmp,$dst,100); imagedestroy($tmp); } I make no guarantees the above code will be any better but to make sure we're not recreating the src file resource it uses a simple caching technique. You may wish to put in additional checks to make sure the src image filename is actually a jpeg. Quote Link to comment https://forums.phpfreaks.com/topic/159741-can-this-image-script-be-optimized/#findComment-842973 Share on other sites More sharing options...
noj75 Posted May 27, 2009 Author Share Posted May 27, 2009 Hi guys, Thanks for the replies. I am not too great with PHP at present. Could anyone give me an example on how to re-arrange the code to deal with the large image first and then the small thumb. Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/159741-can-this-image-script-be-optimized/#findComment-842996 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.