Back to Top

How to write Multiline Long text on image in PHP

How to write Multiline Long text on image in PHP?

Comments (7)

  1. Very information and relevant. Thanks for explaining.

  2. Gurmeet Singh says:

    Kindly Check, the example does not work.
    It says, Too few arguments.
    Kindly check and please upload the working code as I need it.

    1. Hi, I have updated code. Please check now

  3. Chethan R T says:

    Hey Bhumi,
    check my code.
    this is simple code compared to yours. In case if you find any flaws do let me know

    This is my code which works:

    function textWithImage($text){
    $imagePath=”/images/fb-share-bg.jpg”;
    $im = imagecreatefromjpeg($imagePath);
    $txtColor = imageColorAllocate($im, 255, 255, 255);
    $font=”/fonts/OpenSans-Bold.ttf”;
    $description=$text;
    $in = wordwrap($description,50,”|”,true);
    $st = explode(“|”,$in);
    //$inStrArr=str_split($description,60);
    $inStrArr=$st;
    $addOnYVal=0;
    foreach($inStrArr as $key=>$value){
    if($key!=0){
    $addOnYVal+=40;
    }
    list($x, $y) = pc_ImageTTFCenter($im, $value, $font, 20,$addOnYVal);
    ImageTTFText($im,20,0, $x, $y, $txtColor, $font, $value);
    }

    $newImagename=”/uploads/”.$img;
    imagejpeg($im, $newImagename,100);

    } // Enf of textWithImage function

    function pc_ImageTTFCenter($image, $text, $font, $size, $addOnYVal) {
    // find the size of the image
    $xi = ImageSX($image);
    $yi = ImageSY($image);

    // find the size of the text
    $box = ImageTTFBBox($size, $angle, $font, $text);

    $xr = abs(max($box[2], $box[4]));
    $yr = abs(max($box[5], $box[7]));

    // compute centering
    $x = intval(($xi – $xr) / 2);
    $y = intval(($yi + $yr) / 2) + + $addOnYVal;;

    return array($x, $y);
    }

    // Calling function
    $text = “This is to center alignn the text on image”;

    textWithImage($text);

    // Output: Image will be saved in the upload folder

    1. Chethan, thanks for sharing your code!!

    2. doesn’t work

  4. Function does not create seperate text for each line…

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

The Readers’ Poll – June 2013

Posted on 10 years ago

Bhumi