I use this script to upload and resize a remote image. Something goes wrong in the area of resizing. What is it?
<?php
$img[]='http://i.indiafm.com/stills/celebrities/sada/thumb1.jpg';
$img[]='http://i.indiafm.com/stills/celebrities/sada/thumb5.jpg';
foreach($img as $i){
save_image($i);
if(getimagesize(basename($i))){
echo '<h3 style="color: green;">Image ' . basename($i) . ' Downloaded OK</h3>';
}else{
echo '<h3 style="color: red;">Image ' . basename($i) . ' Download Failed</h3>';
}
}
function save_image($img,$fullpath='basename'){
if($fullpath=='basename'){
$fullpath = basename($img);
}
$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);
$im = imagecreatefromstring($rawdata);
$x=300;
$y=250;
$im2 = imagecreatetruecolor($x,$y);
imagecopyresized($im2,$im,0,0,0,0,$x,$y,imagesx($im),imagesy($im));
imagecopyresampled($im2,$im,0,0,0,0,$x,$y,imagesx($im),imagesy($im));
imagedestroy($im);
if(file_exists($fullpath)){
unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $im2);
fclose($fp);
imagedestroy($im2);
}
?>
source
share