我的图片 150x150px 。我需要通过增加源图像周围的透明区域来显示 800x350px 图像,指示新图像中的坐标: 50px 来自顶部, 50px 来自左
我试过了:
// the needed sizes of image
$imgWidth = 800;
$imgHeight = 441;
// currrent sizes of PNG image
$wmSize = getimagesize("wm2.png");
$wmWidth = $wmSize[0]; // 724
$wmHeight = $wmSize[1]; // 144
// current PNG image
$wm = imagecreatefrompng("wm2.png");
imagesavealpha($wm, true);
imagealphablending($wm, true);
// new empty image
$new_empty_image = imagecreate($imgWidth, $imgHeight);
// opacity 0
$transparent = imagecolorallocatealpha($new_empty_image, 255, 255, 255, 127);
imagefilledrectangle($new_empty_image, 0, 0, $imgWidth, $imgHeight, $transparent);
imagecopy($new_empty_image, $wm, $imgWidth, $imgHeight, 0, 0, $wmWidth, $wmHeight);
imagepng($new_empty_image, 'new.png');
但是我只得到没有源PNG图像的空图像:
所以,最后一个工作版本是:
$width = 800;
$height = 441;
// PNG image
list($wm_width, $wm_height) = getimagesize('wm.png');
$wm = imagecreatefrompng('wm.png');
// new empty image
$new_empty_image = imagecreatetruecolor($width, $height);
$transparent = imagecolorallocatealpha($new_empty_image, 255, 255, 255, 127);
imagefill($new_empty_image, 0, 0, $transparent);
// Put wm on top of new image.
imagecopy($new_empty_image, $wm, 50, 150, 0, 0, $wm_width, $wm_height);
imagealphablending($new_empty_image, false);
imagesavealpha($new_empty_image, true);
imagepng($new_empty_image, 'new.png');
答案 0 :(得分:0)
这是一个功能齐全且经过测试的例子:
$width = 800;
$height = 441;
// PNG image
list($wm_width, $wm_height) = getimagesize('wm.png');
$wm = imagecreatefrompng('wm.png');
// new empty image
$new_empty_image = imagecreatetruecolor($width, $height);
$transparent = imagecolorallocatealpha($new_empty_image, 255, 255, 255, 127);
imagefill($new_empty_image, 0, 0, $transparent);
// Put wm on top of new image.
imagecopy($new_empty_image, $wm, 50, 50, 0, 0, $wm_width, $wm_height);
// Optional??? This code seems to be needed in some system and not in others.
imagealphablending($new_empty_image, false);
imagesavealpha($new_empty_image, true)
// ?? Optional
imagepng($new_empty_image, 'new.png');