2013-04-14 36 views
2

比方说,我有一个黑白200x100形象,我把它调整为100x50:扩展图像画布GD或Imagick

// imagick 
$imagick->resizeImage($width, $height, Imagick::FILTER_UNDEFINED, 1) 

// gd 
$image = imagecreatetruecolor($width, $height); 
imagecopyresampled($image, $src, 0, 0, 0, 0, $width, $height, imagesx($src), imagesy($src))) 

但我想要的形象为120x120。如何将画布扩展到这个尺寸,但是保留那个图像,我只是在相同的尺寸中调整大小?类似于Image-> Canvas Size in Photoshop

回答

3
// make the canvas, fill it with $color 
$canvas = imagecreatetruecolor(120, 120); 
imagefilledrectangle($canvas,0,0,120,120,$color); 

// get the image from file... 
list($width, $height) = getimagesize('myimage.jpg'); 
$img = getimagefromjpg('myimage.jpg'); 

// resample image and place it in center of canvas 
$x = intval(($width - 100)/2); 
$y = intval(($height - 50)/2); 
imagecopyresampled($canvas, $img, $x, $y, 0, 0, 100, 50, $width, $height); 

// output etc. ...