Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problems in writing text on an image

    I am using a function that write text on image, the problem is that it sometimes do not center align text and sometimes some characters missed. I use the following logic in the function, i set no of pixels for one character and multiply it with the total no of characters and divide it with the width in pixels of an image, but it is not working accurate at sometime because, i set the character pixels myself if know some way how to get pixels of a character because some characters takes more pixels and some less i.e caps characters. the code is as follows plz suggest a solution

    function writeOnImage($filePath, $title, $author, $subtitle, $title_R, $title_G, $title_B, $subtitle_R, $subtitle_G, $subtitle_B, $title_fontsize, $subtitle_fontsize, $author_R, $author_G, $title_B, $author_fontsize,$finalImage)
    {
    //$existingImage = substr($existingImage, 3);
    $im = imagecreatetruecolor(90, 30);
    $title_fontColor = imagecolorallocate($im, $title_R, $title_G, $title_B);
    $subtitle_fontColor = imagecolorallocate($im, $subtitle_R, $subtitle_G, $subtitle_B);
    $author_fontColor = imagecolorallocate($im, $author_R, $author_G, $author_B);
    // Create some colors
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 399, 29, $white);
    // Replace path by your own font path
    $font = 'arial.ttf';
    $im_bg = @imagecreatefromjpeg($filePath);
    //imagejpeg($im,"New.jpeg", 100);
    list($width, $height) = getimagesize($filePath);
    $pixels_for_one_char = 16;
    $available_pixels = (int) $width ;
    //echo "ONE: ".pixels_for_one_char;
    //echo " TITLE::". $title;
    $title_length = strlen($title);
    $pixels_for_whole_title = $title_length*$pixels_for_one_char;
    if($pixels_for_whole_title > $available_pixels)
    {
    $no_of_lines = ceil($pixels_for_whole_title/$available_pixels);
    }
    else
    {
    $no_of_lines =1;
    }
    //echo "LINES".$no_of_lines ;
    $y = 70;
    $from=0;
    $chars_in_one_line = round($available_pixels/$pixels_for_one_char);
    $limit = $chars_in_one_line;
    $white = imagecolorallocate($im_bg, 255, 255, 255);
    $str = wordwrap($title,$chars_in_one_line,"#");
    $title_in_lines = explode("#",$str);

    for($i=0; $i<count($title_in_lines); $i++)
    {
    //echo "loop".$title_in_lines[$i];
    //$text_to_write = substr($title,$from,$limit);
    $total_pixels_for_title = strlen($title_in_lines[$i])*$pixels_for_one_char;
    $xPosition = ( $available_pixels - $total_pixels_for_title)/2 ;
    //$from = ($i+1)*$chars_in_one_line;
    //$limit = $from + $chars_in_one_line;
    imagettftext($im_bg, $title_fontsize, 0, $xPosition, $y, $title_fontColor, $font, $title_in_lines[$i]);
    imagettftext($im_bg, $title_fontsize, 0, ($xPosition), $y-1, $title_fontColor, $font, $title_in_lines[$i]);
    $y= $y+50;


    }
    ////////////// write subtitle//////////////////////
    $y = $y - 18;
    $pixels_for_one_char = 8;
    $available_pixels = (int) $width ;
    //echo "ONE: ".pixels_for_one_char;
    //echo " TITLE::". $title;
    $title_length = strlen($subtitle);
    $pixels_for_whole_title = $title_length*$pixels_for_one_char;
    if($pixels_for_whole_title > $available_pixels)
    {
    $no_of_lines = ceil($pixels_for_whole_title/$available_pixels);
    }
    else
    {
    $no_of_lines =1;
    }
    //echo "LINES".$no_of_lines ;

    $chars_in_one_line = round($available_pixels/$pixels_for_one_char);
    $limit = $chars_in_one_line;
    $white = imagecolorallocate($im_bg, 255, 255, 255);
    $str = wordwrap($subtitle,$chars_in_one_line,"#");
    $title_in_lines = explode("#",$str);

    for($i=0; $i<count($title_in_lines); $i++)
    {
    //echo "loop".$title_in_lines[$i];
    //$text_to_write = substr($title,$from,$limit);
    $total_pixels_for_title = strlen($title_in_lines[$i])*$pixels_for_one_char;
    $xPosition = ( $available_pixels - $total_pixels_for_title)/2 ;
    //$from = ($i+1)*$chars_in_one_line;
    //$limit = $from + $chars_in_one_line;
    imagettftext($im_bg, $subtitle_fontsize, 0, $xPosition, $y, $subtitle_fontColor, $font, $title_in_lines[$i]);
    imagettftext($im_bg, $subtitle_fontsize, 0, ($xPosition), $y-1, $subtitle_fontColor, $font, $title_in_lines[$i]);
    $y= $y+50;

    }

    ///////////////////////// write author/////////////
    //$xPosition = (int) $width / 4;
    $pixels_for_one_char = 16;
    $available_pixels = (int) $width ;
    $author_length = strlen($author);
    $total_pixels_for_author = $author_length*$pixels_for_one_char;
    $xPosition = ( $available_pixels - $total_pixels_for_author)/2 ;

    $yPosition = $height - 10;
    $path_parts = pathinfo($filePath);
    //echo "<br> Extension".$path_parts['extension'];


    imagettftext($im_bg, $author_fontsize, 0, $xPosition, $yPosition-89, $author_fontColor, $font, $author);
    imagettftext($im_bg, $author_fontsize, 0, ($xPosition), ($yPosition - 90), $author_fontColor, $font, $author);

    //header("Content-type: image/jpeg");
    imagejpeg($im_bg, $finalImage, 100);
    }// end write

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    function writeOnImage($filePath$title$author$subtitle$title_R$title_G$title_B$subtitle_R$subtitle_G$subtitle_B$title_fontsize$subtitle_fontsize$author_R$author_G$title_B$author_fontsize,$finalImage)
    {
    //$existingImage = substr($existingImage, 3);
    $im imagecreatetruecolor(9030);
    $title_fontColor imagecolorallocate($im$title_R$title_G$title_B);
    $subtitle_fontColor imagecolorallocate($im$subtitle_R$subtitle_G$subtitle_B);
    $author_fontColor imagecolorallocate($im$author_R$author_G$author_B);
    // Create some colors
    $white imagecolorallocate($im255255255);
    $grey imagecolorallocate($im128128128);
    $black imagecolorallocate($im000);
    imagefilledrectangle($im0039929$white);
    // Replace path by your own font path
    $font 'arial.ttf';
    $im_bg = @imagecreatefromjpeg($filePath);
    //imagejpeg($im,"New.jpeg", 100);
    list($width$height) = getimagesize($filePath);
    $pixels_for_one_char 16;
    $available_pixels = (int) $width ;
    //echo "ONE: ".pixels_for_one_char;
    //echo " TITLE::". $title;
    $title_length strlen($title);
    $pixels_for_whole_title $title_length*$pixels_for_one_char;
    if(
    $pixels_for_whole_title $available_pixels)
    {
    $no_of_lines ceil($pixels_for_whole_title/$available_pixels);
    }
    else
    {
    $no_of_lines =1;
    }
    //echo "LINES".$no_of_lines ;
    $y 70;
    $from=0;
    $chars_in_one_line round($available_pixels/$pixels_for_one_char);
    $limit $chars_in_one_line;
    $white imagecolorallocate($im_bg255255255);
    $str wordwrap($title,$chars_in_one_line,"#");
    $title_in_lines explode("#",$str);

    for(
    $i=0$i<count($title_in_lines); $i++)
    {
    //echo "loop".$title_in_lines[$i];
    //$text_to_write = substr($title,$from,$limit);
    $total_pixels_for_title strlen($title_in_lines[$i])*$pixels_for_one_char;
    $xPosition = ( $available_pixels $total_pixels_for_title)/;
    //$from = ($i+1)*$chars_in_one_line;
    //$limit = $from + $chars_in_one_line;
    imagettftext($im_bg$title_fontsize0$xPosition$y$title_fontColor$font$title_in_lines[$i]);
    imagettftext($im_bg$title_fontsize0, ($xPosition), $y-1$title_fontColor$font$title_in_lines[$i]);
    $y$y+50;


    }
    ////////////// write subtitle//////////////////////
    $y $y 18;
    $pixels_for_one_char 8;
    $available_pixels = (int) $width ;
    //echo "ONE: ".pixels_for_one_char;
    //echo " TITLE::". $title;
    $title_length strlen($subtitle);
    $pixels_for_whole_title $title_length*$pixels_for_one_char;
    if(
    $pixels_for_whole_title $available_pixels)
    {
    $no_of_lines ceil($pixels_for_whole_title/$available_pixels);
    }
    else
    {
    $no_of_lines =1;
    }
    //echo "LINES".$no_of_lines ;

    $chars_in_one_line round($available_pixels/$pixels_for_one_char);
    $limit $chars_in_one_line;
    $white imagecolorallocate($im_bg255255255);
    $str wordwrap($subtitle,$chars_in_one_line,"#");
    $title_in_lines explode("#",$str);

    for(
    $i=0$i<count($title_in_lines); $i++)
    {
    //echo "loop".$title_in_lines[$i];
    //$text_to_write = substr($title,$from,$limit);
    $total_pixels_for_title strlen($title_in_lines[$i])*$pixels_for_one_char;
    $xPosition = ( $available_pixels $total_pixels_for_title)/;
    //$from = ($i+1)*$chars_in_one_line;
    //$limit = $from + $chars_in_one_line;
    imagettftext($im_bg$subtitle_fontsize0$xPosition$y$subtitle_fontColor$font$title_in_lines[$i]);
    imagettftext($im_bg$subtitle_fontsize0, ($xPosition), $y-1$subtitle_fontColor$font$title_in_lines[$i]);
    $y$y+50;

    }

    ///////////////////////// write author/////////////
    //$xPosition = (int) $width / 4;
    $pixels_for_one_char 16;
    $available_pixels = (int) $width ;
    $author_length strlen($author);
    $total_pixels_for_author $author_length*$pixels_for_one_char;
    $xPosition = ( $available_pixels $total_pixels_for_author)/;

    $yPosition $height 10;
    $path_parts pathinfo($filePath);
    //echo "<br> Extension".$path_parts['extension'];


    imagettftext($im_bg$author_fontsize0$xPosition$yPosition-89$author_fontColor$font$author);
    imagettftext($im_bg$author_fontsize0, ($xPosition), ($yPosition 90), $author_fontColor$font$author);

    //header("Content-type: image/jpeg");
    imagejpeg($im_bg$finalImage100);
    }
    // end write 
    Sorry for the wierd reply.
    its so programmers can see the code properly and make it easier to notice any mistakes.
    also i remember seeing a snippit in this forum for watermarking (writing on images) in the snippet section.

    http://www.codingforums.com/showthread.php?t=72317

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Here is an example of how to center the text by getting the bounding box of the text:
    PHP Code:
    $text 'Some Text to Center';
    $color imagecolorallocate($image255255255); 
    $font 'arial.ttf'
    $fontSize 8;
    $ret imagettfbbox($fontSize0$font$text);
    imagettftext($image$fontSize0, ($imageWidth $ret[4]) / 212$color$font$text); 
    You can get $imageWidth from imagex() or getimagesize() if you're creating the image object from a file. The 12 in imagettftext() is the vertical offset, so you'll probably want to change that.

  • #4
    New Coder
    Join Date
    Jul 2007
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanx for the help.

    Thankyou rafiki and Inigoesdr for the response, i am able to solve my problem.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •