Write in image with php

5

I'm trying to print online certificates dynamically populated 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 imagettftext is not working.

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

    
asked by anonymous 20.09.2015 / 10:58

2 answers

4

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, supposing 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 imagettftext documentation:

  Depending on the version of the GD library used in PHP, when fontfile does not start with / then the .ttf path will be appended to the file name and the library will try to look up that file name for a font path defined in the library.

This means that if the path is relative rather than absolute, instead of looking at it from the location of the script, it will look for the PATH defined in the GD settings, so it probably thinks http is a folder inside the system fonts folder.

A trick that might work (note that in this case it is necessary 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);
    }
}
    
20.09.2015 / 15:06
4

look at this scrawl here. I made the fewest possible changes.

<?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 this sample image as an example:

Theprintedoutputwouldlooksomethinglikethis:

Thereisagoodchancethattheproblemwasgeneratedbecauseyoudidnotdefinethevalueof$modelo_de_certificadoasitcouldhavebeenthebadloadingofthesource.

Whenproblemslikethisarise,youcanalsosearchforsolutionsonthe PHP.net page

p>     
20.09.2015 / 14:24