php - 如何在 PHP GD 中编辑 2 个帖子的文本颜色
问题描述
我想问一下,我有一个网络报价生成器,执行代码如下,
<?php
if(isset($_POST['execute'])) {
echo "<label>Result :</label>";
$folder = "files/quote/";
$overlay = $folder."overlay.png";
$font_quote = "files/_font/"."Ubuntu-Medium.ttf";
$font_copyright = "files/_font/"."Ubuntu-Medium.ttf";
$filename = $folder.md5(rand(000,999)).".png";
$quote = @$_POST['quote'] ? $_POST['quote'] : 'YOUR QUOTE';
$copyright = @$_POST['copyright'] ? $_POST['copyright'] : 'Username';
$backgrond = @$_POST['background'];
if (!filter_var($backgrond, FILTER_VALIDATE_URL) === false) {
$bg = $backgrond;
}else {
$bg = get_redirect_target('https://source.unsplash.com/640x640/?'.urlencode($backgrond));
}
$image = new PHPImage();
$image->setQuality(10);
$image->setDimensionsFromImage($overlay);
$image->draw($bg);
$image->draw($overlay, '50%', '75%');
$image->setFont($font_quote);
$image->setTextColor(array(255, 255, 255));
$image->setAlignVertical('center');
$image->setAlignHorizontal('center');
$image->textBox($quote, array(
'fontSize' => 28,
'x' => 130,
'y' => 240,
'width' => 380,
'height' => 200,
'debug' => false
));
$image->setFont($font_copyright);
$image->setTextColor(array(230, 209, 65));
$image->text('CopyRight © '.$copyright, array(
'fontSize' => 15,
'x' => 0,
'y' => 535,
'width' => 640,
'height' => 20,
'debug' => false
));
$image->save($filename);
$imagebase64 = "data:image/png;base64,".base64_encode(file_get_contents($filename));
echo "<a href='".$imagebase64."' target='_blank' download='$filename'><img src='".$imagebase64."'/></a>";
unlink($filename);
}
?>
问题:
1copyright
字码是否可以做成2色,不只是1色,码还是1色也就是黄色,所以如果有投稿的话,颜色是白色的Copyright ©
,黄色的是username
?
解决方案
你可以试试这个
<?php
if(isset($_POST['execute'])) {
echo "<label>Result :</label>";
$folder = "files/quote/";
$overlay = $folder."overlay.png";
$font_quote = "files/_font/"."Ubuntu-Medium.ttf";
$font_copyright = "files/_font/"."Ubuntu-Medium.ttf";
$filename = $folder.md5(rand(000,999)).".png";
$quote = @$_POST['quote'] ? $_POST['quote'] : 'YOUR QUOTE';
$copyright = @$_POST['copyright'] ? $_POST['copyright'] : 'Username';
$backgrond = @$_POST['background'];
if (!filter_var($backgrond, FILTER_VALIDATE_URL) === false) {
$bg = $backgrond;
}else {
$bg = get_redirect_target('https://source.unsplash.com/640x640/?'.urlencode($backgrond));
}
$image = new PHPImage();
$image->setQuality(10);
$image->setDimensionsFromImage($overlay);
$image->draw($bg);
$image->draw($overlay, '50%', '75%');
$image->setFont($font_quote);
$image->setTextColor(array(255, 255, 255));
$image->setAlignVertical('center');
$image->setAlignHorizontal('center');
$image->textBox($quote, array(
'fontSize' => 28,
'x' => 130,
'y' => 240,
'width' => 380,
'height' => 200,
'debug' => false
));
$image->setFont($font_copyright);
$image->setTextColor(array(255, 255, 255));
$image->text('CopyRight © ', array(
'fontSize' => 15,
'x' => 0,
'y' => 535,
'width' => 640,
'height' => 20,
'debug' => false
));
$image->setFont($font_copyright);
$image->setTextColor(array(230, 209, 65));
$image->text($copyright, array(
'fontSize' => 15,
'x' => 100,
'y' => 535,
'width' => 640,
'height' => 20,
'debug' => false
));
$image->save($filename);
$imagebase64 = "data:image/png;base64,".base64_encode(file_get_contents($filename));
echo "<a href='".$imagebase64."' target='_blank' download='$filename'><img src='".$imagebase64."'/></a>";
unlink($filename);
}
?>
如果您注意到,我已将您的版权文本与用户名变量分开。我将它们都放在相同的 Y 轴上,但在不同的 X 轴上,就像在图表上绘制一样。我还给了它们不同的颜色但相同的字体。
您可以调整版权 X 位置,以防 100 与用户名太远或太近
推荐阅读
- c++ - 使用 PowerShell(或命令提示符)进行调试
- python - 在 Pytest 中,根级别的 conftest.py 是否会被树中更深的 conftest 覆盖?
- c# - 为什么我得到“不能转换类型泛型列表
“? - angular - TypeError:无法读取未定义的属性“管理员”
- python - 将 numpy float64 数据类型转换为 2 个列表
- reactjs - 将主机映射到容器以在源代码更改时进行即时更新时出现 Docker 权限问题
- html - 媒体查询没有被触发
- jquery - 在提交表单时去掉电话号码格式?
- typescript - 未通过 typeof 检查丢弃未定义
- conv-neural-network - 你如何在 GAN 中使用混合变量来定义潜在空间?