我怎样才能避免php中的getimage()警告消息

时间:2009-06-06 07:44:24

标签: php

最近我在项目中工作。我需要调整图片大小,我使用下面的课程。

class SimpleImage 
{  
   var $image;
   var $image_type; 
   function load($filename) 
   {      
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if($this->image_type == IMAGETYPE_JPEG) 
      {
         $this->image = imagecreatefromjpeg($filename);
      } 
      elseif( $this->image_type == IMAGETYPE_GIF ) 
      {
         $this->image = imagecreatefromgif($filename);
      } 
      elseif( $this->image_type == IMAGETYPE_PNG ) 
      {
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) 
   {
      if( $image_type == IMAGETYPE_JPEG ) 
      {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) 
      {
         imagegif($this->image,$filename);         
      } elseif( $image_type == IMAGETYPE_PNG ) 
      {
         imagepng($this->image,$filename);
      }   
      if( $permissions != null) 
      {
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) 
   {
      if( $image_type == IMAGETYPE_JPEG ) 
      {
         imagejpeg($this->image);
      } 
      elseif( $image_type == IMAGETYPE_GIF ) 
      {
         imagegif($this->image);         
      } 
      elseif( $image_type == IMAGETYPE_PNG ) 
      {
         imagepng($this->image);
      }   
   }
   function getWidth() 
   {
      return imagesx($this->image);
   }
   function getHeight() 
   {      
      return imagesy($this->image);
   }
   function resizeToHeight($height) 
   {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
   function resizeToWidth($width) 
   {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
   function scale($scale) 
   {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100; 
      $this->resize($width,$height);
   }
   function resize($width,$height) 
   {      
      $new_image = imagecreatetruecolor($width, $height);      
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;   
   }      
}

当我使用该类时,它会显示一条警告消息,如bellow

  

警告:   和getimagesize(导引头/ SeekerPhoto / so.jpg)   [function.getimagesize]:失败了   open stream:没有这样的文件或目录   在C:\ xampp \ htdocs \ job \ insphoto.php上   第11行

     

警告:图片):提供的参数是   不是有效的图像资源   C:\ xampp \ htdocs \ job \ insphoto.php on   第60行

     

警告:imagesy():提供的参数   不是有效的图像资源   C:\ xampp \ htdocs \ job \ insphoto.php on   第64行

     

警告:imagecopyresampled():   提供的参数不是有效的图像   资源   C:\ xampp \ htdocs \ job \ insphoto.php on   第87行

我如何避免这种警告信息。谢谢Arif ..

2 个答案:

答案 0 :(得分:5)

您应首先检查文件是否存在(is_file function)且可读(is_readable function)。

答案 1 :(得分:0)

你的方法没有任何错误处理方式。

E.g。 load()方法。如果文件首先不存在,那么通过getimagesize()获取信息是没有意义的。如果getimagesize()失败,那么使用其返回值来测试某些图像类型就更没意义了,等等。

(未经测试的)示例:

function load($filename) {
    if ( !is_readable($filename) ) {
        throw new ErrorException("file not readable");
    }
    else if ( false===($image_info=getimagesize($filename)) ) {
        throw new ErrorException("unable to get informations from image file");
    }
    //
    switch( $image_info[2] ) {
        case IMAGETYPE_JPEG:
            $fn = 'imagecreatefromjpeg';
            break;
        case IMAGETYPE_GIF:
            $fn = 'imagecreatefromgif';
            break;
        case IMAGETYPE_PNG:
            $fn = 'imagecreatefrompng';
            break;
        default:
            throw new ErrorException("unknown image type");
    }
    $this->image = $fn($filename);
    if ( false===$this->image ) {
        throw new ErrorException("$fn failed");
    }
    $this->image_type = $image_info[2];
    return $this->image_type;
}