user1122069
user1122069

Reputation: 1807

GD image_png slow

I am resizing several images, and sometimes image_png is taking 13 seconds to return the images, while most return in 0-1 seconds. An image with aspect ratio 320/480 is resized into 320x480 in "0 seconds" while into 750x1334 and 1242x2280 in 10-13 seconds.

The slownesss is not on imagecopyresized (0 seconds), but only with the imagepng function.

Code

function generate_image_thumbnail($source_image_path, $thumbnail_image_path,$new_width,$new_height,$opts = Array())
    {
        $remove_transparency = isset($opts['remove_transparency']) ? $opts['remove_transparency']:false;
        list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);
        switch ($source_image_type) {
            case IMAGETYPE_GIF:
                $source_gd_image = imagecreatefromgif($source_image_path);
                break;
            case IMAGETYPE_JPEG:
                $source_gd_image = imagecreatefromjpeg($source_image_path);
                break;
            case IMAGETYPE_PNG:
                $source_gd_image = imagecreatefrompng($source_image_path);
                break;
        }
        if ($source_gd_image === false) {
            echo "Image Failure: $source_image_path\n";
            echo "Image type: ".$source_image_type."\n";
            exit();
        }
        $source_aspect_ratio = $source_image_width / $source_image_height;
        $thumbnail_aspect_ratio = $new_width / $new_height;
        if ($source_image_width <= $new_width && $source_image_height <= $new_height) {
            $thumbnail_image_width = $source_image_width;
            $thumbnail_image_height = $source_image_height;
        } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
            $thumbnail_image_width = (int) ($new_height * $source_aspect_ratio);
            $thumbnail_image_height = $new_height;
        } else {
            $thumbnail_image_width = $new_width;
            $thumbnail_image_height = (int) ($new_width / $source_aspect_ratio);
        }
        $thumbnail_image_height = $new_height;
        $thumbnail_image_width = $new_width;
        $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
        $time = time();
        imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);
        $time = time() - $time;
        echo "Resample Time: $time\n";

        $time = time();
        $result = imagepng($thumbnail_gd_image, $thumbnail_image_path, 9);
        $time = time() - $time;
        echo "Image Time: $time\n";

        imagedestroy($source_gd_image);
        imagedestroy($thumbnail_gd_image);

        if (!file_exists($thumbnail_image_path))
        {
            var_dump($thumbnail_gd_image);
        }
        return true;
    }

Upvotes: 2

Views: 1520

Answers (2)

forethought
forethought

Reputation: 3253

I know, the answer is already accepted. It might be useful for someone. Some data, to think about how image quality affects performance of imagepng() funtion. The below data is result of running experiements with the image of 3.1mb size.

The name "quality" for the compression parameter is quite misleading, as png compression is always lossless. The trade off is between speed and filesize, it cannot affect quality.

imagepng()  

ImageQuality(value) Duration(sec)

1 ---> 0.458

2 ---> 0.474

3 ---> 0.618

4 ---> 0.624

5 ---> 0.868

6 ---> 1.539

7 ---> 2.274

8 ---> 7.748

9 ---> 13

Upvotes: 2

blurfus
blurfus

Reputation: 14031

High compression on a large image takes time. There is little you can do about it.

Try lower the compression level by changing:

imagepng($thumbnail_gd_image, $thumbnail_image_path, 9);

to

imagepng($thumbnail_gd_image, $thumbnail_image_path, 8); //or another lower value

Upvotes: 2

Related Questions