PHP GD 库图像叠加图像
PHP GD Library Image on Image
我正在编写一些代码,允许用户为公司生成模拟礼品卡。他们输入面额、名称并上传徽标,它将使用基本模板图像并将该信息放在上面。
直到徽标部分,我的一切都按需要工作了。
我使用 imagecreatefrompng("template.png")
创建了我的模板图像,并使用 imagettftext()
将文本放置在所需的坐标中。
现在,说到徽标,我 运行 遇到了一些问题。
我尝试先创建基本图像(模板 + 文本),然后使用 imagecopy
将徽标添加到新图像,但我每次似乎得到的只是基本图像,我永远看不到徽标。
我已经尝试对图像的路径进行硬编码,但我仍然没有得到想要的结果。
// If we have all of the post data
if($denomination && $name && $pin && $defaultText){
// Create our header to flush the image to browser
header("Content-type: image/png");
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; filename="GiftCard.png"');
// Define our source image
$image = imagecreatefrompng("card.png");
// Preserve transparency
imagealphablending($image, true);
imagesavealpha($image, true);
// Colors
$backgroundColor = imagecolorallocate ($image, 255, 255, 255);
$textColor = imagecolorallocate ($image, 255, 255,255);
$font = 'arial.ttf';
// Data
$data = [
'name' => [
'x' => 30,
'y' => 260,
'size' => 12,
'angle' => 0,
'content' => $name,
],
'pin' => [
'x' => 175,
'y' => 210,
'size' => 18,
'angle' => 0,
'content' => $pin
],
'denomination' => [
'x' => 435,
'y' => 45,
'size' => 20,
'angle' => 0,
'content' => $denomination
],
'defaultText' => [
'x' => 30,
'y' => 290,
'size' => 12,
'angle' => 0,
'content' => $defaultText
]
];
// Name
imagettftext($image, $data['name']['size'], $data['name']['angle'], $data['name']['x'], $data['name']['y'], $textColor, $font, $data['name']['content']);
// Pin
imagettftext($image, $data['pin']['size'], $data['pin']['angle'], $data['pin']['x'], $data['pin']['y'], $textColor, $font, $data['pin']['content']);
// Denomination
imagettftext($image, $data['denomination']['size'], $data['denomination']['angle'], $data['denomination']['x'], $data['denomination']['y'], $textColor, $font, '$' . $data['denomination']['content']);
// Default Text
imagettftext($image, $data['defaultText']['size'], $data['defaultText']['angle'], $data['defaultText']['x'], $data['defaultText']['y'], $textColor, $font, $data['defaultText']['content']);
// Create our base image with text
$base = imagepng($image);
// Do we need to place the logo?
if($_FILES["logo"]["name"]){
// Create our logo
$logo = imagecreatefrompng($target_dir.$_FILES["logo"]["name"]);
// Get current width/height of template
list($top_width, $top_height) = getimagesize($base);
// Get width/height of logo
list($bottom_width, $bottom_height) = getimagesize($logo);
// compute new width/height
$new_width = ($top_width > $bottom_width) ? $top_width : $bottom_width;
$new_height = $top_height + $bottom_height;
// create new image and merge
$new = imagecreate($new_width, $new_height);
imagecopy($new, $base, 0, 0, 0, 0, $top_width, $top_height);
imagecopy($new, $logo, 0, $top_height+1, 0, 0, $bottom_width, $bottom_height);
// Send image to browser
$final = imagepng($new);
echo $final;
// Free up memory
imagedestroy($final);
}else{
echo $base;
// Free up memory
imagedestroy($base);
}
}
我的尝试有什么突出的地方吗?简而言之,我正在尝试将文本和图像添加到 "template" png
图像上。认为最好先敲掉文字,然后将其用作徽标的新模板图像。
// Create our base image with text
$base = imagepng($image);
imagepng
returns 布尔值和 输出图像 。所以此时您已经将 headers 和工作中的 PNG 发送到浏览器。然后您尝试对 boolean
执行图像操作。这将生成警告消息。
你在这里重复错误:
...
// Send image to browser
$final = imagepng($new);
echo $final;
// Free up memory
imagedestroy($final);
} else {
echo $base;
// Free up memory
imagedestroy($base);
}
现在您要么输出两个 PNG 并回显一个布尔值,要么输出一个 PNG 并回显一个布尔值。无论如何,都会生成大量警告消息。
总结:
- 您不需要
$base
。而是使用 $image
.
- 您不需要创建或回显
$final
,只需调用 imagepng($new);
。
- 不要在最后的
else
块中 echo $base;
,请调用 imagepng($image);
。
所有这些都应该从 PHP 错误日志文件中显而易见。
我正在编写一些代码,允许用户为公司生成模拟礼品卡。他们输入面额、名称并上传徽标,它将使用基本模板图像并将该信息放在上面。
直到徽标部分,我的一切都按需要工作了。
我使用 imagecreatefrompng("template.png")
创建了我的模板图像,并使用 imagettftext()
将文本放置在所需的坐标中。
现在,说到徽标,我 运行 遇到了一些问题。
我尝试先创建基本图像(模板 + 文本),然后使用 imagecopy
将徽标添加到新图像,但我每次似乎得到的只是基本图像,我永远看不到徽标。
我已经尝试对图像的路径进行硬编码,但我仍然没有得到想要的结果。
// If we have all of the post data
if($denomination && $name && $pin && $defaultText){
// Create our header to flush the image to browser
header("Content-type: image/png");
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; filename="GiftCard.png"');
// Define our source image
$image = imagecreatefrompng("card.png");
// Preserve transparency
imagealphablending($image, true);
imagesavealpha($image, true);
// Colors
$backgroundColor = imagecolorallocate ($image, 255, 255, 255);
$textColor = imagecolorallocate ($image, 255, 255,255);
$font = 'arial.ttf';
// Data
$data = [
'name' => [
'x' => 30,
'y' => 260,
'size' => 12,
'angle' => 0,
'content' => $name,
],
'pin' => [
'x' => 175,
'y' => 210,
'size' => 18,
'angle' => 0,
'content' => $pin
],
'denomination' => [
'x' => 435,
'y' => 45,
'size' => 20,
'angle' => 0,
'content' => $denomination
],
'defaultText' => [
'x' => 30,
'y' => 290,
'size' => 12,
'angle' => 0,
'content' => $defaultText
]
];
// Name
imagettftext($image, $data['name']['size'], $data['name']['angle'], $data['name']['x'], $data['name']['y'], $textColor, $font, $data['name']['content']);
// Pin
imagettftext($image, $data['pin']['size'], $data['pin']['angle'], $data['pin']['x'], $data['pin']['y'], $textColor, $font, $data['pin']['content']);
// Denomination
imagettftext($image, $data['denomination']['size'], $data['denomination']['angle'], $data['denomination']['x'], $data['denomination']['y'], $textColor, $font, '$' . $data['denomination']['content']);
// Default Text
imagettftext($image, $data['defaultText']['size'], $data['defaultText']['angle'], $data['defaultText']['x'], $data['defaultText']['y'], $textColor, $font, $data['defaultText']['content']);
// Create our base image with text
$base = imagepng($image);
// Do we need to place the logo?
if($_FILES["logo"]["name"]){
// Create our logo
$logo = imagecreatefrompng($target_dir.$_FILES["logo"]["name"]);
// Get current width/height of template
list($top_width, $top_height) = getimagesize($base);
// Get width/height of logo
list($bottom_width, $bottom_height) = getimagesize($logo);
// compute new width/height
$new_width = ($top_width > $bottom_width) ? $top_width : $bottom_width;
$new_height = $top_height + $bottom_height;
// create new image and merge
$new = imagecreate($new_width, $new_height);
imagecopy($new, $base, 0, 0, 0, 0, $top_width, $top_height);
imagecopy($new, $logo, 0, $top_height+1, 0, 0, $bottom_width, $bottom_height);
// Send image to browser
$final = imagepng($new);
echo $final;
// Free up memory
imagedestroy($final);
}else{
echo $base;
// Free up memory
imagedestroy($base);
}
}
我的尝试有什么突出的地方吗?简而言之,我正在尝试将文本和图像添加到 "template" png
图像上。认为最好先敲掉文字,然后将其用作徽标的新模板图像。
// Create our base image with text
$base = imagepng($image);
imagepng
returns 布尔值和 输出图像 。所以此时您已经将 headers 和工作中的 PNG 发送到浏览器。然后您尝试对 boolean
执行图像操作。这将生成警告消息。
你在这里重复错误:
...
// Send image to browser
$final = imagepng($new);
echo $final;
// Free up memory
imagedestroy($final);
} else {
echo $base;
// Free up memory
imagedestroy($base);
}
现在您要么输出两个 PNG 并回显一个布尔值,要么输出一个 PNG 并回显一个布尔值。无论如何,都会生成大量警告消息。
总结:
- 您不需要
$base
。而是使用$image
. - 您不需要创建或回显
$final
,只需调用imagepng($new);
。 - 不要在最后的
else
块中echo $base;
,请调用imagepng($image);
。
所有这些都应该从 PHP 错误日志文件中显而易见。