views:

46

answers:

3

UPDATED: I've simplified the code (tried to)

I'm trying to download a series of images as set in an array, but something is clearly not right:

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 />";
        }
}

I get the following errors:

Warning: imagecreatefromstring() [function.imagecreatefromstring]: Empty string or invalid image in C:\inetpub\vhosts\dehold.net\httpdocs\ripdw\index.php on line 15

Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\inetpub\vhosts\dehold.net\httpdocs\ripdw\index.php on line 16

Warning: getimagesize(.009.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\inetpub\vhosts\dehold.net\httpdocs\ripdw\index.php on line 26 *

A: 

try this instead:

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 

// taken from: http://www.weberdev.com/get_example-4009.html

or file_get_contents

Purefan
I tried your suggestion but it didn't save the file as a JPEG, and I would really like to get my piece of code to work (and hopefully understand what I'm doing wrong) :-)Thank you for your input
KasperFP
A: 

You should try file_get_contents in replacement of CURL (simpler but it does the job):

 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        
      } 
}
bPizzi
That did help with saving the file, but it returns an error if the file doesn't exist and a 404 is returned:...Warning: file_get_contents(http://pimpin.dk/jpeg/00x9.jpg) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\inetpub\vhosts\dehold.net\httpdocs\ripdw\index.php on line 22...Is it possible to prevent it?
KasperFP
Yeah, put your $fileContents = file_get_contents($remoteImage); in the try.If it fails (in case of a 404) then your catch instructions will be processed without throwing a Warning (unless you DO throw an exception in your catch, obviously).
bPizzi
A: 

I finally got it to work, with help from you all and a bit of snooping around :-)

I ended up using 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";
        }
}
KasperFP