Revision: 6420
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at May 20, 2008 13:21 by mattkenefick
Initial Code
<?php /************************************** seesaw associates | http://seesaw.net client: file: description: Copyright (C) 2008 Matt Kenefick(.com) **************************************/ class mk_imageUpload{ var $max_size; var $allowed_types; var $thumb_height; var $dest_dir; var $extensions; var $max_height; var $max_main_height; var $last_ext; var $last_pid; var $last_uid; var $image_file; var $image_field; function __construct( $maxHeightMain, $maxHeightThumb, $destDir ){ $this->max_size = (1024/2)*1000; // 750kb $this->allowed_types = array( 'jpeg', 'jpg', 'pjpeg', 'gif', 'png' ); $this->extensions = array( 'image/jpeg' => '.jpg', 'image/gif' => '.gif', 'image/png' => '.png', 'image/x-png' => '.png', 'image/pjpeg' => '.jpg' ); $this->dest_dir = $destDir; $this->max_height = $maxHeightThumb; $this->max_main_height = $maxHeightMain; } function putImage( $formname, $newName ){ $this->image_field = $formname; if( $this->getImage() ){ // Check for errors if ( !$this->checkType() ) return $this->throwError(2); if ( !$this->checkFileSize() ) return $this->throwError(1); // Get Image $img = $this->image_file; // Check seize if ( !$this->checkImageSize() ) return $this->throwError(3); // Get image dimensinos $size = $this->getImageSize(); $size['width'] = $size[0]; $size['height'] = $size[1]; $ratio = $this->max_height/$size['height']; $maxheight = $this->max_height; $maxwidth = $size['width'] * $ratio; // Create Thumbnail $s_t = $this->resizeImage( $size, $img, $maxwidth, $maxheight,$newName,'s' ); if( $size['height'] > $this->max_main_height ){ $ratio = $this->max_main_height/$size['height']; $maxheight = $this->max_main_height; $maxwidth = $size['width'] * $ratio; }else{ $maxheight = $size['height']; $maxwidth = $size['width']; } // Create Large rescaled $s_l = $this->resizeImage( $size, $img, $maxwidth, $maxheight,$newName,'l' ); // remove temporary file unlink($img['tmp_name']); if( $s_t && $s_l ){ $nm = split('_',$newName); $this->last_ext = $this->extensions[$size['mime']]; $this->last_pid = $nm[0]; $this->last_uid = $nm[1]; return 'ok'; }else{ return $this->throwError( 4 ); } }else{ return $this->throwError( 2 ); } } function resizeImage($size,$img,$maxwidth,$maxheight,$newName,$ext){ // Create a thumbnail if($size['mime'] == "image/pjpeg" || $size['mime'] == "image/jpeg"){ $new_img = imagecreatefromjpeg($img['tmp_name']); }elseif($size['mime'] == "image/x-png" || $size['mime'] == "image/png"){ $new_img = imagecreatefrompng($img['tmp_name']); }elseif($size['mime'] == "image/gif"){ $new_img = imagecreatefromgif($img['tmp_name']); } if (function_exists('imagecreatetruecolor')){ $resized_img = imagecreatetruecolor($maxwidth,$maxheight); }else{ return("Error: Please make sure your server has GD library ver 2+"); } imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $maxwidth, $maxheight, $size['width'], $size['height']); if($size['mime'] == "image/pjpeg" || $size['mime'] == "image/jpeg"){ $success = ImageJpeg ($resized_img,$this->dest_dir.$newName.'_'.$ext.$this->extensions[$size['mime']]); }elseif($size['mime'] == "image/x-png" || $size['mime'] == "image/png"){ $success = ImagePNG ($resized_img,$this->dest_dir.$newName.'_'.$ext.$this->extensions[$size['mime']]); }elseif($size['mime'] == "image/gif"){ $success = ImageGIF ($resized_img,$this->dest_dir.$newName.'_'.$ext.$this->extensions[$size['mime']]); } // Remove temporary images ImageDestroy ($resized_img); ImageDestroy ($new_img); return $success; } function getImage(){ if( isset($_FILES[$this->image_field]) && is_uploaded_file($_FILES[$this->image_field]['tmp_name']) ){ $this->image_file = $_FILES[$this->image_field]; return true; } return false; } function returnImg(){ return $this->image_file; } function getImageSize(){ $img = $this->returnImg(); return getimagesize($img['tmp_name']); } function checkImageSize(){ $size = $this->getImageSize(); if( $size[1] < $this->max_height ) return false; return true; } function checkFileSize(){ $img = $this->returnImg(); if( $img['size'] > $this->max_size ) return false; return true; } function checkType(){ $img = $this->returnImg(); $type = split('/',$img['type']); if( !in_array( $type[1], $this->allowed_types ) ) return false; return true; } function throwError($val){ switch($val){ case 1: return 'Error: File size is too large'; break; case 2: return 'Error: Improper file format'; break; case 3: return 'Error: Your image is too small'; break; case 4: return 'Error: There was an error creating the images'; break; } } } ?>
Initial URL
Initial Description
Usage coming soon.
Initial Title
PHP Class: ImageUpload
Initial Tags
class, php, image
Initial Language
PHP