Joomla PHP下载对话框动态图像无效


Joomla PHP Download Dialog dynamic image invalid

我是Joomla和PHP开发的新手,请耐心等待。

在测验后提取一些简单的数据来绘制证书图像。我需要用户点击链接并下载该图片。

再一次,我可以在Joomla之外找到一些工作,但不能在内部。我甚至还没有得到动态创建的映像,只遇到了一个静态映像的问题,即下载的文件无效。

以下是我在Joomla之外使用模板预览作为现有图像的一个脚本:

<?php
header("Content-type: image/png");  
header('Content-Disposition: attachment; filename="template_preview.png"');  
readfile('template_preview.png');  

下载后,图像是正确的。

然而,在我的Joomla文件中:

<?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);  

我得到一个无效的图像。这甚至还不是我绘制动态图像的部分:/

有什么想法吗?

我能从Joomla找到的唯一另一个线程就在这里,但不是完全相同的问题:强制在PHP中下载文件-在Joomla框架内下面还有一些例子:http://php.net/manual/en/function.header.php

提前感谢大家!

#############################################

部分解决方案适用于静态图像(感谢@Chris!),试图通过使动态图像工作

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;

和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 );

使用输出缓冲并将文件刷新到浏览器。可能还需要确保defined('_JEXEC')返回true。

<?php
require_once("includes/variables_certificate.php");
defined('_JEXEC') or die;
$filename = JPATH_SITE.'/templates/'.$this->template.'/template_preview.png';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filename));
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;