2012-01-06 63 views
6

我有一个大小为88x31的图像,并希望将其设置为100x100而不调整实际图像的大小,但仅调整其画布/框架,可能是将其复制到中心100x100大小的新空白图像。任何想法如何做到这一点?PHP GD - 调整大小的图像帧/画布,但不是实际图像

+0

不要回头重新发明轮子,使用现有的代码:https://github.com/avalanche123/Imagine,但如果你坚持,那么已经回答了很多类似的问题。 – 2012-01-06 10:30:41

+1

[创建包含其他图像的GD的图片]的可能的重复(http://stackoverflow.com/questions/2933170/create-a-picture-with-gd-containing-other-images) – 2012-01-06 10:37:20

+0

Imagine的问题是命名空间支持,我的PHP目前还没有,我不想升级,但感谢。 – Marcin 2012-01-06 10:47:19

回答

7

正确的方法是创建一个新的图像,然后将旧的图像复制到它的中间(假设起始图像是JPEG且小于100×100):

$oldimage = imagecreatefromjpeg($filename); 
$oldw = imagesx($oldimage); 
$oldh = imagesy($oldimage); 

$newimage = imagecreatetruecolor(100, 100); // Creates a black image 

// Fill it with white (optional) 
$white = imagecolorallocate($newimage, 255, 255, 255); 
imagefill($newimage, 0, 0, $white); 

imagecopy($newimage, $oldimage, (100-$oldw)/2, (100-$oldh)/2, 0, 0, $old, $oldh);