简体   繁体   English

PHP-GD使行高与文本环绕上的所有字体兼容

[英]PHP-GD make line height compatible across all fonts on text wrap

Ok, so am working on a simple generator using jquery. 好的,所以我正在使用jquery开发一个简单的生成器。 A user enters a text of his/her choice, selects a font, font-color and font-size. 用户输入他/她选择的文本,选择字体,字体颜色和字体大小。 All this properties are displayed on a separate div in real-time (live preview). 所有这些属性都实时(实时预览)显示在单独的div上。

I now need to save the generated preview as a picture. 现在,我需要将生成的预览另存为图片。 For that, I use php GD library. 为此,我使用php GD库。 All works fine but with some fonts, everything is just messed up. 一切正常,但带有某些字体,一切都搞砸了。

第一个字体行高看起来很完美

第二个字体行高乱七八糟

In the first image, everything looks alright but the second image, the line height is just messed up. 在第一个图像中,一切看起来都不错,但是在第二个图像中,行高刚刚被弄乱了。

This is my php script that am using to processes the properties 这是我用来处理属性的PHP脚本

    <?php
        //Width and height of desired image  
        $width = 320;
        $height= 320;

       //Create an image with specified height and width
       $main_img = ImageCreate($width, $height);
       $mx = imagesx($main_img); 
       $my = imagesy($main_img); 

       //Capture values from form
       $main_text = $_POST['rtext'];
       $main_text_size =  $_POST['rfsize'];
       $color = $_POST['rcolor']; 
       $mt_f = $_POST['rfont'];
       $main_text_x = ($mx/2);


       // more code here      


       //wrap text if text too long
       $words = explode(' ', $main_text); 
       $lines = array($words[0]); 
       $currentLine = 0; 
       for($i = 1; $i < count($words); $i++) 
       { 
            $lineSize = imagettfbbox($main_text_size, 0, $mt_f, $lines[$currentLine] . ' ' . $words[$i]); 
            if($lineSize[2] - $lineSize[0] < $mx) 
            { 
                $lines[$currentLine] .= ' ' . $words[$i]; 
            } 
            else 
            { 
                $currentLine++; 
                $lines[$currentLine] = $words[$i]; 
            } 
       } 
       $line_count = 1; 

       // Loop through the lines and place them on the image 
       foreach ($lines as $line) 
       { 
           $line_box = imagettfbbox($main_text_size, 0, $mt_f, "$line"); 
           $line_width = $line_box[0]+$line_box[2]; 
           $line_height = $line_box[1]-$line_box[7]; 
           $line_margin = ($mx-$line_width)/2; 
           $line_y = (($line_height+12) * $line_count); 
           imagettftext($main_img, $main_text_size, 0, $line_margin, $line_y, $main_text_color, $mt_f, $line); 

           // Increment Y so the next line is below the previous line 
           $line_count ++; 
      } 
      header("Content-type: image/png");

      //code to download the image

    ?>

Is there a way I can modify the part of the code where I wrap the text to accomodate all fonts? 有没有一种方法可以修改我在其中包裹文本以适应所有字体的代码部分? Like automatically calculate the line height based on the font? 喜欢根据字体自动计算线高吗?

Thanks, any help will be appreciated 谢谢,任何帮助将不胜感激

I found a very useful class at phpclasses imagefittext.class.php http://www.phpclasses.org/browse/file/41869.html . 我在phpclasses imagefittext.class.php http://www.phpclasses.org/browse/file/41869.html上找到了一个非常有用的类。 I also found an example script that is implemented using the class http://www.phpclasses.org/browse/file/41870.html . 我还找到了使用类http://www.phpclasses.org/browse/file/41870.html实现的示例脚本。 This is exactly what I wanted. 这正是我想要的。

Worked Perfectly!!!1 完美地工作!!! 1

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

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