Code sharing for generating image thumbnails in PHP based on GD2 graphics library

Source: Internet
Author: User
Tags imagejpeg php server
Code sharing for generating image thumbnails based on PHP of GD2 graphics library ,. Code sharing for generating image thumbnails using PHP based on GD2 graphics library. use PHP to generate image thumbnails, make sure that your PHP Server has installed the GD2 graphics library and uses a class to generate image thumbnails based on the GD2 graphics library,

To use PHP to generate image thumbnails, make sure that the GD2 graphics library is installed on your PHP server to generate image thumbnails using a class.

1. usage

$ Resizeimage = new resizeimage ("Image source file address", "200", "100", "0", "thumbnail address"); // only use the preceding sentence, you can generate a thumbnail. the source file and the thumbnail address can be the same. 200,100 represents the width and height respectively.

2. thumbnail code

// Use the following classes to generate image thumbnails. <? Phpclass resizeimage {// image type var $ type; // The actual width var $ width; // the actual height var $ height; // The changed width var $ resize_width; // The height var $ resize_height after the change; // whether to cut the image var $ cut; // the source image var $ srcimg; // the target image address var $ dstimg; // create a temporary Image var $ im; function resizeimage ($ img, $ wid, $ hei, $ c, $ dstpath) {$ this-> srcimg = $ img; $ this-> resize_width = $ wid; $ this-> resize_height = $ hei; $ this-> cut = $ c; // Image type $ this-> type = strtolower (substr (Strrchr ($ this-> srcimg ,". "), 1); // initialize the image $ this-> initi_img (); // The destination image address $ this-> dst_img ($ dstpath ); // -- $ this-> width = imagesx ($ this-> im); $ this-> height = imagesy ($ this-> im ); // generate an image $ this-> newimg (); ImageDestroy ($ this-> im);} function newimg () {// ratio of the changed image $ resize_ratio = ($ this-> resize_width)/($ this-> resize_height ); // ratio of the actual image $ ratio = ($ this-> width)/($ this-> height); if ($ this-> cut) = "1 ") // Cut image {If ($ ratio >=$ resize_ratio) // high priority {$ newimg = imagecreatetruecolor ($ this-> resize_width, $ this-> resize_height); imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, $ this-> resize_width, $ this-> resize_height, ($ this-> height) * $ resize_ratio ), $ this-> height); ImageJpeg ($ newimg, $ this-> dstimg);} if ($ ratio <$ resize_ratio) // width first {$ newimg = imagecreatetruecolor ($ this-> resize_width, $ this-> resize_height); imag Ecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, $ this-> resize_width, $ this-> resize_height, $ this-> width, ($ this-> width)/$ resize_ratio); ImageJpeg ($ newimg, $ this-> dstimg );}} else // uncut graph {if ($ ratio >=$ resize_ratio) {$ newimg = imagecreatetruecolor ($ this-> resize_width, ($ this-> resize_width)/$ ratio ); imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, $ this-> resize_width, ($ this-> resize_width)/$ ratio, $ This-> width, $ this-> height); ImageJpeg ($ newimg, $ this-> dstimg);} if ($ ratio <$ resize_ratio) {$ newimg = imagecreatetruecolor ($ this-> resize_height) * $ ratio, $ this-> resize_height); imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, ($ this-> resize_height) * $ ratio, $ this-> resize_height, $ this-> width, $ this-> height); ImageJpeg ($ newimg, $ this-> dstimg) ;}}// initialize the image function initi_img () {if ($ this-> type = = "Jpg") {$ this-> im = imagecreatefromjpeg ($ this-> srcimg);} if ($ this-> type = "gif ") {$ this-> im = imagecreatefromgif ($ this-> srcimg);} if ($ this-> type = "png ") {$ this-> im = imagecreatefrompng ($ this-> srcimg) ;}// function dst_img ($ dstpath) {$ full_length = strlen ($ this-> srcimg); $ type_length = strlen ($ this-> type); $ name_length = $ full_length-$ type_length; $ name = substr ($ this-> srcimg, 0, $ name _ Length-1); $ this-> dstimg = $ dstpath; // echo $ this-> dstimg ;}}?>

Crop, you need to use PHP to generate image thumbnails. make sure that your PHP Server has installed the GD2 graphics library and uses a class to generate image thumbnails...

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.