Write image with php

Asked

Viewed 2,377 times

5

I am trying to print online certificates dynamically filled with PHP. The code I’m using is:

class Certificado
{

    public $nome_para_certificado = '';
    public $modelo_de_certificado = '';

    function __construct($nome)
    {
        $this->nome_para_certificado = $nome;
    }

    public function gerar() 
    {
        header("Content-Type: image/jpeg");
        $texto = 'Certificamos que ' . $this->nome_para_certificado . ' participou do Evento nos dias 21, 22 e 23 de setembro de 2014 na Universidade Federal da Paraíba - UFPB';
        $img = imagecreatefromjpeg($this->modelo_de_certificado);
        $preto = imagecolorallocate($img, 0, 0, 0);
        $font_path = 'http://meu-site.com/custom/TravelingTypewriter.ttf';
        imagettftext($img, 50, 0, 10, 20, $preto, $font_path, $texto);
        // imagestring($img, 5, 300, 400, $texto, $preto);
        imagejpeg($img);
        imagedestroy($img);
    }
}

When I use imagestring I can print the text right but with the imagettftext it’s not working out.

I need to use the imagettftext to modify font size and type as well.

2 answers

5


Friend a very important detail that needs to be highlighted, you used the source path with http:

    $font_path = 'http://meu-site.com/custom/TravelingTypewriter.ttf';
    imagettftext($img, 50, 0, 10, 20, $preto, $font_path, $texto);

The correct one would be to use the absolute path that is on your server, assuming that on your server the path is:

  • If /etc/www/custom then use:

    $font_path = '/etc/www/custom/TravelingTypewriter.ttf';
    imagettftext($img, 50, 0, 10, 20, $preto, $font_path, $texto);
    
  • If /home/htdocs/custom then use:

    $font_path = '/home/htdocs/custom/TravelingTypewriter.ttf';
    imagettftext($img, 50, 0, 10, 20, $preto, $font_path, $texto);
    
  • If c:\wamp\custom then use:

    $font_path = 'c:/wamp/custom/TravelingTypewriter.ttf';
    imagettftext($img, 50, 0, 10, 20, $preto, $font_path, $texto);
    

The reason for this is explained in the documentation imagettftext:

Depending on the version of the GD library used in PHP, when fontfile does not start with / then the way of the . ttf will be added to the file name and the library will try to find this file name a source path defined in the library.

This means that if the path is relative and not absolute, instead of searching from the location of the script, it will search for the PATH set in the GD settings, then it probably thinks that http is a folder inside the system sources folder.

A trick that might work (note that in this case you need to remove the . ttf extension):

<?php
//Define a pasta do script atual como o caminho das fontes
putenv('GDFONTPATH=' . realpath('.'));

class Certificado
{

    public $nome_para_certificado = '';
    public $modelo_de_certificado = '';

    function __construct($nome)
    {
        $this->nome_para_certificado = $nome;
    }

    public function gerar() 
    {
        header("Content-Type: image/jpeg");
        $texto = 'Certificamos que ' . $this->nome_para_certificado . ' participou do Evento nos dias 21, 22 e 23 de setembro de 2014 na Universidade Federal da Paraíba - UFPB';
        $img = imagecreatefromjpeg($this->modelo_de_certificado);
        $preto = imagecolorallocate($img, 0, 0, 0);

        //Quando usar GDFONTPATH= não use a extensão .ttf
        $font_path = 'TravelingTypewriter';

        imagettftext($img, 50, 0, 10, 20, $preto, $font_path, $texto);
        imagejpeg($img);
        imagedestroy($img);
    }
}
  • William, thank you very much! Your tips have helped me a lot to solve the problem. A hug.

  • I think I did it right. Check it out? Hug.

4

look at this doodle here. I made the smallest changes possible.

<?php

class Certificado {
    public $nome_para_certificado = '';
    public $modelo_de_certificado = '';

    function __construct($nome, $modelo)
    {
        $this->nome_para_certificado = $nome;
        $this->modelo_de_certificado = $modelo;
    }

    public function gerar() 
    {
        header("Content-Type: image/jpeg");
        $texto = 'Certificamos que ' . $this->nome_para_certificado;
        $texto1 = 'participou do Evento nos dias 21, 22 e 23 de setembro de 2014 na';
        $texto2 = 'Universidade Federal da Paraíba - UFPB';
        $img = imagecreatefromjpeg($this->modelo_de_certificado);
        $preto = imagecolorallocate($img, 0, 0, 0);
        $font_path = 'arial.ttf';
        imagettftext($img, 12, 0, 10, 200, $preto, $font_path, $texto);
        imagettftext($img, 12, 0, 10, 230, $preto, $font_path, $texto1);
        imagettftext($img, 12, 0, 10, 250, $preto, $font_path, $texto2);
        // imagestring($img, 5, 300, 400, $texto, $preto);
        imagejpeg($img);
        imagedestroy($img);
    }   
}

$imagem = new Certificado("Edilson Samuel", "modelo.jpg");
$imagem->gerar();

?>

Taking as an example this image sample:

imagem

The printed result would be something like this:

inserir a descrição da imagem aqui

There are plenty of probabilities that the problem was generated because you didn’t define the value of $modelo_de_certificado as could also have been bad loading of the source.

When problems like this arise, you can also search for solutions on the PHP.net

  • Dear Edilson, thank you very much for your very well prepared and very didactic reply. His tips helped me solve it and the problem was exactly the source path, as Oce warned. I removed http and called the source from the relative path: $font_path = '.. /custom/Travelingtypewriter.ttf'. Thank you very much and congratulations for knowing so much about the subject. A hug.

  • Thank you, good luck.

  • 1

    Hi, Edilson, I rejected a suggestion of your editing especially for having put Windows and Ubuntu with code marking and also because had several other improvements that could be done. If you can, check this meta post: How we should format questions and answers? . . . Then I delete this comment that has nothing to do with your reply. Greetings!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.