2016-10-25 3 views
0

Я пытаюсь добавить водяной знак (текст от входа), чтобы .jpg изображения (от входа) и определить позицию, это нормально для одной линии, но больше - начинает двигаться влево:Watermark текст с помощью imagettftext

Image

Update:

echo $the_box["width"] - 1132, 
echo $the_box["height"] - 25 

но мое изображение 1024x768.

if (isset($_POST['TextToUpload'])) { 
    $im = imagecreatefromjpeg($target_file);//jpg. to add watermark(text) 

$font = 'Roboto-Black.ttf'; // font path 
$font_size = 25; 
$font_color = imagecolorresolvealpha($im, 255, 255, 255,$tr1);//tr1 - var to define % of opacity 
$text = $wt;// wt - text from input $wt = $_POST['TextToUpload']; 
$lines = explode('|', wordwrap($text, 15, "\n", true)); //15 sumbols - line limit 

$text_angle=0;//var for angle 
$text_padding = 10; //some padding 
$imageX = imagesx($im)+ $text_padding; 
$imageY = imagesy($im)+ $text_padding; 
$the_box  = calculateTextBox($text, $font, $font_size, $text_angle); 
$y=$the_box["top"] + ($imageY/2) - ($the_box["height"]/2); 
$x=$the_box["left"] + ($imageX/2) - ($the_box["width"]/2); 

foreach ($lines as $line){  
imagettftext($im, $font_size, 0, $x, $y, $font_color, $font, $line); 
// Increment Y so the next line is below the previous line 
@$y += 23;} 

imagepng($im, $target_file); 
imagedestroy($im); 
} 

Я нашел много информации об этой проблеме, но с использованием imagecreate().

функция calculateTextBox: http://php.net/manual/en/function.imagettfbbox.php#105593

ответ

0

Решение:

$wt_len = strlen($text);//char count 
$lc=1;// num lines 
if ($wt_len > 15 && $wt_len <= 30) { 
     $lc=2; 
    } if ($wt_len > 30 && $wt_len <= 45) { 
     $lc=3; 
    } if ($wt_len > 45 && $wt_len <= 60) { 
     $lc=4; 
    } 

foreach ($lines as $line){ 


$y=$the_box["top"] + ($imageY/2) - ($the_box["height"]/2); 
$x=$the_box["left"] + ($imageX/2) - ($the_box["width"]/(2*$lc));// * num lines to get width of 1 line 

imagettftext($im, $font_size, 0, $x, $y, $font_color, $font, $line); 

}