PHP generates image thumbnails-PHP source code

Source: Internet
Author: User
Tags imagejpeg
To generate image thumbnails using PHP, 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. php code

// Use the following classes to generate image thumbnails,
 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 graph {if ($ ratio> = $ resize_ratio) // height first {$ newimg = imagecreatetruecolor ($ this-> resize_width, $ this-> resize_height); imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, 0, $ this-> resize_width, $ this-> resize_height, ($ this-> height) * $ resize_ratio), $ this-> height); ImageJpeg ($ newimg, $ th Is-> dstimg);} if ($ ratio <$ resize_ratio) // width first {$ newimg = imagecreatetruecolor ($ this-> resize_width, $ this-> resize_height ); imagecopyresampled ($ 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, ($ thi S-> 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-> resiz E_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 ($ th Is-> 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 ;}}?>

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.