tinify/tinify-php

Resize same image more than once

celsomtrindade opened this issue · 3 comments

I'm trying to upload an image and create 3 variations of the same image. One is the main image, a medium thumbnail and a smaller thumbnail. But when I'm trying to generate the second and third image, usually I get the error:

Maximum execution time of 30 seconds exceeded

This is the code I'm trying to use:

$source = \Tinify\fromFile($filePath);

$resize = $source->resize(array(
    "method" => "cover",
    "width"  => 1920,
    "height" => 1080
));
$resize->toFile($savePath);

$thumbM = $source->resize(array(
    "method" => "fit",
    "width"  => 400,
    "height" => 350
));
$thumbM->toFile($saveThumbM);

$thumbS = $source->resize(array(
    "method" => "fit",
    "width"  => 80,
    "height" => 80
));
$thumbS->toFile($saveThumbS);

The only difference from each of the images is the name and size. Is there a way to achieve this result?

Thanks for reporting! Your code looks fine. PHP has a maximum execution time by default. That's probably what's limiting the execution time.

You can disable or override the default of 30s with set_time_limit(0) (See http://php.net/manual/en/function.set-time-limit.php)

If that doesn't work for you, feel free to reopen the issue.

@rolftimmermans I was thinking about doing this, but is there a wayto do it with the tinify lib? Because currently each time I call the resize it's sending the file to be compressed before resizing, so if I want to have 3 different images resolutions, it will compress each one of them, right? I was wondering if it's possible to just resize after having a single source.

I know there is other process in php to achieve this, but if it could be done all the process here, it would be better.

There's no built-in support to resume execution halfway. You could try serializing and unserializing (http://php.net/manual/en/language.oop5.serialization.php) but we make no guarantees that this works. Not yet at least. If this is a feasible solution we might consider adding unit tests for it.

Also note that individual compressions could take more than 30 seconds (especially on very large images), so you might have to disable or increase the timeout anyway!