I am using the code below to loop thru the contents of a directory and make a thumbnail of all the images. The directory contains only .jpg images.
Two questions:
Is there a better method of doing this? This process takes too long. It is stopping (unfinished) at 10 minutes.
Should I be expecting an email from my host regarding processor hogging. Or is this a relatively light process?
// error_reporting(E_ALL); set_time_limit (600); /* Adapted from [url]http://uk2.php.net/manual/en/ref.image.php[/url] * resizeToFile resizes a picture and writes it to the harddisk * $sourcefile = the filename of the picture that is going to be resized */ function resizeToFile ($sourcefile) { /* Get the dimensions of the source picture */ $picsize=getimagesize("$sourcefile"); $source_x = $picsize[0]; $source_y = $picsize[1]; $source_id = imageCreateFromJPEG("$sourcefile"); /* Create a new image object (not neccessarily true colour) */ $target_id=imagecreatetruecolor(160, 125); /* Resize the original picture and copy it into the just created image object. */ $target_pic=imagecopyresampled($target_id,$source_id,0,0,0,0,160,125,$source_x,$source_y); /* Create a jpeg with the quality of 30 out of the image object "$target_pic". This will be saved as $targetfile */ $targetfile = str_replace(".jpg","_small.jpg",$sourcefile); imagejpeg ($target_id,$targetfile,30); return true; } /* open photos directory */ $handle = opendir('photos'); /* loop through it */ while (false !== ($file = readdir($handle))) { /* if not already a thumbnail */ if ($file != "." && $file != ".." && substr($file,-10) != "_small.jpg") { /* create a thumbnail */ resizeToFile ("photos/" . $file); } } /* close directory */ closedir($handle);
that should be fine to run. the only reason it stops after 10 minutes is because of the top line: set_time_limit (600); just put set_time_limit(0); // for unlimited