Php simple thumbnail class | image. class. php_PHP tutorial

Source: Internet
Author: User
Tags imagejpeg
Php simple thumbnail class | image. class. php. How to use: unzip imgnewiamge$img-resize(dstimg.jpg, srcimg.jpg, 300,400); Note: This is scaled in proportion, dstimg.jpgis the target file, srcimg.jpg is the source file, and the following method is used:


$ Img = new iamge;
$ Img-> resize('dstimg.jpg ', 'srcimg.jpg', 300,400 );
Note: dstimg.jpg is the target file, and srcimg.jpg is the source file, followed by the width and height of the target file.
$ Img-> thumb('dstimg.jpg ', 'scrimg.jpg', 300,300 );
Note: This is a pay-as-you-go chart. dstimg.jpg is the target file, and srcimg.jpg is the source file, followed by the width and height of the target file.
This is so troublesome for the GD Library. if Imagick is used, you only need two functions to implement the above functions. you can find it by checking the document.

Class image {

Public function resize ($ dstImg, $ srcImg, $ dstW, $ dsomething ){
List ($ srcW, $ srcH) = getimagesize ($ srcImg );
$ Scale = min ($ dstW/$ srcW, $ dsomething/$ srcH );
$ NewW = round ($ srcW * $ scale );
$ NewH = round ($ srcH * $ scale );
$ NewImg = imagecreatetruecolor ($ newW, $ newH );
$ SrcImg = imagecreatefromjpeg ($ srcImg );
Imagecopyresampled ($ newImg, $ srcImg, 0, 0, 0, $ newW, $ newH, $ srcW, $ srcH );
Imagejpeg ($ newImg, $ dstImg );
}

Public function thumb ($ dstImg, $ srcImg, $ dstW, $ dsomething ){
List ($ srcW, $ srcH) = getimagesize ($ srcImg );
$ Scale = max ($ dstW/$ srcW, $ dsomething/$ srcH );
$ NewW = round ($ dstW/$ scale );
$ NewH = round ($ dsomething/$ scale );
$ X = ($ srcW-$ newW)/2;
$ Y = ($ srcH-$ newH)/2;
$ NewImg = imagecreatetruecolor ($ dstW, $ dsomething );
$ SrcImg = imagecreatefromjpeg ($ srcImg );
Imagecopyresampled ($ newImg, $ srcImg, 0, 0, $ x, $ y, $ dstW, $ dsomething, $ newW, $ newH );
Imagejpeg ($ newImg, $ dstImg );
}

}

Function createFromType ($ type, $ srcImg ){
$ Function = "imagecreatefrom $ type ";
Return $ function ($ srcImg );
}
// To solve the problem of different image formats

Export $ img = new iamge; export img-resize('dstimg.jpg ', 'srcimg.jpg', 300,400); Note: This is scaled in proportion, dstimg.jpgis the target file, srcimg.jpg is the source file, followed...

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.