1

更新:コードを簡略化しました(試してみました)

配列に設定された一連の画像をダウンロードしようとしていますが、明らかに何かが正しくありません。

function savePhoto($remoteImage,$fname) {
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_NOBODY, true);
    curl_setopt ($ch, CURLOPT_URL, $remoteImage);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    $fileContents = curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if($retcode==200) {
        $newImg = imagecreatefromstring($fileContents);
        imagejpeg($newImg, ".{$fname}.jpg",100);
    }
    return $retcode;
}

$filesToGet = array('009');
$filesToPrint = array();

foreach ($filesToGet as $file) {
        if(savePhoto('http://pimpin.dk/jpeg/'.$file.'.jpg',$file)==200) {
            $size = getimagesize(".".$file.".jpg");
            echo $size[0] . " * " . $size[1] . "<br />";
        }
}

次のエラーが発生します。

警告:imagecreatefromstring()[function.imagecreatefromstring]:15行目のC:\ inetpub \ vhosts \ dehold.net \ httpdocs \ ripdw\index.phpの空の文字列または無効な画像

警告:imagejpeg():指定された引数は16行目のC:\ inetpub \ vhosts \ dehold.net \ httpdocs \ ripdw\index.phpの有効な画像リソースではありません

警告:getimagesize(.009.jpg)[function.getimagesize]:ストリームを開くことができませんでした:26行目のC:\ inetpub \ vhosts \ dehold.net \ httpdocs \ ripdw\index.phpにそのようなファイルまたはディレクトリはありません*

4

3 に答える 3

0

代わりにこれを試してください:

function get_file1($file, $local_path, $newfilename)
{
    $err_msg = '';
    echo "<br>Attempting message download for $file<br>";
    $out = fopen($newfilename, 'wb');
    if ($out == FALSE){
      print "File not opened<br>";
      exit;
    }

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_FILE, $out);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $file);

    curl_exec($ch);
    echo "<br>Error is : ".curl_error ( $ch);

    curl_close($ch);
    //fclose($handle);

}//end function 

//取得元:http ://www.weberdev.com/get_example-4009.html

またはfile_get_contents

于 2010-07-08T12:59:03.843 に答える
0

CURLの代わりにfile_get_contentsを試してみてください(より単純ですが、それでうまくいきます):

 function savePhoto($remoteImage,$fname) {        
      $fileContents = file_get_contents($remoteImage);        
      try {        
        $newImg = imagecreatefromstring($fileContents);
        imagejpeg($newImg, ".{$fname}.jpg",100);        
      } catch (Exception $e) {        
        //what to do if the url is invalid        
      } 
}
于 2010-07-08T18:40:41.690 に答える
0

皆さんの助けを借りて、少し詮索して、ようやく動作するようになりました:-)

私はCURLを使用することになりました:

function savePhoto($remoteImage,$fname) {
    $ch = curl_init();

    curl_setopt ($ch, CURLOPT_URL, $remoteImage);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    $fileContents = curl_exec($ch);

    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);
    if($retcode == 200) {
        $newImg = imagecreatefromstring($fileContents);
        imagejpeg($newImg, $fname.".jpg",100);
    }

    return $retcode;
}


$website = "http://www.pimpin.dk/jpeg";
$filesToGet = array('009');
$filesToPrint = array();

foreach ($filesToGet as $file) {
        if(savePhoto("$website/$file.jpg",$file)==200) {
            $size = getimagesize($file.".jpg");
            echo $size[0] . " * " . $size[1] . "<br />";
        } else {
            echo "File wasn't found";
        }
}
于 2010-07-09T09:25:59.273 に答える