我有一张尺寸为 88x31 的图像,我想将其设为 100x100 而不调整实际图像的大小,而只调整其 Canvas /框架,可能是通过将其复制到 100x100 尺寸的新空白白色图像的中心。有什么想法吗?
最佳答案
正确的方法是创建一个新图像,然后将旧图像复制到它的中间(假设起始图像是 JPEG 且小于 100x100):
$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, $oldw, $oldh);
关于PHP GD - 调整图像框架/ Canvas 的大小而不是实际图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8756408/