tags:

views:

166

answers:

2

Recently i worked in project. there i need to a resize a picture and i use the following class.

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;   
   }      
}

when i use the class it show a warning message like bellow

Warning: getimagesize(seeker/SeekerPhoto/so.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\xampp\htdocs\job\insphoto.php on line 11

Warning: images): supplied argument is not a valid Image resource in C:\xampp\htdocs\job\insphoto.php on line 60

Warning: imagesy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\job\insphoto.php on line 64

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\xampp\htdocs\job\insphoto.php on line 87

How can i avoid this kind of warning message. Thank you Arif..

+5  A: 

You should check first if the file exists (is_file function) and is readable (is_readable function).

Gumbo
A: 

Your methods have no error handling what so ever.

E.g. the load() method. If the file doesn't exist in the first place it makes little sense to get information from it via getimagesize(). And if getimagesize() fails it makes even less sense to use its return value to test for certain image types, and so on and on.

an (untested) example:

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;
}

VolkerK