0

我是 Joomla 开发和 php 的新手,所以如果我在做一些愚蠢的事情,请告诉我!

我正在创建要在用户完成带有姓名、日期等的测验后打印的证书。我已经显示了要显示的图像和文本,但是当我添加字体时,什么都没有显示。

这是我正在使用和工作的基本脚本(背景图像和字体工作):

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

当我试图在 Joomla 中应用它时,问题就开始了。我在这样的模板中调用它:

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

和文件生成器(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 );

参考:

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

我已经尝试过 imagettftext 与 imagefttext,没有扩展名,有扩展名,上面链接中的一堆东西,结果都是一样的。有任何想法吗?我在猜测(并希望!)一些愚蠢的事情?

4

2 回答 2

2

在您的 PHP 中处理文件时,您应该使用 JPATH_ 而不是 JURI。JURI 用于生成 http URI。

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

应该

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

不确定这是否是问题所在,并且您的代码可能会正常工作,因为我偷偷怀疑 imagecreatefrompng() 接受 http。但实际上这是错误的方法,因为它是一个本地文件。

于 2012-07-27T13:56:41.483 回答
0

路径是我存在的祸根!

$font_file = JPATH_SITE.'/templates/'.$this->template.'/images/font/FelipaRegular.ttf';  
于 2012-07-27T13:07:21.087 回答