Jump to content

Thumbnail Script Timing Out


RyanMinor

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/257339-thumbnail-script-timing-out/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.