2015-01-21 39 views
0

我得到这个脚本生成了一个目录中随机的JPG图像,从数据库中添加一个随机的口号:环绕文本中imagettftext()

<?php 

header('Content-type: text/html; charset=utf-8'); 

include '../connect.php'; 
require_once 'random.php'; 

$timestamp = time(); 
$date = date("d.m.Y_G", $timestamp); 


$slogan = $mysqli->query("SELECT `text` FROM `slogans` ORDER BY RAND() LIMIT 1"); 

    $slogan_txt = $slogan->fetch_assoc(); 



    $bg = get_rand_img('../../images/'); 
    $imgPath = '../../images/' .$bg; 

$text = $slogan_txt[text]; 
$image = $imgPath; 
$font = "font.TTF"; 
$font_size = "25"; 

$image_2 = imagecreatefromjpeg($image); 
$black = imagecolorallocate($image_2,0,0,0); 

$image_width = imagesx($image_2); 
$image_height = imagesy($image_2); 

$text_box = imagettfbbox($font_size,$angle,$font,$text); 
$text_width = $text_box[2]-$text_box[0]; // lower right corner - lower left corner 
$text_height = $text_box[3]-$text_box[1]; 

$x = ($image_width/2) - ($text_width/2); 
$y = ($image_height/2) - ($text_height/2); 

imagettftext($image_2,$font_size,0,$x,$y,$black,$font,$text); 

header ("Content-type: image/png"); 
imagejpeg($image_2); 



?> 

工作正常为止。

现在有一些口号对一行很多单词。我需要他们被包裹,也被集中!

不能在imagettftext()中使用wordwrap,所以我需要以某种方式爆炸它。

我发现网络上的一些功能,但他们没有按预期工作。也许我只是不知道如何将它们与我现有的代码结合起来!

也许有人从自己的项目中得到一个工作示例?

感谢到目前为止!

回答

0

好了,现在的工作。

在这里找到了一些有趣的东西,那就是它!只需要添加计算文字的起始位置(响应图片大小)即可。

GDtext by stil

谢谢!

0

这爆炸一个字符串,并将其放入第二个字符串,如果第一个比14更长,我想你可以在此建立起来。

$string = ""; 
$string2 = ""; 
$name = explode(" ", $name); 
foreach ($name as $n) { 
    if (strlen($string) + strlen($n) > 14) { 
      $string2 .= $n . " "; 
     } else { 
      $string .= $n . " "; 
     }  
} 

为中心,你需要做的是这样的文字: (imagesisex/2) - 数字* pixelsize