0
I am working on a wordpress project, and I need to generate an image dynamically, using PHP’s imagecreatefromjpeg() function in addition to the imagettftext() function to add custom text to the image, with fonts of my choice.
The script I developed works perfectly, but when I try to use it inside the wordpress page I can’t find the font.
Code example (works):
<?php
/*---------------------------------------*/
// Header informando que é uma imagem JPEG
header( 'Content-type: image/jpeg' );
// Carregar imagem já existente no servidor
$imagem = imagecreatefromjpeg( "cupomFinal.jpg" );
$cor = imagecolorallocate($imagem, 119, 119, 119);
$corTitle = imagecolorallocate($imagem, 163, 72, 177);
// Texto que será escrito na imagem
$nome = "Cliente: Vinicius Ribeiro";
$email = "Email: [email protected]";
$contato = "Contato: 31-0000-0000";
$codigo = "Código:";
$codigoDesconto = "00000-00-000-2015";
$font = 'RobotoCondensed-Regular.ttf';
$fontCodigo = 'impact.ttf';
// INSERE OS TEXTOS NA IMAGEM COM AS FONTES ESCOLHIDAS
imagettftext($imagem, 16, 0, 390, 140, $cor, $font, $nome);
imagettftext($imagem, 13, 0, 390, 170, $cor, $font, $email);
imagettftext($imagem, 13, 0, 390, 200, $cor, $font, $contato);
imagettftext($imagem, 12, 0, 50, 170, $corTitle, $fontCodigo, $codigo);
imagettftext($imagem, 18, 0, 50, 200, $corTitle, $fontCodigo, $codigoDesconto);
// eEnvia a imagem para o borwser ou arquivo
imagejpeg( $imagem, NULL, 90);
imagedestroy($imagem);
?>
The above code has been tested separately and worked perfectly, but when I try to implement inside wordpress, the font is never found!
Since the "header" command indicates that the page will be an image, just comment to view the errors. So I visualized the following return:
I have tried to use several paths to the source and all without success.
In some attempts I used the function imagestring() which also writes in the image, but without the custom font, and the script worked perfectly with the syntax below:
imagestring($imagem, 5, 390, 170, $email, $cor);
But it would be extremely important that I use the custom source.
Thanks in advance for the help.
Hug.
How you are importing the source?
– Joerison Silva
@Joerisonsilva the font is added to the project folder, and called through the variables $font and $fontCodigo.
– Vinícius Ribeiro
Trying new possibilities and with the help of this question: (http://translate.googleusercontent.com/translate_c?depth=1&hl=pt-BR&prev=search&rurl=translate.google.com&sl=en&u=http://stackoverflow.com/questions/17489573/invalid-font-filename-imagettfbox&usg=Alkjrhikdfhamzcogwkst-Yyr3och0sqrg) . I used the local path within the source search variable, so: C: wamp www site project wp-content themes project Impact.ttf" Thank you all.
– Vinícius Ribeiro