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中的图像->画布大小之类的东西

4

1 回答 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. ...
于 2013-04-14T11:53:36.443 回答