/**
- *
- * @author Zhao Jinhan
- * @date January 13, 2014 11:54:30
- * @email xb_zjh@126.com
- *
- */
- Header (' content-type:text/html; Charset=utf-8 ');
- Define the width height of the thumbnail image
- Define (' Thumb_width ', 300);
- Define (' Thumb_height ', 300);
- /**
- * Regenerate the uploaded file name
- * @return String
- * @author Zhao Jinhan
- *
- */
- function _file_type ($filetype = null) {
- Switch ($filetype)
- {
- Case "Image/jpeg":
- $fileextname = "JPG";
- Break
- Case "Image/gif":
- $fileextname = "gif";
- Break
- Case "Image/png":
- $fileextname = "png";
- Break
- Default
- $fileextname = false;
- Break
- }
- return $fileextname date (' Ymdhis ', Time ()). $fileextname: false;
- }
- /**
- *
- * @param string $filename
- * @param string $width
- * @param string $height
- * @param string $quality
- * @param string $savepath
- * @return Boolean
- */
- function _make_thumb ($filename = ", $width =thumb_width, $height =thumb_height, $savepath = './upload ') {
- if (file_exists ($filename)) {
- Size of the uploaded image
- $imagesize =getimagesize ($filename);
- $imagewidth = $imagesize [0];
- $imageheight = $imagesize [1];
- $mime = $imagesize [' MIME '];
- Width/height ratio
- $ratio = $imagewidth/$imageheight;
- Create a new background image
- $bgimg = Imagecreatetruecolor ($width, $height);
- $white = Imagecolorallocate ($bgimg, 255, 255, 255);
- Fill background color to white
- Imagefill ($bgimg, 0,0, $white);
- if ($mime = = ' Image/gif ') {
- $im = @imagecreatefromgif ($filename); /* Attempt to open */
- $outfun = ' imagegif ';
- }elseif ($mime = = ' Image/png ') {
- $im = @imagecreatefrompng ($filename); /* Attempt to open */
- $outfun = ' imagepng ';
- }else{
- $im = @imagecreatefromjpeg ($filename); /* Attempt to open */
- $outfun = ' imagejpeg ';
- }
- if ($ratio > 1) {
- Large width
- if ($imagewidth > $width) {
- Zoom the picture onto the background image
- $new _width = $width;
- $new _height = ($width * $imageheight)/$imagewidth;
- $BG _y = Ceil (ABS (($height-$new _height)/2);
- Imagecopyresampled ($bgimg, $im, 0, $bg _y, 0, 0, $new _width, $new _height, $imagewidth, $imageheight);
- }else{
- Copy picture to background image
- $copy = true;
- }
- }else{
- Highly large
- if ($imageheight > $height) {
- Zoom picture
- $new _height = $height;
- $new _width = ($height * $imagewidth)/$imageheight;
- $BG _x = Ceil (($width-$new _width)/2);
- Imagecopyresampled ($bgimg, $im, $bg _x, 0, 0, 0, $new _width, $new _height, $imagewidth, $imageheight);
- }else{
- Copy picture to background image
- $copy = true;
- }
- }
- if ($copy) {
- Copy picture to background image
- $BG _x = Ceil (($width-$imagewidth)/2);
- $BG _y = Ceil (($height-$imageheight)/2);
- Imagecopy ($bgimg, $im, $bg _x, $bg _y, 0, 0, $imagewidth, $imageheight);
- }
- $ext = _file_type ($mime);
- $outfun ($bgimg, $savepath. ' /'. $ext);
- Imagedestroy ($BGIMG);
- return $savepath. ' /'. $ext;
- }else{
- return false;
- }
- }
- if ($_post) {
- $size = $_post[' size ']?strtoupper (Trim ($_post[' size ')): ' 2M ';
- $imgsize = $_files[' img ' [' Size ']?$_files[' img '] [' Size ']/(1024*1024): 0;
- $imgwidth = $imgheight = $_post[' Width-height ']?intval ($_post[' Width-height ']): 300;
- Custom definition File Upload size
- Ini_set (' upload_max_filesize ', $size);
- $mathsize = Str_replace (' M ', ' ', $size);
- if ($imgsize > $mathsize) {
- echo "Picture size must not exceed {$size}!";
- Return
- }
- if ($file _name = _file_type ($_files[' img ' [' type '])) {
- if ($_files[' img ' [' error '] = = UPLOAD_ERR_OK) {
- $savepath = ' upload/';
- if (!is_dir ($savepath)) {
- mkdir ($savepath, 0644);
- }
- Generate thumbnail images
- $thumb _file = _make_thumb ($_files[' img ' [' tmp_name '], $imgwidth, $imgheight, $savepath);
- Move_uploaded_file ($_files[' img '] [' tmp_name '], $savepath. $file _name);
- echo "The resulting image is:";
- }else{
- echo $_files[' img ' [' ERROR '];
- Return
- }
- }else{
- echo "image format is not correct, please upload jpg,gif,png format!";
- Return
- }
}else{
- Echo <<
-
-
- Zoom picture Save to Square
- EOT;
- }
Copy Code |