使用GD或Imagick扩展图像画布

3

假设我有一张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。我该如何扩展画布大小,但保持刚刚调整过尺寸的图片在中心位置,并且保持原来的尺寸不变呢?就像Photoshop中的图像->画布大小。

1个回答

4
// 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. ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接