user2987790
user2987790

Reputation: 145

GD Thumbnail Creation - Is it possible tu run a script by short stages?

I'm using this modified script (http://webcheatsheet.com/php/create_thumbnail_images.php) for creating thumbnails for all images in one step:

function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth, $imageQuality )
{
$dir = opendir( $pathToImages );

while (false !== ($fname = readdir( $dir ))) {

$info = pathinfo($pathToImages . $fname);
if ( strtolower($info['extension']) == 'jpg' ){

$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );

$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );

$tmp_img = imagecreatetruecolor( $new_width, $new_height );
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}",$imageQuality )}}
closedir( $dir );}

I have to create over 250 thumbnails but i cant because of limited execution time. If possible I use set_time_limit()

if (function_exists('set_time_limit') == true AND @ini_get('safe_mode') == 0){
@set_time_limit(9000);
}

But if there is no possibility to modify ini settings - is there a way to let the script convert images in steps like: create 10 thumbs > header('Location: ./?function=createThumbs') > create next 10 thumbs .. until all thumbs are created. Thank You in advance for any help!

Upvotes: 0

Views: 66

Answers (2)

v2solutions.com
v2solutions.com

Reputation: 1439

In order to execute the script for longer duration, you can do set_time_limit(0) which means there will be no limit to the script execution time. But just setting it to zero will not work because browser will time you out if it doesn't see any output for a particular duration.

One of the ways to avoid this is to output something after may be each thumbnail generation by using Output Buffering functions in PHP like obstart() and flush().

Another alternative (which I would strongly recommend for such a high time consuming job) is to use process control tool like Gearman which will allow you to bail out from the script and process the request separately. The advantage of this is that, the end user will be free to move to another sections of your website while you are creating the thumbnails. Gearman provides you a way to put your jobs in background so that the control will return to your PHP script and once the job is done, you can shoot an email to the user or show some notification by using AJAX pull mechanism on your site.

Upvotes: 3

mister martin
mister martin

Reputation: 6252

Due to the web being stateless, you can't tell a PHP script to stop executing part way through a process and then have it pick up again where it left off, at least not explicitly.

What you could do, however, is write some AJAX code that utilizes setTimeout or setInterval, or you could setup a CRON job that repeatedly runs at set intervals. Then you could pass your script the data whether from an array or from a database, keep track of the last ID it worked with as it moves along, and continue until there is no data left.

With the AJAX approach, the browser would have to remain open throughout the entire process otherwise there is no guarantee it will complete, and it relies on JavaScript being enabled. So if you need to make sure it completes, CRON would probably be a better choice.

Upvotes: 1

Related Questions