Generate a thumbnail code proportionally-generate an image

Source: Internet
Author: User
Tags imagejpeg php tutorial

<? Php Tutorial
 
Class My_Lib_simpleimage {
Var $ image;
Var $ image_type;
Function load ($ filename ){
// $ Temporary file name of the filename File $ _ FILES ['name'] ['tmp _ name']
$ Image_info = getimagesize ($ filename );
$ This-> image_type = $ image_info [2];
If ($ this-> image_type = IMAGETYPE_JPEG ){
$ This-> image = imagecreatefromjpeg ($ filename );
} Elseif ($ this-> image_type = IMAGETYPE_GIF ){
$ This-> image = imagecreatefromgif ($ filename );
} Elseif ($ this-> image_type = IMAGETYPE_PNG ){
$ This-> image = imagecreatefrompng ($ filename );
  }
 }
 
Function save ($ filename, $ image_type = IMAGETYPE_JPEG, $ compression = 75, $ permissions = null ){
If ($ image_type = IMAGETYPE_JPEG ){
Imagejpeg ($ this-> image, $ filename, $ compression );
} Elseif ($ image_type = IMAGETYPE_GIF ){
Imagegif ($ this-> image, $ filename );
} Elseif ($ image_type = IMAGETYPE_PNG ){
Imagepng ($ this-> image, $ filename );
  }
If ($ permissions! = Null ){
Chmod ($ filename, $ permissions );
  }
 }
Function output ($ image_type = IMAGETYPE_JPEG ){
If ($ image_type = IMAGETYPE_JPEG ){
Imagejpeg ($ this-> image );
} Elseif ($ image_type = IMAGETYPE_GIF ){
Imagegif ($ this-> image );
} Elseif ($ image_type = IMAGETYPE_PNG ){
Imagepng ($ this-> image );
  }
 }
Function getWidth (){
Return imagesx ($ this-> image );
 }
Function getHeight (){
Return imagesy ($ this-> image );
 }
 
# Setting the height of a thumbnail
Function resizeToHeight ($ height ){
$ Ratio = $ height/$ this-> getHeight ();
$ Width = $ this-> getWidth () * $ ratio;
$ This-> resize ($ width, $ height );
 }
# Setting the width of a thumbnail
Function resizeToWidth ($ width ){
$ Ratio = $ width/$ this-> getWidth ();
$ Height = $ this-> getheight () * $ ratio;
$ This-> resize ($ width, $ height );
 }
Function scale ($ scale ){
$ Width = $ this-> getWidth () * $ scale/100;
$ Height = $ this-> getheight () * $ scale/100;
$ This-> resize ($ width, $ height );
 }
Function resize ($ width, $ height ){
$ New_image = imagecreatetruecolor ($ width, $ height );
Imagecopyresampled ($ new_image, $ this-> image, 0, 0, 0, 0, $ width, $ height, $ this-> getWidth (), $ this-> getHeight ());
$ This-> image = $ new_image;
 }
}
?>

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.