微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php代码中的字体质量差imagettftext

如何解决php代码中的字体质量差imagettftext

即使经过长时间的Google搜索和许多尝试,也无法解决此问题,但仍然没有运气。

.PHP文件中的字体质量非常差,从不同的网站上下载了相同的字体-没运气(质量仍然很差),甚至从Google字体中下载了随机字体(认为我的下载字体因未知原因而损坏了)-仍然没有运气。

不知道如何解决此问题并使其以适当的质量输出到图像上。

表单代码

<div class="s Box hide" id="div3">
                   
                    <label></label> <input type="text" style="text-transform: uppercase;"   placeholder="Iki 6 skaitmenų" pattern="[A-Za-z0-9]+"  class="form-control" name="txt_input_" maxlength="6" >
                </div>
                <br>
        <label><input type="radio" name="check" onclick="show3();" value="s">Individual</label>
        </div>
<div class="button-row">
            <input type="submit" id="submit" name="submit" class="btn btn-info" value="Generate">
        </div>

主要的PHP函数代码生成所有内容):

if (isset($_POST['submit'])) {
    
    
    $check = $_POST['check'];
    
    
    if($check == 's'){
        
    $txt_input_ = $_POST['txt_input_'];
     $countNumber = strlen($txt_input_);
    $txt_input_ = strtoupper($txt_input_);
    $num_input = $_POST['num_input'];
    $width = 680;
    $height = 240;
    
    $textimage = imagecreate($width,$height);
    $grey = imagecolorallocate($textimage,100,100);
    $white = imagecolorallocate($textimage,255,255);
    imagecolortransparent($textimage,0);
    $color = imagecolorallocate($textimage,0);
   
    
    
    // create background image layer
    $background = imagecreatefromjpeg('http://sample-domain.com/greyspace.jpg');
    $font = "Roboto-Bold.ttf"; 

    //imagestring($textimage,5,110,20,$input_text,0xFFFFFF);
    
    if($countNumber ==1){
    imagettftext($textimage,60,200,83,$color,$font,$txt_input_);
    }
    
    if($countNumber ==2){
    imagettftext($textimage,180,$txt_input_);
    }
    if($countNumber ==3){
    imagettftext($textimage,155,$txt_input_);
    }
    if($countNumber ==4){
    imagettftext($textimage,135,$txt_input_);
    }
    if($countNumber ==5){
    imagettftext($textimage,125,$txt_input_);
    }
    
    if($countNumber ==6){
    imagettftext($textimage,105,$txt_input_);
    }
    

    // Merge background image and text image layers
   imagecopymerge($background,$textimage,$width,$height,99);
    
    $width = 680;
    $height = 240;
    $output = imagecreatetruecolor($width,$height);
    imagecopy($output,$background,$height);
    
    
    ob_start();

    imagejpeg($output);
    printf('<img id="output" src="data:image/jpeg;base64,%s" />',base64_encode(ob_get_clean()));
         ob_start();
     imagejpeg($output);
     
     printf('<br><br><a id="" href="data:image/jpeg;base64,%s" download><input type="button" value="Download"></a>',base64_encode(ob_get_clean()));
     
    }

在主要结果中,我生成的jpeg中的字体质量(尝试使用png-仍然相同的质量):

enter image description here

图片符号中边缘非常锐利,就像像素一样,很显然它不是处于最佳状态。 有什么建议可以摆脱这种头痛吗? 预先感谢!

解决方法

正确解决此问题的方法是使用具有适当背景颜色的图像,即使该背景颜色是透明的。这使得图像库使用适当的Alpha通道(这是进行Alpha混合的唯一明智的方法),而不是使用基于索引的Alpha,后者仅一种颜色是透明的,而所有其他颜色都是完全不透明的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。