This PHP image upload class function is very complete, can meet a variety of image upload needs
- /**************************************
- Seesaw Associates | Http://seesaw.net
- Client
- File
- Description
- Copyright (C) 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 have GD library ver.");
- }
- 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 is an Error creating the images ';
- Break
- }
- }
- }
- ?>
Copy Code
|