GD image_png 慢
GD image_png slow
我正在调整几张图片的大小,有时 image_png 需要 13 秒才能 return 图片,而大多数 return 需要 0-1 秒。宽高比为 320/480 的图像在“0 秒”内调整为 320x480,在 10-13 秒内调整为 750x1334 和 1242x2280。
慢的不是imagecopyresized(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 大小的图像进行实验的结果。
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()
图像质量(值)持续时间(秒)
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
我正在调整几张图片的大小,有时 image_png 需要 13 秒才能 return 图片,而大多数 return 需要 0-1 秒。宽高比为 320/480 的图像在“0 秒”内调整为 320x480,在 10-13 秒内调整为 750x1334 和 1242x2280。
慢的不是imagecopyresized(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 大小的图像进行实验的结果。
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()
图像质量(值)持续时间(秒)
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