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

我终于让它工作了,在你们所有人的帮助下和一些窥探:-)

我最终使用了 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 回答
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

您应该尝试使用 file_get_contents 代替 CURL(更简单,但可以完成工作):

 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 回答