function _uploadpic ($upfile, $maxsize, $updir, $newname = ' Date ') { if ($newname = = ' Date ') $newname = Date ("Ymdhis"); Use date to make file name $name = $upfile ["name"]; $type = $upfile ["type"]; $size = $upfile ["Size"]; $tmp _name = $upfile ["Tmp_name"]; Switch ($type) { Case ' Image/pjpeg ': Case ' Image/jpeg ': $extend = ". jpg"; Break Case ' image/gif ': $extend = ". gif"; Break Case ' image/png ': $extend = ". png"; Break } if (Emptyempty ($extend)) { Echo ("Warning! can only upload image type: GIF JPG PNG "); Exit (); } if ($size > $maxsize) { $maxpr = $maxsize/1000; Echo ("Warning! Upload image size cannot exceed ". $maxpr. "k!"); Exit (); } if (Move_uploaded_file ($tmp _name, $updir. $newname. $extend)) { Return $updir. $newname. $extend; } } function show_pic_scal ($width, $height, $picpath) { $imginfo = getimagesize ($picpath); $IMGW = $imginfo [0]; $IMGH = $imginfo [1]; $ra = Number_format (($IMGW/$IMGH), 1); Width-to-height ratio $ra 2 = Number_format (($IMGH/$IMGW), 1); Aspect ratio if ($imgw > $width or $IMGH > $height) { if ($imgw > $imgh) { $newWidth = $width; $newHeight = Round ($newWidth/$ra); } elseif ($IMGW < $IMGH) { $newHeight = $height; $newWidth = Round ($newHeight/$ra 2); } else { $newWidth = $width; $newHeight = Round ($newWidth/$ra); } } else { $newHeight = $IMGH; $newWidth = $IMGW; } $newsize [0] = $newWidth; $newsize [1] = $newHeight; return $newsize; } function Getimageinfo ($SRC) { Return getimagesize ($SRC); } /** * Create picture, return resource type * @param string $src picture path * @return Resource $im return resource type * **/ function Create ($SRC) { $info =getimageinfo ($SRC); Switch ($info [2]) { Case 1: $im =imagecreatefromgif ($SRC); Break Case 2: $im =imagecreatefromjpeg ($SRC); Break Case 3: $im =imagecreatefrompng ($SRC); Break } return $im; } /** * Thumbnail main function * @param string $src picture path * @param int $w thumbnail width * @param int $h thumbnail height * @return Mixed return thumbnail path * **/ function Resize ($src, $w, $h) { $temp =pathinfo ($SRC); $name = $temp ["basename"];//file name $dir = $temp the folder where the ["DirName"];//file is located $extension = $temp ["extension"];//file name extension $savepath = "{$dir}/{$name}";//thumbnail save path, new file named *.thumb.jpg Get basic information about a picture $info =getimageinfo ($SRC); $width = $info [0];//get picture width $height = $info [1];//get picture height $per 1=round ($width/$height, 2);//calculate original aspect ratio $per 2=round ($w/$h, 2);//calculate thumbnail aspect ratio Calculating the zoom ratio if ($per 1> $per 2| | $per 1== $per 2) { The original aspect ratio is greater than or equal to the thumbnail aspect ratio, then the width first $per = $w/$width; } if ($per 1< $per 2) { The original aspect ratio is less than the thumbnail aspect ratio, which is high priority $per = $h/$height; } $temp _w=intval ($width * $per);//Calculate the width of the original image after zooming $temp _h=intval ($height * $per);//Calculate the height of the original image after zooming $temp _img=imagecreatetruecolor ($temp _w, $temp _h);//Create Canvas $im =create ($SRC); Imagecopyresampled ($temp _img, $im, 0,0,0,0, $temp _w, $temp _h, $width, $height); if ($per 1> $per 2) { Imagejpeg ($temp _img, $savepath, 100); Imagedestroy ($im); Return addbg ($savepath, $w, $h, "w"); Width first, fill the background with insufficient height after zooming } if ($per 1== $per 2) { Imagejpeg ($temp _img, $savepath, 100); Imagedestroy ($im); return $savepath; Equal ratio Scaling } if ($per 1< $per 2) { Imagejpeg ($temp _img, $savepath, 100); Imagedestroy ($im); Return addbg ($savepath, $w, $h, "H"); High priority, fill the background with insufficient width after zooming } } /** * Add background * @param string $src picture path * @param int $w background image width * @param int $h background Image height * @param String $first determines the final position of the image, W width first h high priority wh: equal ratio * @return back picture with background * **/ function addbg ($SRC, $w, $h, $fisrt = "W") { $BG =imagecreatetruecolor ($w, $h); $white = Imagecolorallocate ($BG, 255,255,255); Imagefill ($BG, 0,0, $white);//Fill background Get Target picture information $info =getimageinfo ($SRC); $width = $info [0];//target image width $height = $info [1];//target Image Height $img =create ($SRC); if ($fisrt = = "WH") { Equal ratio Scaling return $SRC; } Else { if ($fisrt = = "W") { $x = 0; $y = ($h-$height)/2;//Vertical Center } if ($fisrt = = "H") { $x = ($w-$width)/2;//Horizontal Center $y = 0; } Imagecopymerge ($BG, $img, $x, $y, 0,0, $width, $height, 100); Imagejpeg ($BG, $SRC, 100); Imagedestroy ($BG); Imagedestroy ($IMG); return $SRC; } } ?> How to use: $filename = (_uploadpic ($_files["Upload"), $maxsize, $updir, $newname = ' date '); $show _pic_scal=show_pic_scal ($filename); Resize ($filename, $show _pic_scal[0], $show _pic_scal[1]); |