0

I am trying to make a certificate generator with custom font, but I am unable to import font from it's directory.

Code is below

<?php
// (A) OPEN IMAGE
$img = imagecreatefromjpeg('cert.jpg');

// (B) WRITE TEXT
$white = imagecolorallocate($img, 255, 255, 255);
$txt = "Adnan Khan Akib";
$font = "arial.ttf"; 

$minus = strlen($txt)*70;

//1 word er jonno 70

// THE IMAGE SIZE
$width = imagesx($img)-$minus;
$height = imagesy($img);

// THE TEXT SIZE
$text_size = imagettfbbox(24, 0, $font, $txt);
$text_width = max([$text_size[2], $text_size[4]]) - min([$text_size[0], $text_size[6]]);
$text_height = max([$text_size[5], $text_size[7]]) - min([$text_size[1], $text_size[3]]);

// CENTERING THE TEXT BLOCK
$centerX = CEIL(($width - $text_width) / 2);
$centerX = $centerX<0 ? 0 : $centerX;
$centerY = CEIL(($height - $text_height) / 2);
$centerY = $centerY<0 ? 0 : $centerY;
imagettftext($img, 120, 0, $centerX, $centerY, $white, $font, $txt);

// (C) OUTPUT IMAGE
header('Content-type: image/jpeg');
imagejpeg($img);
imagedestroy($jpg_image);
?>

I have put arial.ttf on the same directory.

I tried,

$font = "arial.ttf"; 

and,

$font = realpath("arial.ttf"); 

also this,

$font = "./arial.ttf"; 

Error is

Warning: imagettftext(): Could not find/open font 
Akib Khan
  • 33
  • 6
  • Carefully read what is said about the **fontfile** parameter for [imagettfbbox()](https://www.php.net/manual/en/function.imagettfbbox.php). – KIKO Software Jan 29 '21 at 16:34
  • Does this answer your question? [Warning: imagettftext() \[function.imagettftext\]: Could not find/open font in /home/a2424901/public\_html/index.php on line 35](https://stackoverflow.com/questions/10366679/warning-imagettftext-function-imagettftext-could-not-find-open-font-in-ho) – adampweb Jan 29 '21 at 17:16

1 Answers1

0

Here is what I did.

Changed this line

$font = "arial.ttf"; 

to

$font = "./arial.ttf";

It worked.

I noticed just now that you have tried that too. Can you please check the permission and the name of the font file is correct?

I went through the logs and found another error in the last line

imagedestroy() expects parameter 1 to be resource, null given

Changing this

imagedestroy($jpg_image);

to

imagedestroy($img);

got rid of the error.

Vagabond
  • 877
  • 1
  • 10
  • 23