2015-05-07 3 views
0

Я делаю небольшое приложение в php, которое помещает текст в изображение. Я получил его wotking с imagettftext. Я также смог поместить его в imagettfbbox с определенной шириной, используя эту функцию http://php.net/manual/en/function.imagettftext.php#89505. Моя единственная проблема в том, что я хочу, чтобы текст был выровнен в правой части окна, как на картинке enter image description herePHP текст для изображения, выравнивание текста

+0

Нужно видеть ваш пример кода. Размещение текстового блока TFF будет основано на значениях X, Y. Измените и добавьте свой код. – Twisty

ответ

0

Вот код, но мне не нужно выровнять всю ячейку справа на оси X, но только текст. Вы можете изображения это нравится текст в HTML

<p style="width:300px;text-align:right">This is the text to be wrapped and aligned to the right</p> 

function wrap($fontSize, $angle, $fontFace, $string, $width){ 

    $ret = ""; 
    $arr = explode(' ', $string); 
    foreach ($arr as $word){ 
     $teststring = $ret.' '.$word; 
     $testbox = imagettfbbox($fontSize, $angle, $fontFace, $teststring); 
     if ($testbox[2] > $width){ 
      $ret.=($ret==""?"":"\n").$word; 
     } else { 
      $ret.=($ret==""?"":' ').$word; 
     } 
    } 
    return $ret; 
} 

function textToImage(){ 
    $pluginDir = plugin_dir_path(__FILE__); 
    $image = imagecreatefrompng($pluginDir . 'resources/img/image.png'); 
    $color = imagecolorallocate($image, 38, 64, 142); 
    $font_path = $pluginDir . 'resources/font/DroidSans.ttf'; 
    $line = "This is the text to be wrapped and aligned to the right"; 

    imagettftext($image, 35, 0, 300, 600, $color, $font_path, wrap(18, 0, $font_path, $line, 300)); 

    imagepng($image, $pluginDir."resources/img/result.png"); 
    imagedestroy($image); 
} 
+0

Любые помощь ребята? Я очень отчаянный. – Jamalissimo

0

Вы можете использовать stil/gd-text класс. Он выполняет выравнивание по правому краю и перенос текста.

<?php 
use GDText\Box; 
use GDText\Color; 

$pluginDir = plugin_dir_path(__FILE__); 
$image = imagecreatefrompng($pluginDir . 'resources/img/image.png'); 

$textbox = new Box($image); 
$textbox->setFontSize(35); 
$textbox->setFontFace($pluginDir . 'resources/font/DroidSans.ttf'); 
$textbox->setFontColor(new Color(38, 64, 142)); 
$textbox->setBox(
    300, // distance from left edge 
    600, // distance from top edge 
    500, // textbox width 
    500 // textbox height 
); 

// now we have to align the text horizontally and vertically inside the textbox 
$textbox->setTextAlign('top', 'right'); 
// it can wrap multilined text 
$textbox->draw("This is the text to be wrapped and aligned to the right"); 

imagepng($image, $pluginDir."resources/img/result.png"); 
imagedestroy($image); 

Демонстрация:

demo

Смежные вопросы