简体   繁体   English

如何在PHP GD中编辑2个帖子的文本颜色

[英]How to edit the text color of 2 posts in PHP GD

I want to ask, I have a web quote maker and execute code as follows, 我想问一下,我有一个网络报价制作工具,并执行以下代码,

<?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);
}

?>

Question: 题:

Whether in 1 the copyright word code can be made into 2 colors, not only 1 color, the code is still 1 color that is yellow, so if there is a submission, the color is white Copyright © and yellow is the username ? 是否可以在1中将copyright文字代码制成2种颜色,而不仅是1种颜色,该代码仍然是黄色的1种颜色,因此,如果有提交,则颜色为白色Copyright © ,黄色是username

You can try this 你可以试试这个

<?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);
}

?>

If you notice, I have separated your copyright text from the username variable. 如果您注意到,我已经将您的版权文本与username变量分开了。 I have placed both of them on same Y axis but different X axis just like plotting on a graph. 我将它们都放置在相同的Y轴上,但放置在不同的X轴上,就像在图形上绘制一样。 I also gave them different colors but same font. 我还给了他们不同的颜色,但字体相同。

You can adjust the copyright X position in case 100 makes it too far or too close to the username 您可以调整版权X的位置,以防100距离用户名太远或太近

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM