Problem with GD library in PHP

0

Good guys and girls, I want to print a certificate for a forum that is dictated where I study and research found the PHP GD library, I saw some tutorials and then the truth was "izi" I've been all day in this xx and I do not find the error first, I did not even have an image, I thought it was the resolution of the image, I changed it, I played with it, in the end I managed to show the image, but the main function that is to put the text on it is missing. A

Here I leave the code

<?php
      //Set the Content Type
      header("content-type: image/jpeg");


    $image = ImageCreateFromJPEG('certificado.jpg');
    $black = imagecolorallocate($image, 0, 0, 0);
    $fuente = "ARIALMT.ttf";
    $text = "Carlos";
    imagettftext($image, 500, 0, 1739, 1499, $black, $fuente, $text);
    $filename = "certificadoout.jpg";
    imagejpeg($image);

    imagedestroy($image);

There are some people who print the image with an img tag and remove the header, but the truth is that it's the same, I get the image shown but the text does not come out on it: / please, help D:

    
asked by Carlos Aponte 14.05.2017 в 06:13
source

1 answer

0

Let's see it in parts, first load the image:

$image = ImageCreateFromJPEG('certificado.jpg');

Then we tell the library to write a text about the image $image with a 500 point size , an angle of 0 degrees, in the coordinates 1739, 1499 strong>, that is black, that uses the file ttf ARIALMT.ttf that is in the directory where the script is executed and that says "Carlos".

imagettftext($image, 500, 0, 1739, 1499, imagecolorallocate($image, 0, 0, 0), "ARIALMT.ttf", "Carlos");

Then we export the image $ image to the browser:

imagejpeg($image);

At the end we liberate memory.

imagedestroy($image);

Then we should generate the correct headers for this file, but let's leave that aside. You need to focus on the 3 points I have highlighted:

  • Size: 500 is too big? it is possible that the text is printing outside the image. Try a size 20 to start, then you can scale it.
  • coordinates: coordinates may be out of range, try with a smaller number; 0, 0 prints the text in the upper left corner.
  • Source file: Not all PHP installations or this library contain source files, you must make sure that the file you are indicating exists in the file system. I recommend using the absolute route of it to avoid confusion regarding its location, eg. /var/www/fonts/ARIALMT.ttf
answered by 14.05.2017 в 17:35