代码之家  ›  专栏  ›  技术社区  ›  Gisto

在Joomla中调用字体时,图像不会显示

  •  0
  • Gisto  · 技术社区  · 13 年前

    我是Joomla dev和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,没有扩展,有扩展,上面链接的一堆东西,结果都一样。有什么想法吗?我在猜测(并希望!)什么愚蠢的事情?

    2 回复  |  直到 13 年前
        1
  •  2
  •   Søren Beck Jensen    13 年前

    在PHP中,当使用文件时,如果使用JURI,则应该使用JPATH_。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。但实际上,这是错误的做法,因为它是一个本地文件。

        2
  •  0
  •   Gisto    13 年前

    道路是我生存的祸根!

    $font_file = JPATH_SITE.'/templates/'.$this->template.'/images/font/FelipaRegular.ttf';  
    
    推荐文章