php gd : image cannot be displayed because it contains errors

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 17:43:01

问题


I'm very annoyed with this error :

If I comment out the

require_once'../class/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."

My code is simple:

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:

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

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

回答1:


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



来源:https://stackoverflow.com/questions/10297900/php-gd-image-cannot-be-displayed-because-it-contains-errors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!