Jump to content

Image copy, resize, rename fromURl not working


Go to solution Solved by StefanRSA,

Recommended Posts

Hi,

 

I am getting an image URL from an xml feed. I am trying to copy the image from the URL in the XML Feed, then rename & resize it.

Its only giving me a black resized block for each image resize... This is driving me nuts.

 

Please look at my script and tell me where am I going wrong?

set_time_limit(-1);
$imgurl='http://path/to/img_main.jpg';
$pic=$imgurl;
$img = $pic;

//////// GET IMAGE NAME
$url_arr = explode ('/', $pic);
$ct = count($url_arr);
$name = $url_arr[$ct-1];
$name_div = explode('.', $name);
$ct_dot = count($name_div);
$img_type = $name_div[$ct_dot -1];
/////// GET IMAGE NAME END

$name= str_replace(' ', '_', $name);
$im = imagecreatefromjpeg($img);
$width = imagesx($im);
$height = imagesy($im);

$newwidth = '400';
$newheight = '240';
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,'../ad_images/'.$name); //save image as jpg
echo '<img src="../ad_images/'.$name.'"> ';

$newwidth1 = '130';
$newheight1 = '98';
$thumb1 = imagecreatetruecolor($newwidth1, $newheight1);
imagecopyresized($thumb1, $im, 0, 0, 0, 0, $newwidth1, $newheight1, $width, $height);
imagejpeg($thumb1,'../ad_images/130/'.$name); //save image as jpg
echo '<img src="../ad_images/130/'.$name.'"> ';


$newwidth2 = '65';
$newheight2 = '49';
$thumb2 = imagecreatetruecolor($newwidth2, $newheight2);
imagecopyresized($thumb2, $im, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);
imagejpeg($thumb2,'../ad_images/65/'.$name); //save image as jpg
echo '<img src="../ad_images/65/'.$name.'"> <br>';

$dbname=$name;
imagedestroy($thumb);
imagedestroy($thumb1);
imagedestroy($thumb2); 
imagedestroy($im);
  • Solution
//$name='newpicfile';
$url=$imgurl;
// create the context
set_time_limit(-1);
$img = file_get_contents($url);
//////// GET IMAGE NAME

$url_arr = explode ('/', $url);
$ct = count($url_arr);
$name = $url_arr[$ct-1];
$name_div = explode('.', $name);
$ct_dot = count($name_div);
$img_type = $name_div[$ct_dot -1];
$name=$adnr.'_'.$inr.'_'.$name;
/////// GET IMAGE NAME END

$im = imagecreatefromstring($img);
$width = imagesx($im);
$height = imagesy($im);
$newwidth = '400';
$newheight = '240';
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,'../ad_images/'.$name); //save image as jpg
//echo '<img src="../ad_images/'.$name.'"> ';

$newwidth1 = '130';
$newheight1 = '75';
$thumb1 = imagecreatetruecolor($newwidth1, $newheight1);
imagecopyresized($thumb1, $im, 0, 0, 0, 0, $newwidth1, $newheight1, $width, $height);
imagejpeg($thumb1,'../ad_images/130/'.$name); //save image as jpg
//echo '<img src="../ad_images/130/'.$name.'"> ';

$newwidth2 = '65';
$newheight2 = '37';
$thumb2 = imagecreatetruecolor($newwidth2, $newheight2);
imagecopyresized($thumb2, $im, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);
imagejpeg($thumb2,'../ad_images/65/'.$name); //save image as jpg
echo '<img src="../ad_images/65/'.$name.'"> <br>';

$dbname=$name;
  $pquery = "INSERT INTO *****";
mysql_query($pquery);  
imagedestroy($thumb);
imagedestroy($thumb1);
imagedestroy($thumb2);

imagedestroy($im);

Am sure this will help somewhere, someone else... ;-) Thanks for the reply mac

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.