PHP scales up and down the image class by the aspect ratio-PHP source code

Source: Internet
Author: User
PHP supports jpg, png, and gif formats to scale up or down the image class according to the maximum width and height.

 Orpic = $ file; $ infos = getimagesize ($ file); $ this-> width = $ infos [0]; $ this-> height = $ infos [1]; $ this-> type = $ infos [2];} // calculate the thumbnail size based on the maximum width and height specified by the user. function cal_size ($ maxwidth, $ maxheight) {// ratio of the maximum width and height of a thumbnail $ thcrown = $ maxwidth/$ maxheight; // ratio of the original image width to height $ crown = $ this-> width/$ this-> height; if ($ crown/$ thcrown> = 1) {$ this-> thwidth = $ maxwidth; $ this-> thheight = $ maxwidth/$ crown ;} else {$ this-> thheight = $ Maxheight; $ this-> thwidth = $ maxheight * $ crown;} function init () {switch ($ this-> type) {case 1: // GIF $ this-> tempic = imagecreatefromgif ($ this-> orpic); break; case 2: // JPG $ this-> tempic = imagecreatefromjpeg ($ this-> orpic); break; case 3: // PNG $ this-> tempic = imagecreatefrompng ($ this-> orpic); break; default: echo 'This image format is not supported temporarily ';} function resize ($ maxwidth, $ maxheight) {// initialize the image $ this-> init (); // Calculate the thumbnail size $ this-> cal_size ($ maxwidth, $ maxheight); $ this-> thpic = imagecreatetruecolor ($ this-> thwidth, $ this-> thheight ); imagecopyresampled ($ this-> thpic, $ this-> tempic, 0, 0, 0, 0, $ this-> thwidth, $ this-> thheight, $ this-> width, $ this-> height);} function save ($ filename, $ type) {switch ($ type) {case 'jpg ': case 'jpeg ': imagejpeg ($ this-> thpic, $ filename); break; case 'PNG ': imagepng ($ this-> thpic, $ file Name); break; case 'GIF': imagegif ($ this-> thpic, $ filename); break; default: echo 'The format you selected is not supported temporarily '; }}}?>

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.