Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

download image from remote source and resize then save

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

like image 937
Ivar Avatar asked Jan 26 '12 17:01

Ivar


Video Answer


1 Answers

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

like image 106
Zul Avatar answered Oct 14 '22 21:10

Zul