download image from remote source and resize then save

Ivar picture Ivar · Jan 26, 2012 · Viewed 11.9k times · Source

Do any of you know of a good php class I can use to download an image from a remote source, re-size it to 120x120 and save it with a file name of my choosing?

So basically I would have an image at "http://www.site.com/image.jpg" save to my web server "/images/myChosenName.jpg" as a 120x120 pixels.

Thanks

Answer

Zulkhaery Basrul picture Zulkhaery Basrul · Jan 26, 2012

You can try this:

<?php    
$img = file_get_contents('http://www.site.com/image.jpg');

$im = imagecreatefromstring($img);

$width = imagesx($im);

$height = imagesy($im);

$newwidth = '120';

$newheight = '120';

$thumb = imagecreatetruecolor($newwidth, $newheight);

imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($thumb,'/images/myChosenName.jpg'); //save image as jpg

imagedestroy($thumb); 

imagedestroy($im);
?>


More information about PHP image function : http://www.php.net/manual/en/ref.image.php