调整外部图像的大小

时间:2012-03-06 10:41:14

标签: php image-resizing

是否有任何脚本可以调整外部图像的大小并输出它?

例如,我想将所有外部方形图像的大小调整为130X130。

喜欢这个

http://mydomain.com/script/resize.php?url=http://otherdomain.com/image.png

编辑: Facebook也是一个例子 https://s-external.ak.fbcdn.net/safe_image.php?d=AQCYX3NIE5gMyujT&url=http%3A%2F%2Fi2.ytimg.com%2Fvi%2FyoLeJNjIVZk%2Fhqdefault.jpg

任何帮助表示感谢。

谢谢

3 个答案:

答案 0 :(得分:4)

// Content type 
header('Content-Type: image/jpeg');

//get image from internet and save it into local disk
$url = 'http://www.google.com/images/srpr/logo3w.png';
$img = 'google.png';
file_put_contents($img, file_get_contents($url));


//get  current size and set new size
list($width, $height) = getimagesize($img);
$new_width = 130;
$new_height = 130;

// genarate resized image copy
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefrompng($img);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// flush image to browser
imagejpeg($image_p, null, 100);
// save resised image to disk 
imagejpeg($image_p, "newimage.jpg",100);

仅当您想要调整大小并保存在您的应用程序上时才使用此代码,否则上面的示例就足够了。

答案 1 :(得分:1)

看看this thread。您可以编写自己的代码来调整图像大小或使用TimThumb脚本。

答案 2 :(得分:1)

如果你不想保存它们,你可以用html或css设置宽度和高度

<img height="130px" with="130px;" src="somesite.com/img.jpg"></img>
相关问题