使用GD或Imagick扩展图像画布

时间:2013-04-14 11:04:26

标签: php gd image-manipulation imagick

假设我有200x100的图像,我将其调整为100x50:

// 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-> Photoshop中的画布大小

1 个答案:

答案 0 :(得分: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. ...