Back to Top

How to write Multiline Long text on image in PHP

How to write Multiline Long text on image in PHP?

Comments (10)

  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. There is some work to do.

    3. doesn’t work

  4. Hello,
    Thank you for this post !!! Saving time !
    Just a typo, replace text by string to print every pieces of text instead of printing the full text in each lines.
    $tb = imagettftext($image, $size, $angle, $xpos, $ypos, $color, $font, $text);
    to
    $tb = imagettftext($image, $size, $angle, $xpos, $ypos, $color, $font, $string);

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

  6. Devraj Shrestha says:

    How to add multiple line text on this image?

    $text = “Text goes here”;
    $width = 320;
    $height = 480;
    $text_length=10;
    $text = wordwrap($text, $text_length, “”, true);
    $text = str_replace(“”, “\n”, $text);
    $textImage = imagecreate($width, $height);
    $color = imagecolorallocate($textImage, 0, 0, 0);
    imagecolortransparent($textImage, $color);
    imagestring($textImage, 5, 10, 5, $text, 0xFFFFFF);
    $background = imagecreatefromjpeg(‘photo-1510832198440-a52376950479.jpg’);
    // Merge background image and text image layers
    imagecopymerge($background, $textImage, 15, 15, 0, 0, $width, $height, 100);
    $output = imagecreatetruecolor($width, $height);
    imagecopy($output, $background, 0, 0, 20, 13, $width, $height);
    ob_start();
    imagepng($output);
    printf(”, base64_encode(ob_get_clean()));

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Most Popular Posts

To learn MySQL PACK_KEYS with example

Posted on 9 years ago

Bhumi

Branching and Merging in Git

Posted on 6 years ago

Bhumi

Table layout using DIV tags

Posted on 11 years ago

Bhumi

Static Methods and Properties in PHP

Posted on 12 years ago

Bhumi

Overview of ECMAScript 6 new Features

Posted on 6 years ago

Bhumi