Jeff PHP framework  0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
image.class.php
Go to the documentation of this file.
00001 <?php
00023 class image {
00024  
00028         private $_image;
00029 
00033         private $_type;
00034  
00041         public function load($filepath) {
00042         
00043                 $image_info = getimagesize($filepath);
00044                 $this->_type = $image_info[2];
00045            
00046                 if($this->_type == IMAGETYPE_JPEG)
00047                         $this->_image = imagecreatefromjpeg($filepath);
00048                 elseif($this->_type == IMAGETYPE_GIF)
00049                         $this->_image = imagecreatefromgif($filepath);
00050                 elseif($this->_type == IMAGETYPE_PNG)
00051                         $this->_image = imagecreatefrompng($filepath);
00052         }
00053 
00059         public function type() {
00060                 return $this->_type;
00061         }
00062 
00072         public function save($filepath, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
00073  
00074                 if($image_type == IMAGETYPE_JPEG)
00075                         imagejpeg($this->_image, $filepath, $compression);
00076                 elseif($image_type == IMAGETYPE_GIF)
00077                         imagegif($this->_image, $filepath);
00078                 elseif($image_type == IMAGETYPE_PNG)
00079                         imagepng($this->_image, $filepath);
00080                 if($permissions != null)
00081                         chmod($filepath, $permissions);
00082 
00083         }
00084 
00091         public function output($image_type=IMAGETYPE_JPEG) {
00092  
00093                 if($image_type == IMAGETYPE_JPEG )
00094                         imagejpeg($this->_image);
00095                 elseif($image_type == IMAGETYPE_GIF)
00096                         imagegif($this->_image);
00097                 elseif($image_type == IMAGETYPE_PNG)
00098                         imagepng($this->_image);
00099 
00100         }
00101 
00107         public function getWidth() {
00108  
00109                 return imagesx($this->_image);
00110 
00111         }
00112         
00118         public function getHeight() {
00119  
00120                 return imagesy($this->_image);
00121 
00122         }
00123 
00133         public function resizeToHeight($height, $opts=null) {
00134  
00135                 $enlarge = gOpt($opts, "enlarge", false);
00136 
00137                 if($enlarge || $height<$this->getHeight()) {
00138                         $ratio = $height / $this->getHeight();
00139                         $width = $this->getWidth() * $ratio;
00140                 }
00141                 else {
00142                         $height = $this->getHeight();
00143                         $width = $this->getWidth();
00144                 }
00145 
00146                 $this->resize($width,$height);
00147 
00148         }
00149  
00159         public function resizeToWidth($width, $opts=null) {
00160         
00161                 $enlarge = gOpt($opts, "enlarge", false);
00162 
00163                 if($enlarge || $width<$this->getWidth()) {
00164                         $ratio = $width / $this->getWidth();
00165                         $height = $this->getheight() * $ratio;
00166                 }
00167                 else {
00168                         $height = $this->getHeight();
00169                         $width = $this->getWidth();
00170                 }
00171 
00172                 $this->resize($width,$height);
00173 
00174         }
00175 
00182         public function scale($scale) {
00183       
00184                 $width = $this->getWidth() * $scale/100;
00185                 $height = $this->getheight() * $scale/100;
00186                 $this->resize($width,$height);
00187 
00188         }
00189  
00190 
00198         public function resize($width, $height) {
00199         
00200                 $new_image = imagecreatetruecolor($width, $height);
00201                 imagecopyresampled($new_image, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
00202                 $this->_image = $new_image;
00203 
00204         }      
00205  
00206 }
00207 
00208 ?>