Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Classname: imageResizer
- * Filename: class.imageResizer.php
- * Author: Bartlomiej Pohl
- * Description: class for Image resizing
- */
- class imageResizer {
- public $accepted_types = array(); //Array with accepted types. Contains the mime_type, extension and function that is called to load image
- public $error_code = 0; //Errorcode. 0: no errors.
- public $error_ini = "../config/errors.ini"; //Inifile from which all errormessages are loaded.
- public $max_file_size = 0; //Sets the Maximum Filesize for sourcefiles to resize
- /* constructor: Params
- * @images_path : String. Path were resized Images are copied to
- * @load_standard_types: Boolean. Sets if standard filetypes are used or not.
- *
- * Return: none
- */
- function __construct($load_standard_types=true) {
- if($load_standard_types == true) {
- $this->loadStandardTypes();
- }
- }
- /* Sets Max Filesize for images
- * Params
- * @file_size: Integer. Maximum Filesize in bytes
- *
- * Return: returns false if $file_size isn't an integer value. else return true;
- */
- function setMaxFileSize($file_size) {
- if(is_integer($file_size)) {
- $this->max_file_size = $file_size;
- return true;
- }
- else {
- return false;
- }
- }
- /* Checks if image is valid. Checks with getimagesize, the array accepted_types and the filesize
- * Params
- * @file: String. Path to Imagefile.
- *
- * Return: boolean. Returns if image is valid or not.
- */
- function imageValid($file) {
- if(file_exists($file)) {
- $file_info = pathinfo($file);
- $file_type = strtolower($file_info["extension"]);
- $accepted_type = false;
- foreach($this->accepted_types as $type) {
- if($type["extension"] == $file_type) {
- $accepted_type = true;
- }
- }
- if($accepted_type == true) {
- if(is_array(getimagesize($file))) {
- if($this->max_file_size != 0) {
- if(filesize($file) <= $this->max_file_size) {
- return true;
- }
- else {
- $this->error_code = 4;
- return false;
- }
- }
- else {
- return true;
- }
- }
- else {
- $this->error_code = 3;
- return false;
- }
- }
- else {
- $this->error_code = 2;
- return false;
- }
- }
- else {
- $this->error_code = 1;
- return false;
- }
- }
- /* Loads standard imagetypes.
- *
- * Return: none
- */
- function loadStandardTypes() {
- $this->addAcceptedType("jpg","imagecreatefromjpeg","imagejpeg");
- $this->addAcceptedType("jpeg","imagecreatefromjpeg","imagejpeg");
- $this->addAcceptedType("gif","imagecreatefromgif","imagegif");
- $this->addAcceptedType("png","imagecreatefrompng","imagepng");
- }
- /* Adds imagestype and imagefunction
- * Params
- * @extension : String. Specififes file extension
- * @function : String. Specifies a gd-function that will be user to read the image. Example: imagecreatefromjpg, imagecreatefromgif etc.
- *
- * Return: none
- */
- function addAcceptedType($extension,$read_function,$create_function) {
- $type["extension"] = strtolower($extension);
- $type["read_function"] = $read_function;
- $type["create_function"] = $create_function;
- $this->accepted_types[] = $type;
- }
- /* Gets Errormessage for currently set Error.
- * Params
- * none
- *
- * Return : String. errormessage. Returns false if no errormessage exists.
- */
- function getErrorMessage() {
- if($this->error_code != 0) {
- $ini_content = parse_ini_file($this->error_ini,true);
- if(isset($ini_content["image_resizer"][$this->error_code])) {
- return $ini_content["image_resizer"][$this->error_code];
- }
- else {
- return "Errormessage dosnt't exist! Errorcode: ".$this->error_code;
- }
- }
- else {
- return false;
- }
- }
- /*
- * Validates and resizes Image to specific size. uses Method resizeImage.
- * Params
- * @source_file : String. File to be resized
- * @destination_file : String. Path to new Image
- * @new_width : Integer. Specifies the width for the new image
- * @new_height : Integer. Specifies the height for the new image
- *
- * return : boolean. returns false if an error occurs.
- */
- function resizeFixed($source_file,$destination_file,$new_width,$new_height) {
- if($this->imageValid($source_file) == true) {
- if($this->resizeImage($source_file,$destination_file,$new_width,$new_height) == true) {
- return true;
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
- /*
- * Validates and resizes Image proportional to specific width. uses Method resizeImage.
- * Params
- * @source_file : String. File to be resized
- * @destination_file : String. Path to new Image
- * @new_width : Integer. Specifies the width for the new image
- *
- * Return : boolean. returns false if an error occurs.
- */
- function resizeByWidth($source_file,$destination_file,$new_width) {
- if($this->imageValid($source_file)) {
- $dimensions = getimagesize($source_file);
- $width = $dimensions[0];
- $height = $dimensions[1];
- $resize_factor = $width / $new_width;
- $new_height = round($height / $resize_factor);
- if($this->resizeImage($source_file,$destination_file,$new_width,$new_height) == true) {
- return true;
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
- /*
- * Validates and resizes Image proportional to specific height. uses Method resizeImage.
- * Params
- * @source_file : String. File to be resized
- * @destination_file : String. Path to new Image
- * @new_height : Integer. Specifies the height for the new image
- *
- * Return : boolean. returns false if an error occurs.
- */
- function resizeByHeight($source_file,$destination_file,$new_height) {
- if($this->imageValid($source_file)) {
- $dimensions = getimagesize($source_file);
- $width = $dimensions[0];
- $height = $dimensions[1];
- $resize_factor = $height / $new_height;
- $new_width = round($width / $resize_factor);
- if($this->resizeImage($source_file,$destination_file,$new_width,$new_height) == true) {
- return true;
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
- /* resizes images to specific size. uses gd library.
- * Params
- * @pfile : String. specifies the sourcefile.
- * @pdst_path : String. specifies the output file.
- * Return : boolean. Returns if resizing was successful
- */
- function resizeImage($pfile,$pdst_path,$pwidth,$pheight) {
- $file_info = pathinfo($pfile);
- $file_type = strtolower($file_info["extension"]);
- foreach($this->accepted_types as $type) {
- if($type["extension"] == $file_type) {
- eval('$img_src = '.$type["read_function"].'($pfile);');
- }
- }
- $dimensions = getimagesize($pfile);
- $img_src_width = $dimensions[0];
- $img_src_height = $dimensions[1];
- $dst = ImageCreateTrueColor($pwidth,$pheight);
- imagecopyresampled($dst,$img_src,0,0,0,0,$pwidth,$pheight,$img_src_width,$img_src_height);
- foreach($this->accepted_types as $type) {
- if($type["extension"] == $file_type) {
- eval('$img = '.$type["create_function"].'($dst,$pdst_path,100);');
- }
- }
- if(imagejpeg($dst,$pdst_path,100)) {
- imagedestroy($dst);
- imagedestroy($img_src);
- return true;
- }
- else {
- return false;
- }
- }
- }
- if(isset($_FILES["image"])) {
- move_uploaded_file($_FILES["image"]["tmp_name"],$_FILES["image"]["name"]);
- $test = new imageResizer();
- $test->setMaxFileSize(5000000);
- if($test->resizeByHeight($_FILES["image"]["name"],"test.jpg",160) != true) {
- echo $test->getErrorMessage();
- }
- }
- ?>
- <form action="class.imageResizer.php" method="POST" enctype="multipart/form-data">
- <input type="file" name="image" />
- <input type="submit" value="hoch damit"/>
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement