Mar*_*cin 6 php gd image-processing
我有一个大小为88x31的图像,并希望在没有调整实际图像尺寸的情况下将其设为100x100,而只调整其画布/框架,可以将其复制到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, $old, $oldh);
Run Code Online (Sandbox Code Playgroud)