I'm new to Joomla and PHP dev so bear with me.
Pulling some simple data after a quiz to draw a certificate image. I need users to click on a link and download that image.
Once again, I can get something working outside of Joomla but not within. I haven't even gotten to the dynamically created image yet and running into issues with just a static one, namely the file downloaded is invalid.
Here's one script I was able to get working outside of Joomla, using the template preview as an existing image:
<?php
header("Content-type: image/png");
header('Content-Disposition: attachment; filename="template_preview.png"');
readfile('template_preview.png');
When downloaded the image is correct.
However, in my Joomla file:
<?php defined('_JEXEC') or die;
require_once("includes/variables_certificate.php");
header("Content-type: image/png");
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="template_preview.png"');
$filename = JPATH_SITE.'/templates/'.$this->template.'/template_preview.png';
readfile($filename);
I get an invalid image. This isn't even the part where I'm pulling in the dynamic image yet :/
Any ideas?
The only other thread I could find from Joomla was here and not quite the same issue: Forcing file download in PHP - inside Joomla framework Also followed a bunch of examples in here: http://php.net/manual/en/function.header.php
Thanks all in advance!
#############################################
Partial Solution working for static image (thanks @Chris!), trying to get the dynamic image to work though
download.php:
<?php
require_once("includes/variables_certificate.php");
defined('_JEXEC') or die;
// $filename = JPATH_SITE.'/templates/'.$this->template.'/template_preview.png'; //works
$filename = JURI::root().'index.php?tmpl=certgenerator&quizmod='.$quizmod; //Not working
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="certificate.png"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
readfile($filename);
exit;
and the certgenerator.php:
defined('_JEXEC') or die;
require_once("includes/variables_certificate.php");
$image_file = JPATH_SITE.'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 = JPATH_SITE.'templates/'.$this->template.'/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 );