2015-01-21 111 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