Jump to content

curl imae save


dsjoes

Recommended Posts

this is the script it is supposed to get the image url and file name from the address bar and then save the image in the pics folder. it doesn't work and there are no errors, i have never used curl before so i don't know what i am doing.

the $img is commented out because i don't know how to add it into the code.

<?php
$url = $_GET["url"];
//$img = $_GET["fileName"];
$fullpath = "/hermes/bosweb/web230/b2302/ipg.account/test_server/pics";

function save_image($url,$fullpath){
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($fullpath)){
        unlink($fullpath);
    }
    $fp = fopen($fullpath,'x');
    fwrite($fp, $rawdata);
    fclose($fp);
}
?>

 

this is was the end of the url looks like

save.php?url=http%3A%2F%2Fapi10.webresizer.com%2Fresizer%2Fdisplay.cgi%3Fsession%3DLO7WC4zoiw1z4pLFMIjK1dps3Q9HKP%26from%3Dresult.jpg%26type%3Djpg&filetype=jpg&filename=Group.jpg&filesize=39.07&width=385&height=289

Link to comment
Share on other sites

Step 1. TEST the url in your browser see if it displays the image (GIGO)

Step 2. You coding is essentially correct.

Copy this to a file then run it...

<?PHP
/* define the function */
function save_image($img,$fullpath){ 
/* check to see if images are of same type - ie gif-gif, png-png, jpg-jpg */
$i_type1_a = explode(".", $img);
$c1 = count($i_type1_a) - 1;
$i_type1 = $i_type1_a[$c1];

$i_type2_a = explode(".", $fullpath);
$c2 = count($i_type2_a) - 1;
$i_type2 = $i_type2_a[$c2];
if($i_type1 != $i_type2) {
	echo "Image types do NOT match";
	exit();
	return;
}
$ch = curl_init ($img); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); 
$rawdata=curl_exec($ch); 
curl_close ($ch); 
if(file_exists($fullpath)){ 
	unlink($fullpath); 
} 
$fp = fopen($fullpath,'x'); 
fwrite($fp, $rawdata); 
fclose($fp); 
} 

/* set the variables */
$img = "http://nstoia.com/shared_files/logo001.jpg"; /* URL TO THE IMAGE BEING DOWNLOADED */
$fullpath = "myimage.jpg"; /* PATH AND NAME FOR SAVED FILE */

/* call the function */
save_image($img,$fullpath);

/* test that the image has been succesfully saved */
?>
<img src="<?PHP echo $fullpath; ?>" alt="">
<?PHP
?>

Link to comment
Share on other sites

ignore the first post

 

i have got this one to work but it saves the image as $link with no extension so when i rename it to link.jpg it loads the picture. so anyone know how to fix this and do it so that it uses the filename from $img.

<?php
$link = $_GET["url"];
//$img = $_GET["fileName"]; //this bit has the extension aswell

function GetImageFromUrl($link)

{

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 0);

curl_setopt($ch,CURLOPT_URL,$link);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result=curl_exec($ch);

curl_close($ch);

return $result;

}
$sourcecode=GetImageFromUrl("$link");
$savefile = fopen('$link', 'w');
fwrite($savefile, $sourcecode);
fclose($savefile);
?>

 

Thanks ;)

Link to comment
Share on other sites

Step 1. TEST the url in your browser see if it displays the image (GIGO)

Step 2. You coding is essentially correct.

Copy this to a file then run it...

<?PHP
/* define the function */
function save_image($img,$fullpath){ 
/* check to see if images are of same type - ie gif-gif, png-png, jpg-jpg */
$i_type1_a = explode(".", $img);
$c1 = count($i_type1_a) - 1;
$i_type1 = $i_type1_a[$c1];

$i_type2_a = explode(".", $fullpath);
$c2 = count($i_type2_a) - 1;
$i_type2 = $i_type2_a[$c2];
if($i_type1 != $i_type2) {
	echo "Image types do NOT match";
	exit();
	return;
}
$ch = curl_init ($img); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); 
$rawdata=curl_exec($ch); 
curl_close ($ch); 
if(file_exists($fullpath)){ 
	unlink($fullpath); 
} 
$fp = fopen($fullpath,'x'); 
fwrite($fp, $rawdata); 
fclose($fp); 
} 

/* set the variables */
$img = "http://nstoia.com/shared_files/logo001.jpg"; /* URL TO THE IMAGE BEING DOWNLOADED */
$fullpath = "myimage.jpg"; /* PATH AND NAME FOR SAVED FILE */

/* call the function */
save_image($img,$fullpath);

/* test that the image has been succesfully saved */
?>
<img src="<?PHP echo $fullpath; ?>" alt="">
<?PHP
?>

 

thanks i will try that now

Link to comment
Share on other sites

when i try to use it for requested from the url i get Image types do NOT match this is the url 

save.php?url=http%3A%2F%2Fapi10.webresizer.com%2Fresizer%2Fdisplay.cgi%3Fsession%3Ds0ckquMwWs9R1TTaMmS60mfDVDfndmu3%26from%3Dresult.jpg%26type%3Djpg&filetype=jpg&filename=ps_logo2.jpg&filesize=7.84&width=385&height=133

 

and this is the image link part of it it is just the google logo that i used to test it with

http://api10.webresizer.com/resizer/display.cgi?session=s0ckquMwWs9R1TTaMmS60mfDVDfndmu3&from=result.jpg&type=jpg&filetype=jpg

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.