Advertisement
Guest User

size image

a guest
Dec 7th, 2012
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.48 KB | None | 0 0
  1. class picture {
  2.    
  3.     private $image_file;
  4.    
  5.     public $image;
  6.     public $image_type;
  7.     public $image_width;
  8.     public $image_height;
  9.    
  10.    
  11.     public function __construct($image_file) {
  12.         $this->image_file=$image_file;
  13.         $image_info = getimagesize($this->image_file);
  14.         $this->image_width = $image_info[0];
  15.         $this->image_height = $image_info[1];
  16.         switch($image_info[2]) {
  17.             case 1: $this->image_type = 'gif'; break;//1: IMAGETYPE_GIF
  18.             case 2: $this->image_type = 'jpeg'; break;//2: IMAGETYPE_JPEG
  19.             case 3: $this->image_type = 'png'; break;//3: IMAGETYPE_PNG
  20.             case 4: $this->image_type = 'swf'; break;//4: IMAGETYPE_SWF
  21.             case 5: $this->image_type = 'psd'; break;//5: IMAGETYPE_PSD
  22.             case 6: $this->image_type = 'bmp'; break;//6: IMAGETYPE_BMP
  23.             case 7: $this->image_type = 'tiffi'; break;//7: IMAGETYPE_TIFF_II (порядок байт intel)
  24.             case 8: $this->image_type = 'tiffm'; break;//8: IMAGETYPE_TIFF_MM (порядок байт motorola)
  25.             case 9: $this->image_type = 'jpc'; break;//9: IMAGETYPE_JPC
  26.             case 10: $this->image_type = 'jp2'; break;//10: IMAGETYPE_JP2
  27.             case 11: $this->image_type = 'jpx'; break;//11: IMAGETYPE_JPX
  28.             case 12: $this->image_type = 'jb2'; break;//12: IMAGETYPE_JB2
  29.             case 13: $this->image_type = 'swc'; break;//13: IMAGETYPE_SWC
  30.             case 14: $this->image_type = 'iff'; break;//14: IMAGETYPE_IFF
  31.             case 15: $this->image_type = 'wbmp'; break;//15: IMAGETYPE_WBMP
  32.             case 16: $this->image_type = 'xbm'; break;//16: IMAGETYPE_XBM
  33.             case 17: $this->image_type = 'ico'; break;//17: IMAGETYPE_ICO
  34.             default: $this->image_type = ''; break;
  35.         }
  36.         $this->fotoimage();
  37.     }
  38.    
  39.     private function fotoimage() {
  40.         switch($this->image_type) {
  41.             case 'gif': $this->image = imagecreatefromgif($this->image_file); break;
  42.    case 'jpg': $this->image = imagecreatefromjpeg($this->image_file); break;
  43.             case 'jpeg': $this->image = imagecreatefromjpeg($this->image_file); break;
  44.             case 'png': $this->image = imagecreatefrompng($this->image_file); break;
  45.         }
  46.     }
  47.    
  48.     public function autoimageresize($new_w, $new_h) {
  49.         $difference_w = 0;
  50.         $difference_h = 0;
  51.         if($this->image_width < $new_w && $this->image_height < $new_h) {
  52.             $this->imageresize($this->image_width, $this->image_height);
  53.         }
  54.         else {
  55.             if($this->image_width > $new_w) {
  56.                 $difference_w = $this->image_width - $new_w;
  57.             }
  58.             if($this->image_height > $new_h) {
  59.                 $difference_h = $this->image_height - $new_h;
  60.             }
  61.                 if($difference_w > $difference_h) {
  62.                     $this->imageresizewidth($new_w);
  63.                 }
  64.                 elseif($difference_w < $difference_h) {
  65.                     $this->imageresizeheight($new_h);
  66.                 }
  67.                 else {
  68.                     $this->imageresize($new_w, $new_h);
  69.                 }
  70.         }
  71.     }
  72.    
  73.     public function percentimagereduce($percent) {
  74.         $new_w = $this->image_width * $percent / 100;
  75.         $new_h = $this->image_height * $percent / 100;
  76.         $this->imageresize($new_w, $new_h);
  77.     }
  78.    
  79.     public function imageresizewidth($new_w) {
  80.         $new_h = $this->image_height * ($new_w / $this->image_width);
  81.         $this->imageresize($new_w, $new_h);
  82.     }
  83.    
  84.     public function imageresizeheight($new_h) {
  85.         $new_w = $this->image_width * ($new_h / $this->image_height);
  86.         $this->imageresize($new_w, $new_h);
  87.     }
  88.    
  89.     public function imageresize($new_w, $new_h) {
  90.         $new_image = imagecreatetruecolor($new_w, $new_h);
  91.         imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $new_w, $new_h, $this->image_width, $this->image_height);
  92.         $this->image_width = $new_w;
  93.         $this->image_height = $new_h;
  94.         $this->image = $new_image;
  95.     }
  96.    
  97.     public function imagesave($image_type='jpeg', $image_file=NULL, $image_compress=100, $image_permiss='') {
  98.         if($image_file==NULL) {
  99.             switch($this->image_type) {
  100.     case 'bmp': header("Content-type: image/bmp"); break;
  101.                 case 'gif': header("Content-type: image/gif"); break;
  102.                 case 'jpeg': header("Content-type: image/jpeg"); break;
  103.     case 'jpg': header("Content-type: image/jpeg"); break;
  104.                 case 'png': header("Content-type: image/png"); break;
  105.             }
  106.         }
  107.         switch($this->image_type) {
  108.    case 'bmp': imagebmp($this->image, $image_file); break;
  109.             case 'gif': imagegif($this->image, $image_file); break;
  110.             case 'jpeg': imagejpeg($this->image, $image_file, $image_compress); break;
  111.    case 'jpg': imagejpeg($this->image, $image_file, $image_compress); break;
  112.             case 'png': imagepng($this->image, $image_file); break;
  113.         }
  114.         if($image_permiss != '') {
  115.             chmod($image_file, $image_permiss);
  116.         }
  117.     }
  118.    
  119.     public function imageout() {
  120.         imagedestroy($this->image);
  121.     }
  122.    
  123.     public function __destruct() {
  124.        
  125.     }
  126.    
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement