2010-03-14 8 views
21

के साथ किसी छवि में टेक्स्ट को सही संरेखित करें, मैं अपने उपयोगकर्ताओं के लिए गतिशील फ़ोरम हस्ताक्षर छवियां सेट अप कर रहा हूं और मैं अपना उपयोगकर्ता नाम छवि पर डाल सकता हूं। मैं इसे ठीक करने में सक्षम हूं, लेकिन चूंकि उपयोगकर्ता नाम अलग-अलग लंबाई हैं और मैं उपयोगकर्ता नाम को सही संरेखित करना चाहता हूं, तो मुझे यह करने के लिए कैसे जा सकता है जब मुझे x & y निर्देशांक सेट करना होगा।imagettftext(), PHP

$im = imagecreatefromjpeg("/path/to/base/image.jpg"); 
$text = "Username"; 
$font = "Font.ttf"; 
$black = imagecolorallocate($im, 0, 0, 0); 

imagettftext($im, 10, 0, 217, 15, $black, $font, $text); 
imagejpeg($im, null, 90); 
+0

ठीक है। मुझे कई पंक्तियों के साथ एक ही समस्या है। – Melki

उत्तर

56

स्ट्रिंग की चौड़ाई प्राप्त करने के लिए imagettfbbox फ़ंक्शन का उपयोग करें और फिर प्रारंभिक एक्स-समन्वय प्राप्त करने के लिए छवि की चौड़ाई से उस घटाएं।

$dimensions = imagettfbbox($fontSize, $angle, $font, $text); 
$textWidth = abs($dimensions[4] - $dimensions[0]); 
$x = imagesx($im) - $textWidth; 
+0

मैं अभी आपको इतना कठिन प्यार करता हूँ। यह बहुत अच्छा काम किया। – rncrtr

+0

@ एंडी ... मुझे भी (बस आपका जवाब पसंद आया) – h2O

+0

एकाधिक लाइनों के साथ कोई विचार? – Melki

3

पूर्व की गणना उपयोगकर्ता का नाम imagettfbbox() का उपयोग करने का आकार।

उस चौड़ाई से आप वहां से प्राप्त होते हैं, तो आप उस एक्स स्थिति को घटा सकते हैं जिस पर आपके टेक्स्ट को शुरू करने की आवश्यकता है।

1

यह काम करेंगे ...............

    $s = split("[\n]+", $text); 
        $top=20; 
        $left=30; 
        $font_file="yourfont.ttf"; 
        $fontsize=20; 
       $__H=$top; 
       foreach($s as $key=>$val){ 
        $_b = imageTTFBbox($fontsize,0,$font_file,$val); 
        $_W = abs($_b[2]-$_b[0]); 
        $_X = ($left+$text_box_width)-$_W; 
        $_H = abs($_b[5]-$_b[3]); 
        $_H +=1; 
        $__H += $_H;    
        $res=imagettftext($image, $this->_fontsize, 0, $_X, $__H, $color, $font_file, $val); 
        $__H += 1; 
5

आप stil/gd-text वर्ग का उपयोग कर सकते हैं। अस्वीकरण: मैं लेखक हूं।

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

$im = imagecreatefromjpeg("/path/to/base/image.jpg"); 

$textbox = new Box($im); 
$textbox->setFontSize(12); 
$textbox->setFontFace("Font.ttf"); 
$textbox->setFontColor(new Color(0, 0, 0)); // black 
$textbox->setBox(
    50, // distance from left edge 
    50, // distance from top edge 
    200, // textbox width 
    100 // textbox height 
); 

// text will be aligned inside textbox to right horizontally and to top vertically 
$textbox->setTextAlign('right', 'top'); 

$textbox->draw("Username"); 

आप बहुभाषी पाठ भी खींच सकते हैं। draw() विधि को पारित स्ट्रिंग में बस \n का उपयोग करें। उदाहरण इस वर्ग के साथ उत्पन्न:

right aligned text demo

1

मैं sujithayur कोड, और बनाया समारोह जो सभी संरेखित करता है (बाएं, केंद्र, दाएं) & (ऊपर, केंद्र, मध्य) और उसके संयोजन की अनुमति देता है बढ़ाया। यह पाठ छाया का भी उपयोग करता है।

// $x is margin from left, in case of left align, and margin from right, in case of right horizontal align 
// $alignHorizontal values can be 'left', 'center', 'right' 
// $alignVertical values can be 'top', 'center', 'bottom' 
function imagettftext_aligned($image, $fontSize, $x, $y, $color, $colorShadow, $fontPath, $text, $alignHorizontal, $alignVertical) { 

     $s = explode("\n", $text); 
     $imageWidth = imagesx($image); 
     $imageHeight = imagesy($image); 

     $top=$y; 
     $left=$imageWidth - $x; 
     $__H=$top; // default - top 
     $lineHeight = $fontSize + 14; 

     if ($alignVertical == 'bottom') 
     $__H = $imageHeight - $y - (count($s) * $lineHeight); 
     elseif ($alignVertical == 'center') 
     $__H = $imageHeight/2 - (count($s) * $lineHeight)/2; 

     foreach($s as $key=>$val){ 
      $_b = imageTTFBbox($fontSize,0,$fontPath,$val); 
      $_W = abs($_b[2]-$_b[0]); 
      $_H = abs($_b[5]-$_b[3]); 
      $_H +=1; 

      if ($alignHorizontal == 'right') 
       $_X = $left - $_W; 
      elseif ($alignHorizontal == 'center') 
       $_X = $imageWidth/2 - $_W/2; 
      else // default - left 
       $_X = $x; 

      imagettftextblur($image, $fontSize, 0, $_X + 2, $__H + 2, $colorShadow, $fontPath, $val, 4); // 1 can be higher to increase blurriness of the shadow 
      imagettftextblur($image, $fontSize, 0, $_X, $__H, $color, $fontPath, $val); 

      $__H += $lineHeight + 1; 
     } 

    return ['bottom' => $__H]; 

} 

// https://github.com/andrewgjohnson/imagettftextblur 
if (!function_exists('imagettftextblur')) 
{ 
    function imagettftextblur(&$image,$size,$angle,$x,$y,$color,$fontfile,$text,$blur_intensity = null) 
    { 
     $blur_intensity = !is_null($blur_intensity) && is_numeric($blur_intensity) ? (int)$blur_intensity : 0; 
     if ($blur_intensity > 0) 
     { 
      $text_shadow_image = imagecreatetruecolor(imagesx($image),imagesy($image)); 
      imagefill($text_shadow_image,0,0,imagecolorallocate($text_shadow_image,0x00,0x00,0x00)); 
      imagettftext($text_shadow_image,$size,$angle,$x,$y,imagecolorallocate($text_shadow_image,0xFF,0xFF,0xFF),$fontfile,$text); 
      for ($blur = 1;$blur <= $blur_intensity;$blur++) 
       imagefilter($text_shadow_image,IMG_FILTER_GAUSSIAN_BLUR); 
      for ($x_offset = 0;$x_offset < imagesx($text_shadow_image);$x_offset++) 
      { 
       for ($y_offset = 0;$y_offset < imagesy($text_shadow_image);$y_offset++) 
       { 
        $visibility = (imagecolorat($text_shadow_image,$x_offset,$y_offset) & 0xFF)/255; 
        if ($visibility > 0) 
         imagesetpixel($image,$x_offset,$y_offset,imagecolorallocatealpha($image,($color >> 16) & 0xFF,($color >> 8) & 0xFF,$color & 0xFF,(1 - $visibility) * 127)); 
       } 
      } 
      imagedestroy($text_shadow_image); 
     } 
     else 
      return imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text); 
    } 
}