hp1 Posted March 14, 2009 Share Posted March 14, 2009 I have a script that generates thumbnails and larger images from an original. The script works but I have noticed that when my gallery pages have a lot (i.e. 100) of thumbnails to generate it freezes the page. some of the thumnails have been successfully created while others have not. It appears to be worse on less powerful machines. How can i tell if the script is causing the server to run out of memory or not or indeed, is this the problem? Link to comment Share on other sites More sharing options...
tooldude Posted March 15, 2009 Share Posted March 15, 2009 I have a script that generates thumbnails and larger images from an original..... How can i tell if the script is causing the server to run out of memory or not or indeed, is this the problem?As one who only recently started playing with the GD library myself, I am not sure how to tell if the server is running out of memory. But I would have to ask, are you being sure to use the imagedestroy($image) function after each and every creation? That should free the memory after each creation.See:php GD library imagedestroy($image) Link to comment Share on other sites More sharing options...
jlhaslip Posted March 15, 2009 Share Posted March 15, 2009 100 thumbnails at a time might be stressing out the Server, indeed. There are limits for CPU usage, time limits and Ram limits as well. These settings can be adjusted by the Server Admin, but you need Root access. Likely not available on a shared server.Can you post your code so we can check stuff, please. Link to comment Share on other sites More sharing options...
hp1 Posted March 16, 2009 Author Share Posted March 16, 2009 As one who only recently started playing with the GD library myself, I am not sure how to tell if the server is running out of memory. But I would have to ask, are you being sure to use the imagedestroy($image) function after each and every creation? That should free the memory after each creation.See:php GD library imagedestroy($image) Yes, Imagedestroy is being used twice, once for destroying after Imagecreatefromjpeg and once for imagecreatetruecolor. Thanks for the suggestion tho Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2009 Share Posted March 16, 2009 If the server is running out of memory you'll get an error. For scripts that don't output HTML, it's probably best to have them use an error log and check there to see if there were errors. Adding this to the top of your script will write errors to the "error.log" file in the same directory as the script: error_reporting(E_ALL);ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');ini_set('html_errors', 0);ini_set('log_errors', 1);ini_set('display_errors', 0); Link to comment Share on other sites More sharing options...
Ingolme Posted March 16, 2009 Share Posted March 16, 2009 Instead of generating thumbnails right at the moment you need them, make the thumbnail and save it somewhere ("thumbs/" or something) the moment the file is uploaded. (If you're using a file uploading script for your images). Link to comment Share on other sites More sharing options...
hp1 Posted March 16, 2009 Author Share Posted March 16, 2009 Instead of generating thumbnails right at the moment you need them, make the thumbnail and save it somewhere ("thumbs/" or something) the moment the file is uploaded. (If you're using a file uploading script for your images).Interesting you should say that, I did exactly that. I used a different script that generated all the thumbnails to a thumbs/ directory. It doesnt seem to have made any difference which makes me think I have more serious problems with my server. I also removed a script that was generating HTML and just replaced it with code from 'view source' and is also makes no difference which sort of confirms that maybe my server has problems. I have contacted my host for help. Anyway just in case there are huge flaws in the script I am using here it is:The page is written using this:<?php$dir = "eb";if ($handle = opendir($dir)){ while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != ".htaccess" && $file != "ir.php" && $file != "vids" && $file != "tg.php" && $file != "tg2.php" && $file != "thumbs") { $name_array[] = $file; }}closedir($handle);}asort($name_array);while(list($key,$val)=each($name_array)){echo "<a href=\"eb/wide/$val\" rel=\"shadowbox[eb]\" title=\"\"><img class=\"gal\" src=\"eb/small/$val\" border=\"0\"></a>";}?> The two images (the thumbnail and the actual image to be displayed) are generated using GD using a PHP script and a .htaccess as follows: <?php// max_width and image variables are sent by htaccess$max_width = $_GET['max_width'];$max_height = 1000;$image = $_GET['image'];$size = GetImageSize($image);$width = $size[0];$height = $size[1];// get the ratio needed$x_ratio = $max_width / $width;$y_ratio = $max_height / $height;// if image allready meets criteria, load current values in// if not, use ratios to load new size infoif ( ($width <= $max_width) && ($height <= $max_height) ) { $tn_width = $width; $tn_height = $height;} else if (($x_ratio * $height) < $max_height) { $tn_height = ceil($x_ratio * $height); $tn_width = $max_width;} else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height;}// read image$src = ImageCreateFromJpeg($image);// set up canvas$dst = imagecreatetruecolor($tn_width,$tn_height);// copy resized image to new canvasImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width,$tn_height,$width,$height);// send the header and new imageheader('Content-type: image/jpeg');ImageJpeg($dst, "", 90);// clear out the resourcesImageDestroy($src);ImageDestroy($dst);?> .htaccess file: RewriteEngine onRewriteBase /eb/# scaling small, medium, largeRewriteRule ^small/([A-Za-z0-9_.]+).(jpg|gif|png|JPG)$ ir.php?max_width=70ℑ=$1.$2RewriteRule ^medium/([A-Za-z0-9_.]+).(jpg|gif|png|JPG)$ ir.php?max_width=200ℑ=$1.$2RewriteRule ^large/([A-Za-z0-9_.]+).(jpg|gif|png|JPG)$ ir.php?max_width=400ℑ=$1.$2RewriteRule ^wide/([A-Za-z0-9_.]+).(jpg|gif|png|JPG)$ ir.php?max_width=640ℑ=$1.$2 Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2009 Share Posted March 16, 2009 One problem I see is that you're redirecting requests for all image types, but only using imagecreatefromjpeg. That's not going to work for a gif or png. Link to comment Share on other sites More sharing options...
hp1 Posted March 17, 2009 Author Share Posted March 17, 2009 One problem I see is that you're redirecting requests for all image types, but only using imagecreatefromjpeg. That's not going to work for a gif or png.True, but it will only ever be me that uploads images to the gallery and therefore will always be jpegs. A fair point tho Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.