我是Joomla和PHP dev的新手,所以请耐心等待。
在测验后拉出一些简单的数据来绘制证书图像。我需要用户点击链接并下载该图像。
再一次,我可以在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找到的唯一一个其他主题就在这里而不是同一个问题:Forcing file download in PHP - inside Joomla framework 此外还有一些示例: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 );
答案 0 :(得分:2)
使用输出缓冲并将文件刷新到浏览器。可能还要确保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;