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 に答える