Advertisement
Guest User

Bartlomiej Pohl

a guest
Feb 22nd, 2008
1,482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.76 KB | None | 0 0
  1. <?php
  2. /* Classname: imageResizer
  3.  * Filename: class.imageResizer.php
  4.  * Author: Bartlomiej Pohl
  5.  * Description: class for Image resizing
  6.  */
  7.  
  8. class imageResizer {
  9.     public $accepted_types = array();                   //Array with accepted types. Contains the mime_type, extension and function that is called to load image
  10.     public $error_code = 0;                             //Errorcode. 0: no errors.
  11.     public $error_ini = "../config/errors.ini";         //Inifile from which all errormessages are loaded.
  12.     public $max_file_size = 0;                          //Sets the Maximum Filesize for sourcefiles to resize
  13.    
  14.     /* constructor: Params
  15.      *  @images_path        :   String. Path were resized Images are copied to
  16.      *  @load_standard_types:   Boolean. Sets if standard filetypes are used or not.
  17.      *
  18.      * Return: none
  19.      */
  20.     function __construct($load_standard_types=true) {
  21.         if($load_standard_types == true) {
  22.             $this->loadStandardTypes();
  23.         }
  24.     }
  25.    
  26.     /* Sets Max Filesize for images
  27.      * Params
  28.      *  @file_size: Integer. Maximum Filesize in bytes
  29.      *
  30.      * Return: returns false if $file_size isn't an integer value. else return true;
  31.      */
  32.     function setMaxFileSize($file_size) {
  33.         if(is_integer($file_size)) {
  34.             $this->max_file_size = $file_size;
  35.            
  36.             return true;
  37.         }
  38.         else {
  39.             return false;
  40.         }
  41.     }
  42.    
  43.     /* Checks if image is valid. Checks with getimagesize, the array accepted_types and the filesize
  44.      * Params
  45.      *  @file: String. Path to Imagefile.
  46.      *
  47.      * Return: boolean. Returns if image is valid or not.
  48.      */
  49.     function imageValid($file) {
  50.         if(file_exists($file)) {
  51.             $file_info = pathinfo($file);
  52.             $file_type = strtolower($file_info["extension"]);
  53.            
  54.             $accepted_type = false;
  55.            
  56.             foreach($this->accepted_types as $type) {
  57.                 if($type["extension"] == $file_type) {
  58.                     $accepted_type = true;
  59.                 }
  60.             }
  61.            
  62.             if($accepted_type == true) {
  63.                 if(is_array(getimagesize($file))) {
  64.                     if($this->max_file_size != 0) {
  65.                         if(filesize($file) <= $this->max_file_size) {
  66.                             return true;
  67.                         }
  68.                         else {
  69.                             $this->error_code = 4;
  70.                            
  71.                             return false;
  72.                         }
  73.                     }
  74.                     else {
  75.                         return true;
  76.                     }
  77.                 }
  78.                 else {
  79.                     $this->error_code = 3;
  80.                    
  81.                     return false;
  82.                 }
  83.             }
  84.             else {
  85.                 $this->error_code = 2;
  86.                
  87.                 return false;
  88.             }
  89.         }
  90.         else {
  91.             $this->error_code = 1;
  92.            
  93.             return false;
  94.         }
  95.     }
  96.    
  97.     /* Loads standard imagetypes.
  98.      *
  99.      * Return: none
  100.      */
  101.     function loadStandardTypes() {
  102.         $this->addAcceptedType("jpg","imagecreatefromjpeg","imagejpeg");
  103.         $this->addAcceptedType("jpeg","imagecreatefromjpeg","imagejpeg");
  104.         $this->addAcceptedType("gif","imagecreatefromgif","imagegif");
  105.         $this->addAcceptedType("png","imagecreatefrompng","imagepng");
  106.     }
  107.    
  108.     /* Adds imagestype and imagefunction
  109.      * Params
  110.      *  @extension  :   String. Specififes file extension
  111.      *  @function   :   String. Specifies a gd-function that will be user to read the image. Example: imagecreatefromjpg, imagecreatefromgif etc.
  112.      *
  113.      * Return: none
  114.      */
  115.     function addAcceptedType($extension,$read_function,$create_function) {
  116.         $type["extension"] = strtolower($extension);
  117.         $type["read_function"] = $read_function;
  118.         $type["create_function"] = $create_function;
  119.        
  120.         $this->accepted_types[] = $type;
  121.     }
  122.     /* Gets Errormessage for currently set Error.
  123.      *  Params
  124.      *   none
  125.      *
  126.      *  Return  :   String. errormessage. Returns false if no errormessage exists.
  127.      */
  128.     function getErrorMessage() {
  129.         if($this->error_code != 0) {
  130.             $ini_content = parse_ini_file($this->error_ini,true);
  131.            
  132.             if(isset($ini_content["image_resizer"][$this->error_code])) {
  133.                 return $ini_content["image_resizer"][$this->error_code];
  134.             }
  135.             else {
  136.                 return "Errormessage dosnt't exist! Errorcode: ".$this->error_code;
  137.             }
  138.         }
  139.         else {
  140.             return false;
  141.         }
  142.     }
  143.    
  144.     /*
  145.      * Validates and resizes Image to specific size. uses Method resizeImage.
  146.      * Params
  147.      *  @source_file        :       String. File to be resized
  148.      *  @destination_file   :       String. Path to new Image
  149.      *  @new_width          :       Integer. Specifies the width for the new image
  150.      *  @new_height         :       Integer. Specifies the height for the new image
  151.      *
  152.      * return   :   boolean. returns false if an error occurs.
  153.      */
  154.     function resizeFixed($source_file,$destination_file,$new_width,$new_height) {
  155.         if($this->imageValid($source_file) == true) {
  156.             if($this->resizeImage($source_file,$destination_file,$new_width,$new_height) == true) {
  157.                 return true;
  158.             }
  159.             else {
  160.                 return false;
  161.             }
  162.         }
  163.         else {
  164.             return false;
  165.         }
  166.     }
  167.    
  168.     /*
  169.      * Validates and resizes Image proportional to specific width. uses Method resizeImage.
  170.      * Params
  171.      *  @source_file            :       String. File to be resized
  172.      *  @destination_file       :       String. Path to new Image
  173.      *  @new_width              :       Integer. Specifies the width for the new image
  174.      *
  175.      * Return   :   boolean. returns false if an error occurs.
  176.      */
  177.     function resizeByWidth($source_file,$destination_file,$new_width) {
  178.         if($this->imageValid($source_file)) {
  179.             $dimensions = getimagesize($source_file);
  180.            
  181.             $width = $dimensions[0];
  182.             $height = $dimensions[1];
  183.            
  184.             $resize_factor = $width / $new_width;
  185.             $new_height = round($height / $resize_factor);
  186.            
  187.             if($this->resizeImage($source_file,$destination_file,$new_width,$new_height) == true) {
  188.                 return true;
  189.             }
  190.             else {
  191.                 return false;
  192.             }
  193.         }
  194.         else {
  195.             return false;
  196.         }
  197.     }
  198.    
  199.     /*
  200.      * Validates and resizes Image proportional to specific height. uses Method resizeImage.
  201.      * Params
  202.      *  @source_file            :       String. File to be resized
  203.      *  @destination_file       :       String. Path to new Image
  204.      *  @new_height             :       Integer. Specifies the height for the new image
  205.      *
  206.      * Return   :   boolean. returns false if an error occurs.
  207.      */
  208.     function resizeByHeight($source_file,$destination_file,$new_height) {
  209.         if($this->imageValid($source_file)) {
  210.             $dimensions = getimagesize($source_file);
  211.            
  212.             $width = $dimensions[0];
  213.             $height = $dimensions[1];
  214.            
  215.             $resize_factor = $height / $new_height;
  216.             $new_width = round($width / $resize_factor);
  217.            
  218.             if($this->resizeImage($source_file,$destination_file,$new_width,$new_height) == true) {
  219.                 return true;
  220.             }
  221.             else {
  222.                 return false;
  223.             }
  224.         }
  225.         else {
  226.             return false;
  227.         }
  228.     }
  229.    
  230.     /* resizes images to specific size. uses gd library.
  231.      * Params
  232.      *  @pfile      :   String. specifies the sourcefile.
  233.      *  @pdst_path  :   String. specifies the output file.
  234.      * Return   : boolean. Returns if resizing was successful
  235.      */
  236.     function resizeImage($pfile,$pdst_path,$pwidth,$pheight) {
  237.         $file_info = pathinfo($pfile);
  238.         $file_type = strtolower($file_info["extension"]);
  239.  
  240.         foreach($this->accepted_types as $type) {
  241.             if($type["extension"] == $file_type) {
  242.                 eval('$img_src = '.$type["read_function"].'($pfile);');
  243.             }
  244.         }
  245.        
  246.         $dimensions = getimagesize($pfile);
  247.        
  248.         $img_src_width = $dimensions[0];
  249.         $img_src_height = $dimensions[1];
  250.         $dst = ImageCreateTrueColor($pwidth,$pheight);
  251.        
  252.         imagecopyresampled($dst,$img_src,0,0,0,0,$pwidth,$pheight,$img_src_width,$img_src_height);
  253.        
  254.         foreach($this->accepted_types as $type) {
  255.             if($type["extension"] == $file_type) {
  256.                 eval('$img = '.$type["create_function"].'($dst,$pdst_path,100);');
  257.             }
  258.         }
  259.        
  260.         if(imagejpeg($dst,$pdst_path,100)) {
  261.             imagedestroy($dst);
  262.             imagedestroy($img_src);
  263.             return true;
  264.         }
  265.         else {
  266.             return false;
  267.         }
  268.     }
  269. }
  270.  
  271.  
  272. if(isset($_FILES["image"])) {
  273.     move_uploaded_file($_FILES["image"]["tmp_name"],$_FILES["image"]["name"]);
  274.  
  275.     $test = new imageResizer();
  276.     $test->setMaxFileSize(5000000);
  277.    
  278.     if($test->resizeByHeight($_FILES["image"]["name"],"test.jpg",160) != true) {
  279.         echo $test->getErrorMessage();
  280.     }
  281. }
  282.  
  283.  
  284.  
  285. ?>
  286.  
  287. <form action="class.imageResizer.php" method="POST" enctype="multipart/form-data">
  288. <input type="file" name="image" />
  289. <input type="submit" value="hoch damit"/>
  290. </form>
  291.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement