PHP implements two instance _ php skills for image compression

Source: Internet
Author: User
This article mainly introduces two examples of PHP image compression, which are of great practical value and worth learning. if you need it, you can refer to this article to introduce two methods of PHP image compression, readers can make improvements based on specific application references to meet their application needs! The main code is as follows:

Instance 1:

<? Php/*** desription compression image * @ param sting $ imgsrc Image path * @ param string $ Save path after imgdst compression */function image_png_size_add ($ imgsrc, $ imgdst) {list ($ width, $ height, $ type) = getimagesize ($ imgsrc); $ new_width = ($ width> 600? 600: $ width) * 0.9; $ new_height = ($ height> 600? 600: $ height) * 0.9; switch ($ type) {case 1: $ giftype = check_gifcartoon ($ imgsrc); if ($ giftype) {header ('content-Type: image/gif '); $ image_wp = imagecreatetruecolor ($ new_width, $ new_height); $ image = imagecreatefromgif ($ imgsrc); imagecopyresampled ($ image_wp, $ image, 0, 0, 0, 0, $ new_width, $ new_height, $ width, $ height); imagejpeg ($ image_wp, $ imgdst, 75); imagedestroy ($ image_wp);} break; case 2: header ('cont Ent-Type: image/jpeg '); $ image_wp = imagecreatetruecolor ($ new_width, $ new_height); $ image = imagecreatefromjpeg ($ imgsrc); imagecopyresampled ($ image_wp, $ image, 0, 0, 0, 0, $ new_width, $ new_height, $ width, $ height); imagejpeg ($ image_wp, $ imgdst, 75); imagedestroy ($ image_wp); break; case 3: header ('content-Type: image/png '); $ image_wp = imagecreatetruecolor ($ new_width, $ new_height); $ image = imagecreatefro Mpng ($ imgsrc); imagecopyresampled ($ image_wp, $ image, 0, 0, 0, 0, $ new_width, $ new_height, $ width, $ height); imagejpeg ($ image_wp, $ imgdst, 75); imagedestroy ($ image_wp); break ;}} /*** desription determine whether to perform GIF animation * @ param sting $ image_file Image path * @ return boolean t is f no */function check_gifcartoon ($ image_file) {$ fp = fopen ($ image_file, 'RB'); $ image_head = fread ($ fp, 1024); fclose ($ fp); return preg_match ("/". Chr (0x21). chr (0xff). chr (0x0b). 'netscape2. 0'. "/", $ image_head )? False: true;}?>

Instance 2:

<? Php/* ---------------------------------------------------------------------- function: adjust the Image size or generate a thumbnail. return: True/False parameter: $ Image the Image to be adjusted (including path) $ Dw = 450; the absolute width of the thumbnail $ Dh = 450 when the maximum height is adjusted; the absolute height of the thumbnail $ Type = 1 1, adjust the size; 2, generate the thumbnail $ path = 'IMG /'; // path $ phtypes = array ('IMG/GIF', 'IMG/jpg ', 'IMG/jpeg', 'IMG/bmp ', 'IMG/pjpeg ', 'IMG/x-png '); Function img ($ Image, $ Dw = 450, $ Dh = 450, $ Type = 1) {IF (! File_Exists ($ Image) {Return False;} // IF you want to generate a thumbnail, copy the source Image and assign the value IF ($ Type! = 1) {Copy ($ Image, Str_Replace (". "," _ x. ", $ Image); $ Image = Str_Replace (". "," _ x. ", $ Image);} // Obtain the object type. create different objects according to different types. $ ImgInfo = GetImageSize ($ Image); Switch ($ ImgInfo [2]) {Case 1: $ Img = @ ImageCreateFromGIF ($ Image); Break; Case 2: $ Img = @ ImageCreateFromJPEG ($ Image); Break; Case 3: $ Img = @ ImageCreateFromPNG ($ Image); Break;} // IF the object is not created successfully, it indicates that the non-Image file IF (Empty ($ Img )) {// IF an error occurs when a thumbnail is generated, delete the copied file IF ($ Type! = 1) {Unlink ($ Image);} Return False;} // IF you want to adjust the size, IF ($ Type = 1) {$ w = ImagesX ($ Img); $ h = ImagesY ($ Img); $ width = $ w; $ height = $ h; IF ($ width> $ Dw) {$ Par = $ Dw/$ width; $ width = $ Dw; $ height = $ height * $ Par; IF ($ height> $ Dh) {$ Par = $ Dh/$ height; $ height = $ Dh; $ width = $ width * $ Par;} ElseIF ($ height> $ Dh) {$ Par = $ Dh/$ height; $ height = $ Dh; $ width = $ width * $ Par; IF ($ width> $ Dw) {$ Par = $ Dw/$ width; $ width = $ Dw; $ height = $ height * $ Par ;}} Else {$ width = $ width; $ height = $ height ;}$ nImg = ImageCreateTrueColor ($ width, $ height ); // create a true color canvas ImageCopyReSampled ($ nImg, $ Img, $ width, $ height, $ w, $ h ); // re-sample and copy part of the Image and adjust the size of ImageJpeg ($ nImg, $ Image); // output the Image to the browser or file in JPEG format and Return True; // if a thumbnail is generated,} Else {$ w = ImagesX ($ Img); $ h = ImagesY ($ Img); $ width = $ w; $ height = $ h; $ nImg = ImageCreateTrueColor ($ Dw, $ Dh); IF ($ h/$ w> $ Dh/$ Dw) {// The height is relatively large $ widt H = $ Dw; $ height = $ h * $ Dw/$ w; $ IntNH = $ height-$ Dh; ImageCopyReSampled ($ nImg, $ Img, 0, -$ IntNH/1.8, 0, 0, $ Dw, $ height, $ w, $ h);} Else {// The width is relatively large $ height = $ Dh; $ width = $ w * $ Dh/$ h; $ IntNW = $ width-$ Dw; ImageCopyReSampled ($ nImg, $ Img,-$ IntNW/1.8, 0, 0, 0, $ width, $ Dh, $ w, $ h);} ImageJpeg ($ nImg, $ Image); Return True ;}}?><? Php if ($ _ SERVER ['request _ method'] = 'post') {if (! Is_uploaded_file ($ _ FILES ["photo"] [tmp_name]) {echo "the image does not exist"; exit () ;}if (! Is_dir ('IMG ') {// create mkdir ('IMG') if the path does not exist;} $ upfile = $ _ FILES ["photo"]; $ pinfo = pathinfo ($ upfile ["name"]); $ name = $ pinfo ['basename']; // file name $ tmp_name = $ upfile ["tmp_name"]; $ file_type = $ pinfo ['extension']; // Obtain the file type $ showphpath = $ path. $ name; if (in_array ($ upfile ["type"], $ phtypes) {echo "file type does not match! "; Exit () ;}if (move_uploaded_file ($ tmp_name, $ path. $ name) {echo" successful! "; Img ($ showphpath, 100,800, 2);} echo" ";}?>

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.