PHP 生成图像需要太长时间


php it takes too long to generate an image

我有这个代码

$filename = 'http://4.bp.blogspot.com/-Da3HqoaO2yU/T62i43Bgm5I/AAAAAAAARzI/B4Ggaq_cUJc/s400/dotsgreendistressedBackgroundFairy2.jpg';   

        // Get new dimensions
        list($width, $height) = getimagesize($filename);
        $new_width = $width * $percent;
        $new_height = $height * $percent;
        // Resample
        $image_p = imagecreatetruecolor(400, 200);
        $image = imagecreatefromjpeg($filename);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, 400, 200, $width, $height);

        // texts
        $width = 400;
        $height = 150;
        $im = imagecreatefromjpeg($filename);
        $x = imagesx($image_p) - $width ;
        $y = imagesy($image_p) - $height;
        $backgroundColor = imagecolorallocate ($image_p, 255, 255, 255);
        $textColor = imagecolorallocate ($image_p, 0, 0,0);
        imagestring ($image_p, 45, 30, 10, 'a ', $textColor);
        imagestring ($image_p, 45, 30, 30, 'b ', $textColor);
        imagestring ($image_p, 45, 30, 50, 'c', $textColor);
        imagestring ($image_p, 45, 30, 70, 'd', $textColor);
        imagestring ($image_p, 45, 30, 90, 'e', $textColor);
        imagestring ($image_p, 45, 30, 110, 'f', $textColor);

        // Output
        imagejpeg($image_p, null, 100);
        header('Content-type: image/jpeg');

这是非常简单的图像,但生成大约需要 5 秒,哪里会有问题?

使用 php 生成图像不是一个快速的过程。您应该缓存该图像,以便只需生成一次。预先存在的图像加载速度要快得多。如果可能,请在不需要用户等待图像时生成映像。