Code for generating thumbnails of php complete images proportionally-PHP source code

Source: Internet
Author: User
Tags imagejpeg
Ec (2); & lt ;? Phprequire (resize_img.php); howtousetheclass: requests. $ imgresizenewresize_img (); $ imgresize script ec (2); script

Require ('resize _ img. php ');
// How to use the class:
// Makes a simple thumbnail of an image of 100x100 and saves the image then outputs it.
$ Imgresize = new resize_img ();

$ Imgresize-> sizelimit_x = 100;
$ Imgresize-> sizelimit_y = 100;
$ Imgresize-> keep_proportions = true;
$ Imgresize-> output = 'png ';

If ($ imgresize-> resize_image ('C:/xampp/htdocs/wannabe/image_bin/public/treeshadows_001.gif ') = false)
{
Echo 'error! ';
}
Else
{
$ Imgresize-> save_resizedimage ('C:/xampp/htdocs/wannabe/image_bin/public/thumbnails/', 'treeshadows _ 001 ');
$ Imgresize-> output_resizedimage ();
}

$ Imgresize-> destroy_resizedimage ();

?>

// Resize_img.php File

/**
* By Daniel M. Story
*/

Class resize_img
{
Var $ image_path = '';
// Holds the image path
Var $ sizelimit_x = 250;
// The limit of the image width
Var $ sizelimit_y = 250;
// The limit of the image height
Var $ image_resource = '';
// Holds the image resource
Var $ keep_proportions = true;
// If true it keeps tutorial the image proportions when resized
Var $ resized_resource = '';
// Holds the resized image resource
Var $ hasGD = false;
Var $ output = 'same ';
// Can be JPG, GIF, PNG, or SAME (same will save as old type)

Function resize_img ()
{
If (function_exists ('gd _ info') {$ this-> hasGD = true ;}
}

Function resize_image ($ image_path)
{
If ($ this-> hasGD === false) {return false ;}
// No GD installed on the server!

List ($ img_width, $ img_height, $ img_type, $ img_attr) = @ getimagesize ($ image_path );
// This is going to get the image width, height, and format

If ($ img_width! = 0) | ($ img_width! = 0 ))
// Make sure it was loaded correctly
{
Switch ($ img_type)
{
Case 1:
// GIF
$ This-> image_resource = @ imagecreatefromgif ($ image_path );
If ($ this-> output = 'same') {$ this-> output = 'gif ';}
Break;
Case 2:
// JPG
$ This-> image_resource = @ imagecreatefromjpeg ($ image_path );
If ($ this-> output = 'same') {$ this-> output = 'jpg ';}
Break;
Case 3:
// PNG
$ This-> image_resource = @ imagecreatefrompng ($ image_path );
If ($ this-> output = 'same') {$ this-> output = 'png ';}
}
If ($ this-> image_resource = '') {return false ;}
// It wasn' t able to load the image
}
Else {return false ;}
// Something happened!

If ($ this-> keep_proportions = true)
{
If ($ img_width-$ this-> sizelimit_x)> ($ img_height-$ this-> sizelimit_y ))
{
// If the width of the img is greater than the size limit we scale by width
$ Scalex = ($ this-> sizelimit_x/$ img_width );
$ Scaley = $ scalex;
}
Else
// If the height of the img is greater than the size limit we scale by height
{
$ Scalex = ($ this-> sizelimit_y/$ img_height );
$ Scaley = $ scalex;
}

}
Else
{
$ Scalex = ($ this-> sizelimit_x/$ img_width );
$ Scaley = ($ this-> sizelimit_y/$ img_height );
// Just make the image fit the image size limit

If ($ scalex> 1) {$ scalex = 1 ;}
If ($ scaley> 1) {$ scaley = 1 ;}
// Don't make it so it streches the image
}

$ New_width = $ img_width * $ scalex;
$ New_height = $ img_height * $ scaley;

$ This-> resized_resource = @ imagecreatetruecolor ($ new_width, $ new_height );
// Creates an image resource, with the width and height of the size limits (or new resized proportion)

If (function_exists ('imageantialias') {@ imageantialias ($ this-> resized_resource, true );}
// Helps in the quality of the image being resized

@ Imagecopyresampled ($ this-> resized_resource, $ this-> image_resource, 0, 0, 0, 0, $ new_width, $ new_height, $ img_width, $ img_height );
// Resize the iamge onto the resized resource

@ Imagedestroy ($ this-> image_resource );
// Destory old image resource

Return true;
}

Function save_resizedimage ($ path, $ name)
{
Switch (strtoupper ($ this-> output ))
{
Case 'gif ':
// GIF
@ Imagegif ($ this-> resized_resource, $ path. $ name. '.gif ');
Break;
Case 'jpg ':
// JPG
@ Imagejpeg ($ this-> resized_resource, $ path. $ name. '.jpg ');
Break;
Case 'png ':
// PNG
@ Imagepng ($ this-> resized_resource, $ path. $ name. '.png ');
}
}

Function output_resizedimage ()
{
$ The_time = time ();
Header ('Last-Modified: '. date ('d, d m y h: I: s', $ the_time). 'gmt ');
Header ('cache-Control: public ');

Switch (strtoupper ($ this-> output ))
{
Case 'gif ':
// GIF
Header ('content-type: image/gif ');
@ Imagegif ($ this-> resized_resource );
Break;
Case 'jpg ':
// JPG
Header ('content-type: image/jpg ');
@ Imagejpeg ($ this-> resized_resource );
Break;
Case 'png ':
// PNG
Header ('content-type: image/png ');
@ Imagepng ($ this-> resized_resource );
}
}

Function destroy_resizedimage ()
{
@ Imagedestroy ($ this-> resized_resource );
@ Imagedestroy ($ this-> image_resource );
}

}

?>

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.