PHP file Upload class can generate thumbnail code

Source: Internet
Author: User
Tags explode file upload imagejpeg mkdir php file upload

<?php

The code is as follows Copy Code
if ($_get[' Action '] = = ' Save ' {       
      
     $up = new Upload ();       
    $up->set_dir ( DirName (__file__). ' /upload/', ' {y}/{m} ');       
    $up->set_thumb (100,80) ;       
    $up->set_watermark (dirname (__file__). /jblog/images/watermark.png ', 6,90);       
    $fs = $up- >execute ();       
      
     var_dump ($FS);       
}    


?>

The code is as follows Copy Code
&lt;html&gt;


&lt;head&gt;&lt;title&gt;test&lt;/title&gt;&lt;/head&gt;


&lt;body style= "margin:0;padding:0" &gt;


&lt;form name= "Upload" method= "post" action= "Action=save" enctype= "Multipart/form-data" style= "margin:0" &gt;


&lt;input type= "File" Name= "attach[]"/&gt;


&lt;input type= "File" Name= "attach[]"/&gt;


&lt;input type= "Submit" name= "submit" value= "Upload"/&gt;


&lt;/form&gt;


&lt;/body&gt;


&lt;/html&gt;

The code is as follows Copy Code
Class Upload {





var $dir; Attachment Storage Physical Directory


var $time; Custom File Upload Time


var $allow _types; Allow upload attachment type


var $field; Upload Control Name


var $maxsize; Maximum allowable file size in KB





var $thumb _width; Thumbnail width


var $thumb _height; Thumbnail height





var $watermark _file; Watermark Picture Address


var $watermark _pos; Watermark Location


var $watermark _trans;//Watermark Transparency








Constructors


$types: File types allowed to upload, $maxsize: Allowed size, $field: Upload control name, $time: Custom upload Time


function upload ($types = ' jpg|png ', $maxsize = 1024, $field = ' attach ', $time = ') {


$this-&gt;allow_types = explode (' | ', $types);


$this-&gt;maxsize = $maxsize * 1024;


$this-&gt;field = $field;


$this-&gt;time = $time? $time: Time ();


}





Set up and create a directory where files are stored specifically


$basedir: Base directory, must be a physical path


$filedir: Custom subdirectory, available parameters {Y}, {m}, {d}


function Set_dir ($basedir, $filedir = ' ") {


$dir = $basedir;


!is_dir ($dir) &amp;&amp; @mkdir ($dir, 0777);


if (!emptyempty ($filedir)) {


$filedir = str_replace (Array (' {y} ', ' {m} ', ' {d} '), Array (date (' Y ', $this-&gt;time), date (' m ', $this-&gt;time), date (' d ' , $this-&gt;time)), Strtolower ($filedir));


$dirs = explode ('/', $filedir);


foreach ($dirs as $d) {


!emptyempty ($d) &amp;&amp; $dir. = $d. '/';


!is_dir ($dir) &amp;&amp; @mkdir ($dir, 0777);


}


}


$this-&gt;dir = $dir;


}





Picture thumbnail settings, not set if you don't generate thumbnails


$width: Thumbnail width, $height: thumbnail height


function Set_thumb ($width = 0, $height = 0) {


$this-&gt;thumb_width = $width;


$this-&gt;thumb_height = $height;


}





Picture watermark settings, if not generated add watermark is not set


$file: Watermark picture, $pos: Watermark location, $trans: Watermark Transparency


function Set_watermark ($file, $pos = 6, $trans = 80) {


$this-&gt;watermark_file = $file;


$this-&gt;watermark_pos = $pos;


$this-&gt;watermark_trans = $trans;


}





/*----------------------------------------------------------------


Execute file Upload, finish processing returns an array of file information that contains uploaded successes or failures,


Where: Name is the filename, upload success is uploaded to the server file name, upload failure is the local file name


Dir holds the physical path for the attachment on the server, and the upload fails without the value


Size is an attachment, upload failure does not exist this value


Flag is Status ID, 1 indicates success,-1 indicates file type is not allowed,-2 indicates file size exceeds


-----------------------------------------------------------------*/


function Execute () {


$files = Array (); Successfully uploaded file information


$field = $this-&gt;field;


$keys = Array_keys ($_files[$field] [' name ']);


foreach ($keys as $key) {


if (!$_files[$field] [' name '] [$key]) continue;





$fileext = $this-&gt;fileext ($_files[$field [' name '] [$key]); Get file name extension


$filename = Date (' Ymdhis ', $this-&gt;time). Mt_rand (10,99). $fileext; Generate file name


$filedir = $this-&gt;dir; Attachment Actual Storage Directory


$filesize = $_files[$field] [' size '] [$key]; File size





File type not allowed


if (!in_array ($fileext, $this-&gt;allow_types)) {


$files [$key] [' name '] = $_files[$field] [' name '] [$key];


$files [$key] [' flag '] =-1;


Continue


}





File size exceeds


if ($filesize &gt; $this-&gt;maxsize) {


$files [$key] [' name '] = $_files[$field] [' name '] [$key];


$files [$key] [' name '] = $filesize;


$files [$key] [' flag '] =-2;


Continue


}





$files [$key] [' name '] = $filename;


$files [$key] [' dir '] = $filedir;


$files [$key] [' size '] = $filesize;





Save uploaded files and delete temporary files


if (Is_uploaded_file ($_files[$field] [' tmp_name '] [$key])) {


Move_uploaded_file ($_files[$field] [' tmp_name '] [$key], $filedir. $filename);


@unlink ($_files[$field] [' tmp_name '] [$key]);


$files [$key] [' flag '] = 1;





Watermark and generate thumbnails for a picture


if (In_array ($fileext, array (' jpg ', ' png ')) {


if ($this-&gt;thumb_width) {


if ($this-&gt;create_thumb ($filedir. $filename, $filedir. ' Thumb_ '. $filename)) {


$files [$key] [' thumb '] = ' thumb_ '. $filename; Thumbnail file name


}


}


$this-&gt;create_watermark ($filedir. $filename);


}


}


}





return $files;


}





Create thumbnails to generate thumbnails with the same extension


$SRC _file: Source image path, $thumb _file: Thumbnail path


function Create_thumb ($src _file, $thumb _file) {


$t _width = $this-&gt;thumb_width;


$t _height = $this-&gt;thumb_height;





if (!file_exists ($src _file)) return false;





$src _info = getimagesize ($src _file);





Copy the source image as a thumbnail if the source image is less than or equal to the thumbnail


if ($src _info[0] &lt;= $t _width &amp;&amp; $src _info[1] &lt;= $t _height) {


if (!copy ($src _file, $thumb _file)) {


return false;


}


return true;


}





Calculate thumbnail size proportionally


if ($src _info[0]-$t _width &gt; $src _info[1]-$t _height) {


$t _height = ($t _width/$src _info[0]) * $SRC _info[1];


} else {


$t _width = ($t _height/$src _info[1]) * $SRC _info[0];


}





Get file name extension


$fileext = $this-&gt;fileext ($src _file);





Switch ($fileext) {


Case ' jpg ':


$src _img = imagecreatefromjpeg ($src _file); Break


Case ' PNG ':


$src _img = imagecreatefrompng ($src _file); Break


Case ' gif ':


$src _img = imagecreatefromgif ($src _file); Break


}





Create a true-color thumbnail image


$thumb _img = @ImageCreateTrueColor ($t _width, $t _height);





imagecopyresampled function Copy of the image smoothness is better, the first consideration


if (function_exists (' imagecopyresampled ')) {


@ImageCopyResampled ($thumb _img, $src _img,0,0,0,0, $t _width, $t _height, $src _info[0], $src _info[1]);


} else {


@ImageCopyResized ($thumb _img, $src _img,0,0,0,0, $t _width, $t _height, $src _info[0], $src _info[1]);


}





Generate thumbnails


Switch ($fileext) {


Case ' jpg ':


Imagejpeg ($thumb _img, $thumb _file); Break


Case ' gif ':


Imagegif ($thumb _img, $thumb _file); Break


Case ' PNG ':


Imagepng ($thumb _img, $thumb _file); Break


}





Destroying temporary images


@ImageDestroy ($src _img);


@ImageDestroy ($thumb _img);





return true;





}





Add a watermark to a picture


$file: Files to add a watermark


function Create_watermark ($file) {





Returns if the file does not exist


if (!file_exists ($this-&gt;watermark_file) | | |!file_exists ($file)) return;


if (!function_exists (' getimagesize ')) return;





Check the file types supported by GD


$GD _allow_types = Array ();


if (function_exists (' imagecreatefromgif ')) $gd _allow_types[' image/gif '] = ' imagecreatefromgif ';


if (function_exists (' imagecreatefrompng ')) $gd _allow_types[' image/png '] = ' imagecreatefrompng ';


if (function_exists (' imagecreatefromjpeg ')) $gd _allow_types[' image/jpeg '] = ' imagecreatefromjpeg ';





Get file information


$fileinfo = getimagesize ($file);


$wminfo = getimagesize ($this-&gt;watermark_file);





if ($fileinfo [0] &lt; $wminfo [0] | | | $fileinfo [1] &lt; $wminfo [1]) return;





if (array_key_exists ($fileinfo [' MIME '], $GD _allow_types)) {


if (array_key_exists ($wminfo [' MIME '], $GD _allow_types)) {





Create an image from a file


$temp = $gd _allow_types[$fileinfo [' MIME ']] ($file);


$temp _wm = $gd _allow_types[$wminfo [' MIME ']] ($this-&gt;watermark_file);





Watermark Location


Switch ($this-&gt;watermark_pos) {


Case 1://Top Left


$DST _x = 0; $DST _y = 0; Break


Case 2://Top Center


$DST _x = ($fileinfo [0]-$wminfo [0])/2; $DST _y = 0; Break


Case 3://Top Right


$DST _x = $fileinfo [0]; $DST _y = 0; Break


Case 4://Bottom Left


$DST _x = 0; $DST _y = $fileinfo [1]; Break


Case 5://Bottom Center


$DST _x = ($fileinfo [0]-$wminfo [0])/2; $DST _y = $fileinfo [1]; Break


Case 6://Bottom Right


$DST _x = $fileinfo [0]-$wminfo [0]; $DST _y = $fileinfo [1]-$wminfo [1]; Break


Default://Random


$DST _x = Mt_rand (0, $fileinfo [0]-$wminfo [0]); $DST _y = Mt_rand (0, $fileinfo [1]-$wminfo [1]);


}





if (function_exists (' imagealphablending ')) imagealphablending ($temp _wm,true); Set the blending mode of the image


if (function_exists (' Imagesavealpha ')) Imagesavealpha ($temp _wm,true); To save full alpha channel information





Add a watermark to an image


if (function_exists (' Imagecopymerge ')) {


Imagecopymerge ($temp, $temp _wm, $dst _x, $dst _y,0,0, $wminfo [0], $wminfo [1], $this-&gt;watermark_trans);


} else {


Imagecopymerge ($temp, $temp _wm, $dst _x, $dst _y,0,0, $wminfo [0], $wminfo [1]);


}





Save picture


Switch ($fileinfo [' MIME ']) {


Case ' Image/jpeg ':


@imageJPEG ($temp, $file);


Break


Case ' image/png ':


@imagePNG ($temp, $file);


Break


Case ' image/gif ':


@imageGIF ($temp, $file);


Break


}


Destroying zero images


@imageDestroy ($temp);


@imageDestroy ($temp _wm);


}


}


}





Get file name extension


function Fileext ($filename) {


Return Strtolower (substr strrchr ($filename, '. '), 1,10);


}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.