php生成图像需要很长时间

时间:2013-11-16 13:37:32

标签: php

我有这段代码

$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秒钟,哪里可能有问题?

1 个答案:

答案 0 :(得分:0)

使用php生成图像并不是一个快速的过程。您应该缓存该图像,这样您只需要生成一次。预先存在的图像将加载MUCH更快。如果可能,在您不要求用户等待时生成您的图像。