Php packaging gd Library

Source: Internet
Author: User
The php-encapsulated gd library uses thinkphp of SAEs to implement thumbnails on the local device and SAEs. thumbnails are mainly used on the Waterfall Stream. Otherwise, a picture is 23 MB, which is too slow, I encapsulated one myself and smiled! & Lt ;? PhpclassThumb {set the maximum width to use and display private $ max_widthnull in the editor; private php encapsulates the gd Library

Using thinkphp of sae version can not implement thumbnails on the local and SAEs. thumbnails are mainly used on the Waterfall Stream. Otherwise, a picture is 2 3 M, which is too slow. you can encapsulate it yourself!


  Max_width = $ max_widht; $ this-> file_name = $ file_name; $ this-> water_name = $ water_name;} public function create_image () {// Obtain the ori Image Information list ($ width, $ height, $ type) = getimagesize ($ this-> file_name); // when the original image is larger than the required maximum width, if ($ width> $ this-> max_width) {// get the image compression percentage $ per = $ this-> max_width/$ width; $ new_width = $ width * $ per; $ new_height = $ height * $ per;} else {$ new_height = $ height; $ new_width = $ Width;} // create a true color image $ image_p = imagecreatetruecolor ($ new_width, $ new_height-10); $ image = $ this-> image_obj ($ type, $ this-> file_name); imagecopyresampled ($ image_p, $ image, 0, 0, 0, 0, $ new_width, $ new_height, $ width, $ height ); $ this-> image_dump ($ type, $ image_p, $ this-> file_name); $ this-> water ();} /** generate an image and add a watermark */private function water () {$ water_name = $ this-> water_name; $ dist_name = $ this-> f Ile_name; list ($ dist_width, $ dist_height, $ type) = getimagesize ($ dist_name); $ dist_im = $ this-> image_obj ($ type, $ this-> file_name ); $ water_name = "D:/xampps/htdocs/buyingfeiblog/1/App/Modules/Admin/Tpl/Public/Images/water.png"; list ($ w_width, $ w_height) = getimagesize ($ water_name); // Obtain the image watermark information $ water_src = imagecreatefrompng ($ water_name); // set the image watermark position in the lower right corner $ x = ($ dist_width-$ w_width) /4*3; $ y = ($ Dist_height-$ w_height)/4*3; if (imagecopy ($ dist_im, $ water_src, $ x, $ y, 0, 0, $ w_width, $ w_height )) {imagepng ($ dist_im, $ dist_name); echo "success" ;}else {echo "error" ;}// generate the image type, keep the original image type unchanged when generating different images. private function image_dump ($ type, $ image_p, $ filename) {switch ($ type) {case 1: imagegif ($ image_p, $ filename); $ dis_im = imagecreatefromgif (file_name); break; case 2: imagejpeg ($ image_p, $ this -> File_name); $ dis_im = imagecreatefromjpeg (file_name); break; case 3: imagepng ($ image_p, file_name); $ dis_im = imagecreatefrompng (file_name); break; default :}} // different resource objects are generated based on different images. private function image_obj ($ type, $ filename) {switch ($ type) {// 1 = GIF, 2 = JPG, 3 = PNG, case 1: $ image = imagecreatefromgif ($ filename); break; case 2: $ image = imagecreatefromjpeg ($ filename); break; case 3: $ image = im Agecreatefrompng ($ filename); break; default:} return $ image; }}$ thumb = new Thumb (725, "D:/xampps/htdocs/test/test.jpg "); $ thumb-> create_image (); // create_image?>

It's that simple,
This mainly includes generating a true color chart,
Create graphical object resources
Merge images. OK!

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.