PHP upload images and compression of the implementation method,
This article explains the PHP upload image and compression of the implementation method, before a "PHP implementation image upload and compression" has been a simple introduction to everyone, the implementation of the upload image and then according to the proportional thumbnail, specify the maximum height or maximum width of the thumbnail, the specific content is as follows
Implementation code:
<?php function _uploadpic ($upfile, $maxsize, $updir, $newname = ' Date ') {if ($newname = = ' Date ') $newname = d Ate ("ymdhis"); Use date to do 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); Aspect ratio $ra 2 = Number_format (($IMGH/$IMGW), 1); Aspect 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 =getima Geinfo ($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 ["dirname"];//file is in the same folder $extension = $temp ["extension"];//file name extension $savepath = "{ $dir}/{$name} ";//thumbnail save path, new file name *.thumb.jpg//Get basic information of picture $info =getimageinfo ($SRC); $width = $info [0];//Gets the picture width $height = $info [1];//Gets the picture height $per 1=round ($width/$height, 2);//Calculates the original aspect ratio $per 2=round ($w/$h, 2); /calculate thumbnail aspect ratio//calculate scaling 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, then the 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 in the background if the height is insufficient after zooming} if ($per 1== $per 2) {imagejpeg ($temp _img, $savepath, 100); Imagedestroy ($im); return $savepath; Equal To zoom} 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 * @par Am String $first determines the final position of the image, W width first h high priority WH: equal than * @return returns a picture with background * **/function addbg ($src, $w, $h, $fisrt = "W") {$BG =im Agecreatetruecolor ($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") {//etc than zoom return $SRC; } else {if ($fisrt = = "W") {$x = 0; $y = ($h-$height)/2;//Vertical Center} if ($fisrt = = "h") {$x = ($w-$width)/2;//horizontally centered $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);
I hope this article will help you learn PHP programming.
Articles you may be interested in:
- PHP picture upload class with picture display
- Simple PHP Image upload Program
- PHP Image upload Class Code
- PHP image upload storage source and can preview
- PHP Image Upload Code
- thinkphp Image upload function sharing
- Two examples of image compression implemented by PHP
- PHP implementation image upload and compression
http://www.bkjia.com/PHPjc/1084575.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084575.html techarticle PHP upload image and compression of the implementation method, this article explains the PHP upload image and compression of the implementation method, before a "PHP implementation of image upload and compression" has been a simple for everyone ...