php gd:图片无法显示,因为其中包含错误 [英] php gd : image cannot be displayed because it contains errors

查看:64
本文介绍了php gd:图片无法显示,因为其中包含错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个错误让我很生气:

如果我将

require_once'../class/myclass.class.php'; 

显示图像. 如果我取消对调用myclass.class.php的行的注释,则显示以下消息:

the image is displayed. If I un-comment my line calling myclass.class.php, I have this message:

"The image "http://..." cannot be displayed because it contains errors."

我的代码很简单:

myclass.class.php:

myclass.class.php :

<?php    
class myclass {
  public function getPanelData( $model ){
    $aFieldsData = array(
      'PAN35'=>array(
        'col'=>1,
        'row'=>3,
        'v-font'=>10,
        'v-marge-top'=>0,
        'v-marge-right'=>1,
        'v-marge-bottom'=>0,
        'v-marge-left'=>1
      )
    );
    if( key_exists($model, $aFieldsData) )
      return $aFieldsData[$model];
    else
      return false;
    }
  }
?>

img.inc.php:

img.inc.php:

<?php
  session_start();
  require_once('myfunctions.inc.php');
  require_once('../class/myclass.class.php');
  $oData = new myclass();
  header('Content-Type: image/png');
  $sPanelModel = $_SESSION['produit'];
  $sEtiquette = '../img/etiquettes/label_'.$sPanelModel.'_preview.png';
  $rImg = imagecreatefrompng($sEtiquette);
  imagepng($rImg);
  imagedestroy($rImg);
?>

注意:如果我注释require_once调用myclass.class.php,则此代码有效. 调用functions.inc.php起作用(仅少数函数).

Note: This code works if I comment require_once calling myclass.class.php. Calling functions.inc.php works (only few functions).

tree :
/
 + class
   + myclass.class.php
 + inc
   + functions.inc.php
   + img.inc.php
 + images
   + etiquettes

推荐答案

如果确实通过取消对myclass.class.php的require()进行注释而起作用,那么最可能的原因是此文件包含空白行(空格) )在<?php之前或?>之后.这会将Ascii字符添加到图像的输出,或者在您的header()语句中插入php错误消息(无法发送标头),从而使您的文件混乱. 但是,正如我在评论中提到的那样,如果您的唯一目的是输出图片,则可以使用readfile()而不是创建图像实例. 希望能有所帮助, 斯蒂芬

If it is true that it works by uncommenting the require() for myclass.class.php, then the most likely cause is this file contains blank lines (whitespace) before the <?php or after the ?>. This would add Ascii characters to the output of the image, or insert a php error message (Headers could not be sent) on your header() statement and thus mess up your file. However, as I mentioned in my comment, if your sole purpose is to output the picture you could use readfile() instead of creating an image instance. Hope that helps, Stefan

这篇关于php gd:图片无法显示,因为其中包含错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆