Search code examples
phpjoomlajoomla2.5joomla1.6imagettftext

Image doesn't show up when font called within Joomla


I'm new to Joomla dev and php so if I'm doing something stupid please let me know!

I'm creating a certificate to be printed after a user completes a quiz with their name, date, etc. I've gotten the image to show up and text, but when I add the font nothing shows up.

Here's the base script I'm using and works (background image and font working):

$image_file = 'images/certificate_page.png';
$my_img = imagecreatefrompng ($image_file);
$font_size_big = 24;
$text_colour = imagecolorallocate( $my_img, 0, 0, 0 );
$font_file = 'FelipaRegular.ttf';  

imagefttext( $my_img, $font_size_big, 0, 5, 75, $text_colour, $font_file, "test with font"); 
imagestring( $my_img, 4, 30, 25, "test without font", $text_color );

header( "Content-type: image/png" );
imagepng( $my_img );

imagecolordeallocate( $text_color );
imagedestroy( $my_img );

When I tried to apply it within Joomla the problems started. I'm calling it from within a template like this:

<img src="<?php echo JURI::root().'index.php?tmpl=certgenerator&quizmod='.$quizmod.'' ?>" />

and the file generator (certgenerator.php):

defined('_JEXEC') or die;
require_once("includes/variables_certificate.php");

$image_file = JURI::root().'templates/'.$this->template.'/images/certificate_page.png'; 
$my_img = imagecreatefrompng ($image_file);
$font_size_big = 24;
$text_color = imagecolorallocate( $my_img, 0, 255, 0 );
$font_file = 'FelipaRegular.ttf';  

imagefttext( $my_img, $font_size_big, 0, 55, 75, $text_color, $font_file, "why u no work"); //if commented out image displays but not font obviously, as it's written now it returns a blank page
imagestring( $my_img, 4, 30, 25, "works", $text_color ); //works but doesn't include font

header( "Content-type: image/png" );
imagepng( $my_img );

imagecolordeallocate( $text_color );
imagedestroy( $my_img );

Reference:

http://php.net/manual/en/function.imagettftext.php

I've tried imagettftext vs imagefttext, no extension, with extension, bunch of stuff from the link above, all the same result. Any ideas? I'm guessing (and hoping!) something stupid?


Solution

  • In your PHP when working with files you should use JPATH_ instead if JURI. JURI is used for generating http URIs.

    $image_file = JURI::root().'templates/'.$this->template.'/images/certificate_page.png'; 
    

    Should be

    $image_file = JPATH_SITE.'/templates/'.$this->template.'/images/certificate_page.png'; 
    

    Not sure if this is the problem, and your code may well work as I have a sneaky suspicion that imagecreatefrompng() accepts http. But really it is the wrong way to go about it as it is a local file.