0

首先,我制作了一个包含图像的 url 数组(1 个 url = 1 个图像,该 url 中没有其他内容):

$image_endings = array();
for($x=1;$x<=25;$x++) {
    for($y=1;$y<=48;$y++) {
        $image_endings[] ="${x}n${y}w.png";
    }
}

现在我运行每个 url,如果该 url 存在,我下载图像:

foreach ($image_endings as $se){
    $url = 'http://imgs.xkcd.com/clickdrag/'.$se;

if (@GetImageSize($url)) {

//echo  "image exists ";
    $img = file_get_contents($url);
    file_put_contents("tiles/".$se,$img);

    $width = 50;
    $height = 50;
    $filename = 'tiles/'.$se;
    $image = imagecreatefrompng ( $filename );
    $new_image = imagecreatetruecolor ( $width, $height ); // new wigth and height
    imagealphablending($new_image , false);
    imagesavealpha($new_image , true);
    imagecopyresampled ( $new_image, $image, 0, 0, 0, 0, $width, $height, imagesx ( $image ), imagesy ( $image ) );
    $image = $new_image;

    // saving
    imagealphablending($image , false);
    imagesavealpha($image , true);
    imagepng ( $image, $filename );

} else {

// echo  "image does not exist ";

}

这个脚本的问题 - 完全完成需要大约 5 分钟。我想知道我是否可以让它运行得更快?

4

1 回答 1

0

与其检查GetImageSize每一个,因为这可能会花费不必要的时间,不如只检查一次(当你去获取它时):

...

foreach ($image_endings as $se){
    $url = 'http://imgs.xkcd.com/clickdrag/'.$se;

/* Don't do this... time consuming...
if (@GetImageSize($url)) {
echo  "image exists ";
*/


$img = file_get_contents($url);

// Check here if it existed.
if ($img !== false) {

    file_put_contents("tiles/".$se,$img);
...

根据@GeraldSchneider 的评论……有file_put_contents(...)必要吗?

于 2013-03-28T13:04:11.340 回答