Jump to content

Archived

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

cstegner

*SOLVED*Fatal error: Maximum execution time of 30 seconds.!?!?! How do I get passed this?

Recommended Posts

I got this error while running a script that is going to make thumbnails of all 10,000 images on my server.

Fatal error: Maximum execution time of 30 seconds exceeded in /home/httpd/vhosts/bigornot.com/httpdocs/make_th.php on line 55

I don't care how long it takes to execute the script, so how can I get it to ignore this rule?

Thanks (need help ASAP)

Share this post


Link to post
Share on other sites
set_time_limit();
[a href=\"http://us2.php.net/function.set-time-limit\" target=\"_blank\"]http://us2.php.net/function.set-time-limit[/a]

be careful with that, though.

Share this post


Link to post
Share on other sites
Same error :( this time just on line 56 instead, because of the added line of code of course :P

Any other ideas?

ohh do I need to put a time between the brackets? I left them blank thinking that would give it an endless amount of time.

Share this post


Link to post
Share on other sites
Ok...problems.

Ok you were right.

You solved the timing out problem and I am not positive--because the file never totally loads--but I think the file crashes some where in the middle of the 2500 images in the first batch.

This is what I got now

[code]
<?
set_time_limit(0);

include("inc/db.inc");

$query = "SELECT photo2 FROM images";
$result = mysql_query($query);

while($info = mysql_fetch_array($result)){

gd2 image resize code

}
?>
[/code]

How can I set this up so that it will pace its self?

hmmn maybe something like setting a limit and having a header at the end of the file that would redirect it to the same page but tell it where the limit left off?

Other ideas?

Share this post


Link to post
Share on other sites
Ok this is soooooo frustrating.

This is the code I have right now. It never loads to the next page. Just keeps trying to load the one page. GRRR

Please help, this is all I have left.

[code]

<?
set_time_limit(0);

include("inc/db.inc");

if(!($lastimage)){
    $lastimage = "0";
}

$query = "SELECT * FROM images ORDER BY id LIMIT $lastimage, 10";
$result = mysql_query($query);

while($info = mysql_fetch_array($result)){

    $imagename = $info['photo1'];

    // !!!! MAKE THUMNAIL !!!!
    $sourcefile = "images/" . $imagename;
    $forcedwidth = "75";
    $forcedheight = "75";
    $destfile = "images/th_" . $imagename;

    $fw = $forcedwidth;
    $fh = $forcedheight;
    $is = getimagesize( $sourcefile );
    if( $is[0] >= $is[1] )
    {
        $orientation = 0;
    }
    else
    {
        $orientation = 1;
        $fw = $forcedheight;
        $fh = $forcedwidth;
    }
    if ( $is[0] > $fw || $is[1] > $fh )
    {
        if( ( $is[0] - $fw ) >= ( $is[1] - $fh ) )
        {
            $iw = $fw;
            $ih = ( $fw / $is[0] ) * $is[1];
        }
        else
        {
            $ih = $fh;
            $iw = ( $ih / $is[1] ) * $is[0];
        }
        $t = 1;
    }
    else
    {
        $iw = $is[0];
        $ih = $is[1];
        $t = 2;
    }
    if ( $t == 1 )
    {
        $img_src = imagecreatefromjpeg( $sourcefile );
        $img_dst = imagecreatetruecolor( $iw, $ih );
        imagecopyresampled( $img_dst, $img_src, 0, 0, 0, 0, $iw, $ih, $is[0], $is[1] );
        if( !imagejpeg( $img_dst, $destfile, 90 ) )
        {
            exit( );
        }
    }
    else if ( $t == 2 )
    {
        copy( $sourcefile, $destfile );
    }
    echo $destfile;
}
echo "done";
$newlast = "$lastimage + 10";
header("location: make_th.php?lastimage=$newlast");
?>

[/code]

Share this post


Link to post
Share on other sites
okay strip the header redirect.

write a quick check in your loop that will autoadvance if it finds a thumb already. If there's a thumb, it skips it -- no thumb, it runs.

[code]while($info = mysql_fetch_array($result)){

    $imagename = $info['photo1'];

    // !!!! MAKE THUMNAIL !!!!
    $sourcefile = "images/" . $imagename;
    $forcedwidth = "75";
    $forcedheight = "75";
    $destfile = "images/th_" . $imagename;
    if(!file_exists($destfile)) { // if the file doesn't exist -- do the following
        ... all your resize code here ...
    } // if the file exists, do nothing
} // end while loop
[/code]

if it bombs, just run it again -- keeps bombing on the same image, you might just have a bad one.

another bit of advice... I'd use imagecreatefromstring(file_get_contents($sourcefile)) instead of imagecreatefromjpeg() to create your images. that way, if there just happens to be a PNG, GIF, or other image file in there, it gets read just fine.

Share this post


Link to post
Share on other sites
Good idea on the file exist, and I think your right with the imagecreatefromjpeg(). That might have been causing it to crash.

So you are saying get rid of the header redirect and the limits and all of that stuff and just let the script run with the above changes?

With these changes alone you think it is alright to let it rip on 4,000 images?

Thanks for the help.

Share this post


Link to post
Share on other sites
The header just loops the file into infinity. You can make the max execution time 0, but I'd probably do 240 or so. If it dies from execution time, just run it again.

Share this post


Link to post
Share on other sites
ohhhhhh, ok I see what you are saying now. It was almost too easy, I was trying to think too hard about it.

Ok just keep re running the script and everytime it will pass right by the ones I already did and start on new ones. So simple, your awesome, I'll try it out tonight and I don't see any reason why it woun'dt work. thanks.

-chris

Share this post


Link to post
Share on other sites
Michael I know you thought you had gotten rid of me, but not yet...sorry :(

Here it is... what I have no idea...

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 3072 bytes) in /home/httpd/vhosts/bigornot.com/httpdocs/make_th.php on line 57

-Chris

Share this post


Link to post
Share on other sites
Right next to that set_time_limit function (right above or below) add the following:

ini_set("memory_limit","128M");

that _should_ increase the image size suppored to 128mb... I've never gotten a memory error that the memory_limit increase to 128M didn't fix.

EDIT: ALSO -- if you might try imagedestroy() to destroy images once you've finished processing them -- before the loop starts over. destroy them all hahaha!

imagedestory($img_src);
imagedestory($img_dst);

that will free up any system resources (memory) taken up by those images before you try creating more.

Share this post


Link to post
Share on other sites
You are the best man, where do I send the beer to??

As for destroying the images, I want to keep the original image along with the new thumbnail. Does the image destroy delete the actual orignal image or just the memory space used for it?

-Chris

Share this post


Link to post
Share on other sites
it just deletes the GD object from memory. If these images are in the megabytes, then using the imagedestroy() function will just free up memory before trying to load more images into memory.

It's like unsetting the variable -- files on your hard drive aren't affected.

Share this post


Link to post
Share on other sites
Hey Michael, I just wanted to say thank you a million!

Everything is now working perfect! SOLVED!!!!

-Chris

Share this post


Link to post
Share on other sites

×

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.