首先我创建一个包含图像的网址数组(1个url = 1个图像,该网址中没有其他内容):
$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 ";
}
这个脚本的问题 - 需要大约5分钟才能完成。我想知道我是否可以让它跑得更快?
答案 0 :(得分: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(...)
是否必要?