gpt4 book ai didi

PHP GD 库 Image on Image

转载 作者:搜寻专家 更新时间:2023-10-31 21:21:09 25 4
gpt4 key购买 nike

我正在编写一些代码,允许用户为公司生成模拟礼品卡。他们输入面额、名称并上传 Logo ,它将使用基本模板图像并将该信息放在上面。

直到 Logo 部分,我的一切都按需工作。

我使用 imagecreatefrompng("template.png") 创建了我的模板图像,并使用 imagettftext() 将文本放置在所需的坐标中。

现在,谈到 Logo ,我遇到了一些问题。

我尝试先创建基本图像(模板 + 文本),然后使用 imagecopy 将 Logo 添加到新图像,但我每次似乎得到的只是基本图像和我从来没有看到 Logo 。

我已经尝试对图像的路径进行硬编码,但我仍然没有得到想要的结果。

  // 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 图像上。我认为最好先敲掉文本,然后将其用作 Logo 的新模板图像。

最佳答案

// Create our base image with text
$base = imagepng($image);

imagepng返回一个 bool 值并输出图像。所以此时您已经将 header 和工作 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 并回显一个 bool 值,要么输出一个 PNG 并回显一个 bool 值。无论如何,都会生成大量警告消息。

总结:

  1. 您不需要$base。而是使用 $image
  2. 您不需要创建或回显 $final,只需调用 imagepng($new);
  3. 不要在最后的 else block 中 echo $base;,调用 imagepng($image);

所有这些都应该从 PHP 错误日志文件中显而易见。

关于PHP GD 库 Image on Image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48038076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com