RyanMinor Posted February 20, 2012 Share Posted February 20, 2012 I have a quick question about a script I wrote that searches all .jpg images in a directory (and it's sub-directories) and makes thumbnails of them. The problem I am having is that I am getting an internal server error after about 15 seconds. I have included my script, the class it requires, and my php.ini file that resides in the root of my site. I have searched for a couple of hours online and haven't been able to come up with an answer as to why I am getting that error. The thumbnail file... <?php set_time_limit(0); require_once('classes/Create_Thumbnail.php'); $filter = '.jpg'; $directory = 'media'; // Do not include a trailing slash $it = new RecursiveDirectoryIterator("$directory"); foreach(new RecursiveIteratorIterator($it) as $file) { if (!((strpos(strtolower($file), $filter)) === false) || empty($filter)) { $items[] = preg_replace("#\\\#", "/", $file); } } foreach ($items as $item) { $photo = new Create_Thumbnail($item); $photo->createThumbnail(); } ?> The Create_Thumbnail class... <?php class Create_Thumbnail { private $_photo; private $_photoBasename; private $_photoWidth; private $_photoHeight; private $_photoType; private $_resizedPhoto; private $_thumbFolder; private $_thumbWidth = 125; private $_thumbHeight = 125; private $_thumbSuffix = '_thumb'; private $_thumbnail; /** * Constructor retrieves photo's basename, extension, width, and height. */ public function __construct($photo) { $this->_photo = $photo; $this->_photoBasename = pathinfo($this->_photo, PATHINFO_FILENAME); $this->_extension = pathinfo($this->_photo, PATHINFO_EXTENSION); $this->_thumbFolder = dirname($photo) . '/'; list($this->_photoWidth, $this->_photoHeight, $this->_photoType) = getimagesize($this->_photo); } /** * Method to resize the original image to a size that is much closer to the desired thumbnail size. */ public function resize() { $photoRatio = $this->calculatePhotoRatio(); if ($photoRatio != 1) { $this->_resizedWidth = round($this->_photoWidth * $photoRatio); $this->_resizedHeight = round($this->_photoHeight * $photoRatio); } else { $this->_resizedWidth = $this->_thumbWidth; $this->_resizedHeight = $this->_thumbHeight; } $resource = $this->createResource($this->_photoType, $this->_photo); $resized = imagecreatetruecolor($this->_resizedWidth, $this->_resizedHeight); imagecopyresampled($resized, $resource, 0, 0, 0, 0, $this->_resizedWidth, $this->_resizedHeight, $this->_photoWidth, $this->_photoHeight); $this->_resizedPhoto = $this->_thumbFolder . $this->_photoBasename . '_resized.' . $this->_extension; $this->createNewImage($this->_photoType, $resized, $this->_resizedPhoto); imagedestroy($resource); imagedestroy($resized); return $this->_resizedPhoto; } /** * Method to create the thumbnail. */ public function createThumbnail() { $source = $this->resize(); list($width_original, $height_original, $type) = getimagesize($source); $base_name = pathinfo($source, PATHINFO_FILENAME); $base_name = str_replace('_resized', '', $base_name); $extension = pathinfo($source, PATHINFO_EXTENSION); $source_x = ($width_original / 2) - ($this->_thumbWidth / 2); $source_y = ($height_original / 2) - ($this->_thumbHeight / 2); $resource = $this->createResource($type, $source); $thumb = imagecreatetruecolor($this->_thumbWidth, $this->_thumbHeight); imagecopyresampled($thumb, $resource, 0, 0, $source_x, $source_y, $this->_thumbWidth, $this->_thumbHeight, $this->_thumbWidth, $this->_thumbHeight); $this->_thumbnail = $this->_thumbFolder . $base_name . $this->_thumbSuffix . '.' . $extension; $this->createNewImage($type, $thumb, $this->_thumbnail); unlink($source); return $this->_thumbnail; } /** * Method to create an image resource. */ public function createResource($type, $source) { switch ($type) { case 1: $resource = imagecreatefromgif($source); break; case 2: $resource = imagecreatefromjpeg($source); break; case 3: $resource = imagecreatefrompng($source); break; } return $resource; } /** * Method to calculate the photo ratio. */ public function calculatePhotoRatio() { if ($this->_photoWidth > $this->_photoHeight) { $maximumHeight = $this->_thumbHeight; $photoRatio = $maximumHeight / $this->_photoHeight; } elseif ($this->_photoWidth < $this->_photoHeight) { $maximumWidth = $this->_thumbWidth; $photoRatio = $maximumWidth / $this->_photoWidth; } else { $photoRatio = 1; } return $photoRatio; } /** * Method to create a new image. */ public function createNewImage($type, $source, $destination) { switch ($type) { case 1: if (function_exists('imagegif')) { imagegif($source, $destination); } else { imagejpeg($source, $destination, 50); } break; case 2: imagejpeg($source, $destination, 100); break; case 3: imagepng($source, $destination); break; } } } The php.ini file... file_uploads on upload_max_filesize = 150M post_max_size = 150M max_input_time = -1 max_execution_time = 0 memory_limit = 150M register_argc_argv = false Quote Link to comment https://forums.phpfreaks.com/topic/257339-thumbnail-script-timing-out/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 20, 2012 Share Posted February 20, 2012 Add the following two lines of code immediately after the first opening <?php tag to see if there are any php detected errors - ini_set("display_errors", "1"); error_reporting(-1); Quote Link to comment https://forums.phpfreaks.com/topic/257339-thumbnail-script-timing-out/#findComment-1319040 Share on other sites More sharing options...
RyanMinor Posted February 20, 2012 Author Share Posted February 20, 2012 The only error I am getting is the internal server error after about 15 seconds. I am using Go Daddy hosting if that provides any insight. Quote Link to comment https://forums.phpfreaks.com/topic/257339-thumbnail-script-timing-out/#findComment-1319041 Share on other sites More sharing options...
kicken Posted February 20, 2012 Share Posted February 20, 2012 GoDaddy will kill scripts that use too much resources or that persist for too long. You may be running into this problem. Quote Link to comment https://forums.phpfreaks.com/topic/257339-thumbnail-script-timing-out/#findComment-1319050 Share on other sites More sharing options...
PFMaBiSmAd Posted February 20, 2012 Share Posted February 20, 2012 A) Are any thumbnails getting created? B) How about downloading the source images to your development system, creating all the thumbnails, and uploading the thumbnails to the live server. Quote Link to comment https://forums.phpfreaks.com/topic/257339-thumbnail-script-timing-out/#findComment-1319059 Share on other sites More sharing options...
RyanMinor Posted February 20, 2012 Author Share Posted February 20, 2012 Yes, thumbnails are being created. I actually finished everything by specifying a each sub-directory within the main photo directory. The script worked just as I had intended with the exception of giving me the error I specified. Honestly, this is not the first issue I have run into with GoDaddy hosting. I assumed before even posting this question that it was GoDaddy killing my script. However, after looking over my code does everything look good? It's actually one of my first from-scratch OOP scripts. Quote Link to comment https://forums.phpfreaks.com/topic/257339-thumbnail-script-timing-out/#findComment-1319063 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.