得到了一个php脚本,可以从url下载很多图片,我可以让脚本工作得更快吗?

得到了一个php脚本,可以从url下载很多图片,我可以让脚本工作得更快吗?,php,Php,首先,我创建一个包含图像的url数组(1个url=1个图像,该url中没有其他内容): } 此脚本的问题是—需要约5分钟才能完全完成。我想知道我是否可以让它运行得更快?而不是在每一个上都检查GetImageSize,因为这可能需要不必要的时间,不如只检查一次(当您去获取它时): 根据@GeraldSchneider的评论。。。文件内容(…)是必要的吗?你在刮xkcd.com吗?不应该$image\u endings[]=“${x}n${y}w.png”be$image_endings[]=“{$

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

}


此脚本的问题是—需要约5分钟才能完全完成。我想知道我是否可以让它运行得更快?

而不是在每一个上都检查
GetImageSize
,因为这可能需要不必要的时间,不如只检查一次(当您去获取它时):


根据@GeraldSchneider的评论。。。
文件内容(…)
是必要的吗?

你在刮xkcd.com吗?不应该
$image\u endings[]=“${x}n${y}w.png”
be
$image_endings[]=“{$x}n{$y}w.png”?也就是说,您没有正确地将变量名括在大括号中。如果您从远程URL拍摄图像,并使用嵌套循环25 x 48,则肯定会花费很多时间…@John Conde,我不知道刮取是什么意思。@Sepster-hmm它现在是如何工作的,我会尝试更改它,但我认为不会。我猜他想保存已下载图像的大小调整版本。我将用
imagecreatefromstring($img)
替换
imagecreatefromstring()
,并删除
文件\u put\u contents()
调用。“这样,它只被写入文件一次。”杰拉尔德·施奈德同意;他应该使用他已经拥有的内存流,而不是创建然后读取文件流。虽然internet I/O显然将成为这里的瓶颈,但这种(潜在的)不必要的磁盘I/O肯定不会有帮助。
$image_endings = array();
for($x=1;$x<=25;$x++) {
    for($y=1;$y<=48;$y++) {
        $image_endings[] ="${x}n${y}w.png";
    }
}
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 ";
...

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);
...