Php Script Not Completing? Resizing Dir of Imgs Timing Out

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spiffy577
Forum Newbie
Posts: 3
Joined: Tue Dec 25, 2007 12:09 am

Php Script Not Completing? Resizing Dir of Imgs Timing Out

Post by spiffy577 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello-

   I have a java uploader that uploads a bunch of files (images) to a temporary folder.  That isn't the problem.  After they are uploaded, a php script processes them.  It creates a directory (images) and a thumb directory (images/thumbs), then the script resizes for both the main image and the thumb.  Now, for some reason, the script only processes about 100 images (out of the 350+ or so) and then just stops.  Now, I know there is an execute time and a file size so I upped them to outrageous amounts.  Still no luck.  It just stops then stops loading the rest of the script.  Not sure why.  It seems to time out about 30 seconds in or so.  Anyway, the script is below, called with CreateCustomer.  Also, I am hosting on Godaddy if that helps.  Btw, ignore all of the directory info.  I changed it to keep it a bit more private.  That part works fine.  

Thanks for any info!  

    -Josh

P.S.  Side question, sometimes getimagesize returns 0 as the dimension sizes.  Any idea as to why?  Tia!!!

Code: Select all

<?php 
ini_set("set_time_limit", "1000000");

function resizeImage($filename, $width, $height)
{
	
	// Content type
	if (file_exists($filename))
	{
	// Get new dimensions
		list($width_orig, $height_orig, $type, $attr) = @getimagesize($filename);
		
		if ($height_orig == 0 || $width_orig == 0)
		{
			$image = open_image($filename);
			$width_orig = imagesx($image);
			$height_orig = imagesy($image);
		}
		
		if ($width && ($width_orig < $height_orig)) {
		   $width = ($height / $height_orig) * $width_orig;
		} else {
		   $height = ($width / $width_orig) * $height_orig;
		}
	
		// Resample
		$image_p = @imagecreatetruecolor($width, $height);
		$image = @imagecreatefromjpeg($filename);
		@imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
		
		// Output
		@imagejpeg($image_p, "/home/content/create/tempimages/old.jpg", 90);
	}
}

function open_image ($file) {
        # JPEG:
        $im = @imagecreatefromjpeg($file);
        if ($im !== false) { return $im; }

        # GIF:
        $im = @imagecreatefromgif($file);
        if ($im !== false) { return $im; }

        # PNG:
        $im = @imagecreatefrompng($file);
        if ($im !== false) { return $im; }

        # GD File:
        $im = @imagecreatefromgd($file);
        if ($im !== false) { return $im; }

        # GD2 File:
        $im = @imagecreatefromgd2($file);
        if ($im !== false) { return $im; }

        # WBMP:
        $im = @imagecreatefromwbmp($file);
        if ($im !== false) { return $im; }

        # XBM:
        $im = @imagecreatefromxbm($file);
        if ($im !== false) { return $im; }

        # XPM:
        $im = @imagecreatefromxpm($file);
        if ($im !== false) { return $im; }

        # Try and load from string:
        $im = @imagecreatefromstring(file_get_contents($file));
        if ($im !== false) { return $im; }

        return false;
}

function createCustomer($name, $name2)
{
	set_time_limit(0);

	$blnSuccess = delete_directory('/home/content/onlineimages/'.$name);
	if ($blnSuccess)
		print "<script>alert('".ucfirst($name)." existed.  Now erased...');</script>";
	
	createDirStruct($name);
	makeThumbs($value, $name);

[color=red][b]// SCRIPT NEVER MAKES IT TO THIS POINT BELOW[/b][/color]

	echo '<span class="medHeader">'.$name.' is created!</span>';
}

function makeThumbs($value, $name)
{
    $handler = opendir('/home/content/create/tempimages');
	$filenum = 1;



    // keep going until all files in directory have been read
[color=red] [b]  // I TRIED IT THIS WAY TO SEE IF IT WASN'T READING ALL OF THE FILES.  IT DOES.[/b] [/color]    
while ($file = readdir($handler)) {
        // if $file isn't this directory or its parent, 
        // add it to the results array
        if ($file != '.' && $file != '..')
            $results[] = $file;
    }

    // tidy up: close the handler
    closedir($handler);

	echo "Number of Images: ".count($results)."<br>\r\n";

	for ($i=0;$i<=count($results); $i++)	
	{
		$file = $results[$i];
        if (stristr($file, ".jpg"))
		{

			$strNum = $filenum + '';
			$strNum = str_pad($strNum, 4, "0", STR_PAD_LEFT);
			
			copy("/home/content/create/tempimages/".$file, "/home/content/create/tempimages/temp.jpg");

			resizeImage("/home/content/create/tempimages/temp.jpg", 150, 150);
			moveFile("/home/content/create/tempimages/old.jpg", "/home/content/onlineimages/".$name."/images/thumbs/tn".$name.$strNum.".jpg");

			resizeImage("/home/content/create/tempimages/temp.jpg", 600, 600);
			moveFile("/home/content/create/tempimages/old.jpg", "/home/content/onlineimages/".$name."/images/".$name.$strNum.".jpg");
			
		if (file_exists("/home/content/onlineimages/".$name."/images/thumbs/tn".$name.$strNum.".jpg"))			
				$filenum++;
		}

    }

}

function moveFile($oldfile, $newfile)
{

if (!rename($oldfile, $newfile)) {
	echo $oldfile." ".$newfile." failed to copy $file...<br>";
}

}

function createDirStruct($value)
{
	$value = strtolower($value);
	mkdir("/home/content/onlineimages/".$value);
	mkdir("/home/content/onlineimages/".$value."/images");
	mkdir("/home/content/onlineimages/".$value."/images/thumbs");
		
}
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Turn on error reporting. The cause should be obvious.

(Psst: set_time_limit() not ini_set() ! ;-)
spiffy577
Forum Newbie
Posts: 3
Joined: Tue Dec 25, 2007 12:09 am

Yeah, Tried That

Post by spiffy577 »

I did turn on error reporting and nothing was shown. I also removed the @'s from the beginning of some of the functions. Same deal. Nothing... I am horribly confused....
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

You're attempting to set a non-existent ini directive. Replace ini_set() with set_time_limit(1000000). also note that it will have no effect if Godaddy is running PHP in safe mode.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

ini_set("set_time_limit", "1000000");
set_time_limit is not a ini configuration value, it's actually max_execution_time. Although it is better to use set_time_limit(0); as Ambush Commander has already suggested :wink:
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Another note: I've seen many hosting companies kill misbehaving scripts with daemons to ensure they don't bog down servers. You may want to use `nice` to limit your CPU usage.
Post Reply