Php: How to upload and compress images. _ PHP Tutorial

Source: Internet
Author: User
Tags imagejpeg learn php programming
Php uploads and compresses images ,. How to upload and compress images in php. This document describes how to upload and compress images in php, in the previous article "PHP implements image upload and compression", we have implemented a simple php method for uploading and compressing images,

This article describes how to upload and compress images in php. the previous article "PHP implements image upload and compress" has provided a brief introduction, upload the image and specify the maximum height or width of the thumbnail according to the Scaled thumbnail. The details are as follows:

Implementation code:

<? Php function _ UPLOADPIC ($ upfile, $ maxsize, $ updir, $ newname = 'Date') {if ($ newname = 'Date ') $ newname = date ("Ymdhis"); // use the date as the 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 ': $ exten D = ". png"; break;} if (emptyempty ($ extend) {echo ("Warning! Only image types can be uploaded: gif jpg png "); exit ();} if ($ size> $ maxsize) {$ maxpr = $ maxsize/1000; echo (" Warning! The size of the uploaded image 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 $ ra2 = 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/$ ra2);} 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 an image, return resource type * @ param string $ src Image 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 ;} /*** main function of thumbnail ** @ param string $ src Image path * @ param int $ w width of the thumbnail * @ 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"]; // the folder where the file is located $ extension = $ temp ["extension"]; // the file extension $ savepath = "{$ dir}/{$ name }"; // the path for saving the thumbnail. the new file name is * .thumb.jpg // get the basic information of the image $ info = getImageInfo ($ src); $ width = $ info [0]; // Obtain the image width $ height = $ info [1]; // Obtain the image height $ per1 = round ($ width/$ height, 2 ); // calculate the aspect ratio of the source image $ per2 = round ($ w/$ h, 2); // calculate Thumbnail aspect ratio // calculate the zoom ratio if ($ per1> $ per2 | $ per1 = $ per2) {// The aspect ratio of the source image is greater than or equal to the aspect ratio of the thumbnail, then, according to the width priority $ per =w w/$ width;} if ($ per1 <$ per2) {// The aspect ratio of the source image is smaller than the aspect ratio of the thumbnail, $ per = $ h/$ height;} $ temp_w = intval ($ width * $ per ); // calculate the scaled width of the source image $ temp_h = intval ($ height * $ per); // calculate the scaled height of the source image $ temp_img = imagecreatetruecolor ($ temp_w, $ temp_h ); // create a canvas $ im = create ($ src); imagecopyresampled ($ temp_img, $ im, 0, 0, 0, $ temp_w, $ temp_h, $ width, $ height ); if ($ per1> $ per2) {Imagejpeg ($ temp_img, $ savepath, 100); imagedestroy ($ im); return addBg ($ savepath, $ w, $ h, "w"); // The width is preferred, add the background when the height is insufficient after scaling.} if ($ per1 === per2) {imagejpeg ($ temp_img, $ savepath, 100); imagedestroy ($ im ); return $ savepath; // proportional scaling} if ($ per1 <$ per2) {imagejpeg ($ temp_img, $ savepath, 100); imagedestroy ($ im ); return addBg ($ savepath, $ w, $ h, "h"); // high priority, add the background when the width is insufficient after scaling}/*** add the background * @ param string $ src Image 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 height first wh: equals * @ return returns the image with the background added **/function addBg ($ src, $ w, $ h, $ fisrt = "w ") {$ bg = imagecreatetruecolor ($ w, $ h); $ white = imagecolorallocate ($ bg, 255,255,255); imagefill ($ bg, $ white ); // fill in the background // get the target image information $ info = getImageInfo ($ src); $ width = $ info [0]; // target image width $ height = $ info [1]; // target Image height $ img = create ($ src); if ($ fisrt = "wh "){ // Proportional scaling return $ src;} else {if ($ fisrt = "w") {$ x = 0; $ y = ($ h-$ height)/2; // Vertical center} if ($ fisrt = "h") {$ x = ($ w-$ width)/2; // horizontally center $ y = 0 ;} imagecopymerge ($ bg, $ img, $ x, $ y, 100, $ width, $ height, 100); imagejpeg ($ bg, $ src ); imagedestroy ($ bg); imagedestroy ($ img); return $ src ;}}?>

Usage:

$filename=(_UPLOADPIC($_FILES["upload"],$maxsize,$updir,$newname='date'));   $show_pic_scal=show_pic_scal(230, 230, $filename);   resize($filename,$show_pic_scal[0],$show_pic_scal[1]); 

I hope this article will help you learn php programming.

Articles you may be interested in:
  • PHP image upload class with image display
  • Simple PHP image upload program
  • Php image upload code
  • Php image Upload storage source code and can be previewed
  • PHP image upload code
  • Thinkphp implements image upload function sharing
  • Two PHP image compression instances
  • PHP uploads and compresses images

Examples: This article illustrates how php uploads and compresses images. the previous article "PHP uploads and compresses images" has made it simple for you...

Related Article

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.