2011-03-09 131 views
1

如何在GD中创建图像/缩略图的固定大小(高度/宽度)?固定大小的缩略图像twitpic.com

我知道有很多PHP脚本,但只是缩放它,高度/宽度将始终是不同的大小。

我喜欢像twitpic.com和Facebook缩略图

+1

可能重复[?ImageMagick的或GD Libary的图像大小调整和创建缩略图(http://stackoverflow.com/questions/4277823/imagemagick-or- gd-libary-for-image-resizing-and-thumbnail-creation) – seriousdev 2011-03-09 23:35:21

+0

[将全景图像调整为固定大小]的可能重复(http://stackoverflow.com/questions/5157998/resize-panoramic-image-to-fixed - 大小) – CanSpice 2011-03-09 23:35:40

回答

0

你需要使用getimagesize

得到高度和图像的宽度,然后用imagecopyresized

调整它的其余部分是所有使用GD完成与加载和保存图像相同的基本工作。

这里有一个基本的例子,如果你想考虑高度/宽度比,那么你必须做一些额外的数学。

<?php 
header("Content-type: image/png"); 

$size = getimagesize($filename); 
$image  = imagecreatefrompng($filename); 
$thumbnail = imagecreate(100,100); 
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, 100, 100, $size[0], $size[1]); 
imagepng($thumbnail); 
imagedestroy($image); 
imagedestroy($thumbnail); 
+0

谢谢,但我知道这一点。当它调整到固定的高度和宽度时,图像看起来很糟糕。需要一种方法来裁剪特定区域,然后调整其大小。就像twitpic.com – user622378 2011-03-09 23:54:04

+0

如果你想它看起来成比例,那么你必须从图像的一部分具有相同的高/宽比例复制。例如要从200x300图像制作100x100的缩略图,您需要获取200x200的图像。你是这个意思吗? – Jacob 2011-03-10 01:41:19

0

这很容易与Thumbnailer

$th=new Thumbnailer("your-photo.jpg"); 
$th->thumbSquare(100)->save("thumb.jpg");