【问题标题】:Aligning php Generated Image dynamic text in center将php生成的图像动态文本居中对齐
【发布时间】:2013-01-09 03:19:47
【问题描述】:

我想将图像上生成的文本与图像的中心对齐。目前,我不知道是否可以对齐它。下面是代码。

$im = @imagecreatefromjpeg('poloroid.jpg');

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
//imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
//$text = 'John...';
$fbid = $_POST["id"]; 
$text = $_POST["want"];
$fb_email =$_POST["email"];
$fb_name=$_POST["name"];

$uploads_dir = 'uploaded_files/';
// Replace path by your own font path
$font = 'verdana.ttf';

//image file name
//$name ="$fbid.png";
$name = $uploads_dir.$fbid.".png"; //this saves the image inside uploaded_files folder

// Add some shadow to the text
imagettftext($im, 20, 0,  25, 126, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 25, 125, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
//imagepng($im);
imagepng($im,$name,9);
imagedestroy($im);

感谢大家的帮助。

【问题讨论】:

    标签: php imagettftext


    【解决方案1】:

    我已经稍微更新了你的代码:

    function ImageTTFCenter($image, $text, $font, $size, $angle = 45) 
    {
        $xi = imagesx($image);
        $yi = imagesy($image);
    
        $box = imagettfbbox($size, $angle, $font, $text);
    
        $xr = abs(max($box[2], $box[4]));
        $yr = abs(max($box[5], $box[7]));
    
        $x = intval(($xi - $xr) / 2);
        $y = intval(($yi + $yr) / 2);
    
        return array($x, $y);
    }
    
    $im = @imagecreatefromjpeg('poloroid.jpg');
    
    // Create some colors
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    //imagefilledrectangle($im, 0, 0, 399, 29, $white);
    
    // The text to draw
    //$text = 'John...';
    $fbid = $_POST["id"]; 
    $text = $_POST["want"];
    $fb_email =$_POST["email"];
    $fb_name=$_POST["name"];
    
    $uploads_dir = 'uploaded_files/';
    // Replace path by your own font path
    $font = 'verdana.ttf';
    
    //image file name
    //$name ="$fbid.png";
    $name = $uploads_dir.$fbid.".png"; //this saves the image inside uploaded_files folder
    
    list($x, $y) = ImageTTFCenter($im, $text, $font, 20)
    // Add some shadow to the4 text
    imagettftext($im, 20, 0, $x, $y+1, $grey, $font, $text);
    
    // Add the text
    imagettftext($im, 20, 0, $x, $y, $black, $font, $text);
    
    // Using imagepng() results in clearer text compared with imagejpeg()
    //imagepng($im);
    imagepng($im,$name,9);
    imagedestroy($im);
    

    ImageTTFCenter 函数会找到你图像的中心坐标,你会告诉imagettftext

    【讨论】:

    • 它将文本的开头对齐在中间,但不使文本居中。你明白我的意思吗?
    【解决方案2】:
    $im = @imagecreatefromjpeg('poloroid.jpg');
    
    // Create some colors
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    //imagefilledrectangle($im, 0, 0, 399, 29, $white);
    
    // The text to draw
    //$text = 'John...';
    $fbid = $_POST["id"]; 
    $text = $_POST["want"];
    $fb_email =$_POST["email"];
    $fb_name=$_POST["name"];
    
    $uploads_dir = 'uploaded_files/';
    // Replace path by your own font path
    $font = 'verdana.ttf';
    $font_size = 20;
    $angle = 45;
    
    //image file name
    //$name ="$fbid.png";
    $name = $uploads_dir.$fbid.".png"; //this saves the image inside uploaded_files folder
    
    // Get image Width and Height
    $image_width = imagesx($im);  
    $image_height = imagesy($im);
    
    // Get Bounding Box Size
    $text_box = imagettfbbox($font_size,$angle,$font,$text);
    
    // Get your Text Width and Height
    $text_width = $text_box[2]-$text_box[0];
    $text_height = $text_box[7]-$text_box[1];
    
    // Calculate coordinates of the text
    $x = ($image_width/2) - ($text_width/2);
    $y = ($image_height/2) - ($text_height/2);
    
    // Add some shadow to the text
    imagettftext($im, $font_size, 0, $x, $y+1, $grey, $font, $text);
    
    // Add the text
    imagettftext($im, $font_size, 0, $x, $y, $black, $font, $text);
    
    // Using imagepng() results in clearer text compared with imagejpeg()
    //imagepng($im);
    imagepng($im,$name,9);
    imagedestroy($im);
    

    【讨论】:

    • 我使用了这个代码。但是由于宽度的不同,图像的位置不同。对吗?
    • 没看懂,请说的详细点。
    • 计算文本边界框高度的行应该是:$text_height = $text_box[7]-$text_box[1]; - 上面的代码错误地将其计算为 0。遗憾的是,我不允许编辑单个字符...
    • 根据@John 的上述评论编辑答案。还添加了对证明此更改合理性的 imagettfbbox() 文档的引用。
    • 根据documentationxy坐标代表基线起点的坐标,所以不会居中
    【解决方案3】:

    您可以使用stil/gd-text 类。免责声明:我是作者。

    <?php
    use GDText\Box;
    use GDText\Color;
    
    $im = @imagecreatefromjpeg('poloroid.jpg');
    
    $textbox = new Box($im);
    $textbox->setFontSize(20);
    $textbox->setFontFace('verdana.ttf');
    $textbox->setFontColor(new Color(0, 0, 0)); // black
    $textbox->setTextShadow(
        new Color(0, 0, 0, 80), // black color, but 60% transparent
        0,
        -1 // shadow shifted 1px to top
    );
    $textbox->setBox(
        0,  // distance from left edge
        0,  // distance from top edge
        imagesx($im), // textbox width, equal to image width
        imagesy($im)  // textbox height, equal to image height
    );
    
    // now we have to align the text horizontally and vertically inside the textbox
    // the texbox covers whole image, so text will be centered relatively to it
    $textbox->setTextAlign('center', 'center');
    // it accepts multiline text
    $textbox->draw($text);
    
    $uploads_dir = 'uploaded_files/';
    //image file name
    //$name ="$fbid.png";
    $name = $uploads_dir.$fbid.".png"; //this saves the image inside uploaded_files folder
    imagepng($im, $name, 9);
    imagedestroy($im);
    

    演示:

    【讨论】:

    • 非常好用
    • 太棒了。它非常易于使用。很好的文档。
    • 嘿,这很好用。有什么方法可以自动增加字体大小直到字体填满整个宽度?例如,如果我正在制作一个希望单词“example”填充整个宽度的徽标生成器 - 指定的填充?
    • @smartrahat 如何设置多个段落?就像我如何知道第一段在哪个坐标结束并为第二段及以后设置相应的框大小?我试图标记作者,但不知何故它没有链接):
    【解决方案4】:
    foreach ($user as $key=>$value){    
        $bb = imagettfbbox($value['font-size'],0,$fontname,$value['name']);    
        $WW = abs($bb[2]-$bb[0]);    
        $XX = ($value['XPos']+$WW);    
        $HH = abs($bb[5]-$bb[3]);    
        $HH +=1;
        $HHH += $HH;
        imagettftext($im, $value['font-size'], 0, $value['XPos'],   $value['YPos'], $color[$value['color']], $fontname, $value['name']);  
        $HHH += 1;
        $WIDE = abs($bb[2]-$bb[0]);  
        $endpoint=$value['XPos']+$WIDE;  
        $bb2 = imagettfbbox($value['font-size'],0,$fontname,$value['name']);  
        $WW2 = abs($bb2[2]-$bb2[0]);     
        $x2pos= $endpoint-$WW2;    
        imagettftext($im, $value['font-size'], 0, $x2pos, $value['YPos'], $color[$value['color']], $fontname, $value['name']);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      相关资源
      最近更新 更多