GD image_png slow


GD image_png slow

我正在调整多个图像的大小,有时image_png需要13秒才能返回图像,而大多数图像需要0-1秒才能返回。宽高比为320/480的图像在"0秒内"调整为320x480,而在10-13秒内调整为750x1334和1242x2280。

速度不在imagecopyrize(0秒)上,而仅在imagepng函数上。

代码

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;
    }

大图像的高压缩需要时间。你对此无能为力。

尝试通过更改来降低压缩级别

imagepng($thumbnail_gd_image, $thumbnail_image_path, 9);

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

我知道,答案已经被接受了。它可能对某人有用。一些数据,思考图像质量如何影响imagepng()函数的性能。以下数据是使用3.1mb大小的图像进行运行实验的结果。

压缩参数的名称"quality"相当误导,因为png压缩总是无损的。速度之间的权衡和文件大小,它不会影响质量。

imagepng()  

图像质量(值)持续时间(秒)

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