imagepng很慢

时间:2012-08-11 01:35:10

标签: php

我有一个.php文件,它应该加载一个图像以便在img标签中显示(即<img src="the_file.php?which=0"/>)。它看起来像这样:

<?php
    ob_clean();

    header("Content-type: image/png");

    include_once("util.php");

    //Do a simple calculation to get $name from (int)$_GET["which"];

    $im = imagecreatefrompng("protected_directory/".$name.".png");

    imagepng($im,NULL,0,NULL);

    imagedestroy($im);

    ob_end_flush();
?>

它可以正常工作,但图像加载速度比直接加载它要慢得多(即<img src="protected_directory/the_name.png"/>,其中“the_name”的计算方式与PHP文件相同,但我不能这样做是因为protected_directory不是世界可读的。)

我的问题是,为什么突然这么慢?它不是一个大图像,但也不是非常小。

3 个答案:

答案 0 :(得分:3)

如果您只是显示现有文件,请使用readfile()将其输出到浏览器。没有必要为此创建一个可编辑的GD对象的所有开销。

答案 1 :(得分:1)

imagepng is known to be slow,如果您需要使用PHP脚本输出图像,请使用以下代码:

$filename = md5(time() . mk_rand());
imagepng($im, $filename);
echo file_get_contents($filename);

答案 2 :(得分:1)

作为另一个答案,我发现你可以使用第三个参数来压缩图像(PNG使用zlib)。将其设置为9与其他解决方案一样有效。

相关问题