imagettftext() is Not Working even after activating GD library

120 Views Asked by At

I am able to see the image result with imagemerage successfully but imagettftext is not working please help me to solve this issue.

header('Content-type: image/jpeg');

$dest = imagecreatefromjpeg('ITI_card.jpg');

        $filename = 'students_img/' . $student_photo;
        $source = imagecreatefromjpeg($filename);

        list($width, $height) = getimagesize($filename);
        $newwidth = 230;
        $newheight = 275;
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

        $color = imagecolorallocate($dest, 0x00, 0x00, 0x00);

        $font_path = 'arial.ttf';

        imagettftext($dest, 25, 0, 266, 182, $color, $font_path, $name);
        imagettftext($dest, 25, 0, 266, 232, $color, $font_path, $fathername);
        imagettftext($dest, 25, 0, 266, 276, $color, $font_path, $gender);
        imagettftext($dest, 25, 0, 266, 322, $color, $font_path, $branch);
        imagettftext($dest, 25, 0, 266, 367, $color, $font_path, $academic_year);
        imagettftext($dest, 25, 0, 266, 412, $color, $font_path, $roll_no);
        imagettftext($dest, 25, 0, 266, 460, $color, $font_path, $dob);
        imagettftext($dest, 25, 0, 266, 501, $color, $font_path, $address);
        imagettftext($dest, 25, 0, 266, 532, $color, $font_path, $address1);
        imagettftext($dest, 25, 0, 266, 595, $color, $font_path, $contact);

        imagecopymerge($dest, $thumb, 761, 160, 0, 0, 230, 275, 100);

       imagejpeg($dest);
1

There are 1 best solutions below

0
Ken Lee On

Just make sure that

  1. the font file (arial.ttf) exists in your server
  2. the variables $student_photo, $name, etc are properly set.

then there should be no problem.

Please try the following code: (1.jpg is a sample picture, 2.jpg is a green color picture. I have changed the text to use RED color to make it clearer to be seen)

<?php

header('Content-type: image/jpeg');
 $dest = imagecreatefromjpeg('1.jpg');

            $filename = '2.jpg';
            $source = imagecreatefromjpeg($filename);

            list($width, $height) = getimagesize($filename);
            $newwidth = 230;
            $newheight = 275;
            $thumb = imagecreatetruecolor($newwidth, $newheight);
            imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

            $color = imagecolorallocate($dest, 0xFF, 0x00, 0x00);

            $font_path = 'arial.ttf';
            $test="TESTING";

            imagettftext($dest, 25, 0, 266, 182, $color, $font_path, $test);


            imagecopymerge($dest, $thumb, 761, 160, 0, 0, 230, 275, 100);
            imagejpeg($dest);
?>

A live working example is at

http://www.createchhk.com/SO/testSO21Nov2021.php