[英]PHP GD Library Image on Image
I am working on some code that allows a users to generate a mock gift card for a company. 我正在研究一些允许用户为公司生成模拟礼品卡的代码。 They enter in a denomination, name, and upload a logo and it will use a base template image and put that information on top of it.
他们输入面额,名称和上传徽标,它将使用基本模板图像并将该信息放在其上。
Up until the logo part, I had everything working as needed. 直到徽标部分,我根据需要工作。
I created my template image using imagecreatefrompng("template.png")
and use imagettftext()
to place the text in the desired coordinates. 我使用
imagecreatefrompng("template.png")
创建了我的模板图像,并使用imagettftext()
将文本放在所需的坐标中。
Now, when it comes to the logo, I am running into some issues. 现在,当涉及到徽标时,我遇到了一些问题。
I tried creating the base image first (template + text) and then using imagecopy
in order to add the logo to the new image but all I appear to be getting each time is the base image and I never see the logo. 我首先尝试创建基本图像(模板+文本),然后使用
imagecopy
将徽标添加到新图像中,但我每次看起来都是基本图像而我从未看到过徽标。
I have tried hardcoding the path to the image and I am still not getting the desired result. 我试过硬编码图像的路径,但我仍然没有得到理想的结果。
// 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);
}
}
Anything stand out with what I am attempting? 什么东西都与我正在尝试的东西脱颖而出? In short, I am trying to add both text and an image onto a "template"
png
image. 简而言之,我正在尝试将文本和图像添加到“模板”
png
图像上。 Figured it would have been best to knock the text out first and then use that as the new template image for the logo. 想象一下,最好先将文本敲出来,然后将其用作徽标的新模板图像。
// Create our base image with text
$base = imagepng($image);
imagepng
returns a boolean and outputs the image . imagepng
返回一个布尔值并输出图像 。 So at this point you've sent your headers and a working PNG to the browser. 所以此时您已将标题和工作PNG发送到浏览器。 You then try to perform image operations on a
boolean
. 然后尝试对
boolean
执行图像操作。 This will generate warning messages. 这将生成警告消息。
You repeat the error here: 你在这里重复错误:
...
// Send image to browser
$final = imagepng($new);
echo $final;
// Free up memory
imagedestroy($final);
} else {
echo $base;
// Free up memory
imagedestroy($base);
}
Now you've either output two PNGs and echoed a boolean, or output one PNG and echoed a boolean. 现在你要么输出两个PNG并回显一个布尔值,要么输出一个PNG并回显一个布尔值。 Regardless, warning messages aplenty will be generated.
无论如何,都会产生很多警告信息。
In summary: 综上所述:
$base
. $base
。 Instead use $image
. $image
。 $final
, only call imagepng($new);
$final
,只需要调用imagepng($new);
. echo $base;
echo $base;
in your final else
block, call imagepng($image);
else
块,调用imagepng($image);
. All of this should have been apparent from the PHP error log file. 所有这些都应该从PHP错误日志文件中显而易见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.