PHP generation of proportional thumbnails class and user-defined function sharing _ php instance

Source: Internet
Author: User
Tags imagejpeg
This article mainly introduces PHP generation of proportional thumbnails class and user-defined function sharing, respectively encapsulated into a class and user-defined function, for more information, see the following two proportional thumbnails.
1. It is a class file and can be used after being instantiated.
2. It is lightweight for custom methods

Class File

The Code is as follows:

$ Resizeimage = new resizeimage ("./shawn.jpg", "200", "100", "0", "../pic/shawnsun.jpg ");
// Instantiate the following class to generate a thumbnail
// The source file and thumbnail address can be the same, 200,100 represents the width and height, and the fourth parameter is optional, 0 is not, 1 is


The Code is as follows:

<? Php
Class resizeimage {

// Image type
Public $ type;
// Actual width
Public $ width;
// Actual height
Public $ height;
// Change the width
Public $ resize_width;
// The height after the change
Public $ resize_height;
// Whether to cut the image
Public $ cut;
// Source Image
Public $ srcimg;
// Target image address
Public $ dstimg;
// Temporarily created image
Public $ im;

Function resizeimage ($ img, $ wid, $ hei, $ c, $ dstpath ){

$ This ---> srcimg = $ img;
$ This-> resize_width = $ wid;
$ This-> resize_height = $ hei;
$ This-> cut = $ c;

// Image type
$ This-> type = strtolower (substr (strrchr ($ this-> srcimg, "."), 1 ));
// Initialize the image
$ This-> initi_img ();
// Target image address
$ This-> dst_img ($ dstpath );
// W & H
$ This-> width = imagesx ($ this-> im );
$ This-> height = imagesy ($ this-> im );
// Generate an image
$ This-> newimg ();
ImageDestroy ($ this-> im );
}

Function newimg (){

// Ratio of the changed image
$ Resize_ratio = ($ this-> resize_width)/($ this-> resize_height );
// Ratio of the actual image
$ Ratio = ($ this-> width)/($ this-> height );

If ($ this-> cut) = "1 ")
// Cut the image
{
If ($ ratio> = $ resize_ratio)
// High priority
{
$ Newimg = imagecreatetruecolor ($ this-> resize_width, $ this-> resize_height );
Imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, $ this-> resize_width,
$ This-> resize_height, ($ this-> height) * $ resize_ratio ),
$ This-> height
);
ImageJpeg ($ newimg, $ this-> dstimg );
}
If ($ ratio <$ resize_ratio)
// The width is preferred.
{
$ Newimg = imagecreatetruecolor ($ this-> resize_width, $ this-> resize_height );
Imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, $ this-> resize_width,
$ This-> resize_height, $ this-> width,
($ This-> width)/$ resize_ratio)
);
ImageJpeg ($ newimg, $ this-> dstimg );
}
}
Else
// No cut chart
{
If ($ ratio> = $ resize_ratio)
{
$ Newimg = imagecreatetruecolor ($ this-> resize_width,
($ This-> resize_width)/$ ratio
);
Imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, $ this-> resize_width,
($ This-> resize_width)/$ ratio, $ this-> width,
$ This-> height
);
ImageJpeg ($ newimg, $ this-> dstimg );
}
If ($ ratio <$ resize_ratio)
{
$ Newimg = imagecreatetruecolor ($ this-> resize_height) * $ ratio,
$ This-> resize_height
);
Imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0,
($ This-> resize_height) * $ ratio,
$ This-> resize_height, $ this-> width,
$ This-> height
);
ImageJpeg ($ newimg, $ this-> dstimg );
}
}
}

// Initialize the image
Function initi_img (){

If ($ this-> type = "jpg ")
{
$ This-> im = imagecreatefromjpeg ($ this-> srcimg );
}
If ($ this-> type = "gif ")
{
$ This-> im = imagecreatefromgif ($ this-> srcimg );
}
If ($ this-> type = "png ")
{
$ This-> im = imagecreatefrompng ($ this-> srcimg );
}
}
// Target image address
Function dst_img ($ dstpath ){

$ Full_length = strlen ($ this-> srcimg );
$ Type_length = strlen ($ this-> type );
$ Name_length = $ full_length-$ type_length;

$ Name = substr ($ this-> srcimg, 0, $ name_length-1 );
$ This-> dstimg = $ dstpath;

// Echo $ this-> dstimg;
}
}

?>

Custom Method

The Code is as follows:


Thumbs('shawn.jpg', 'shawnsun.jpg ', 100,100 );
// The parameter property is similar to method 1.

The Code is as follows:


<? Php

Function thumbs ($ FileName, $ SaveTo, $ SetW, $ SetH ){
$ IMGInfo = getimagesize ($ FileName );
If (! $ IMGInfo) return false;

If ($ IMGInfo ['mime '] = "image/pjpeg" | $ IMGInfo ['mime'] = "image/jpeg "){
$ ThisPhoto = imagecreatefromjpeg ($ FileName );
} Elseif ($ IMGInfo ['mime '] = "image/x-png" | $ IMGInfo ['mime'] = "image/png "){
$ ThisPhoto = imagecreatefrompng ($ FileName );
} Elseif ($ IMGInfo ['mime '] = "image/gif "){
$ ThisPhoto = imagecreatefromgif ($ FileName );
}

$ Width = $ IMGInfo [0];
$ Height = $ IMGInfo [1];
$ Scalc = max ($ width/$ SetW, $ height/$ SetH );
$ Nw = intval ($ width/$ scalc );
$ Nh = intval ($ height/$ scalc );
Echo "Scaling: w $ nw, h $ nh
";

If ($ SetW-$ nw = 1) {$ nw = $ SetW ;}
If ($ SetH-$ nh = 1) {$ nh = $ SetH ;}
Echo "+ corrected 1 pixel: w $ nw, h $ nh
";

// Fill the width
If ($ SetW-$ nw> 0 ){
$ Nh = $ nh + ($ nh/$ nw) * ($ SetW-$ nw ));
Echo "* fill in width ". ($ SetW-$ nw ). ", accompany makeup ". ($ nh/$ nw) * ($ SetW-$ nw ))."
";
$ Nw = $ SetW;
}
// Fill the height
If ($ SetH-$ nh> 0 ){
$ Nw = $ nw + ($ nw/$ nh) * ($ SetH-$ nh ));
Echo "* need to fill in height ". ($ SetH-$ nh ). ", accompany fill width ". ($ nw/$ nh) * ($ SetH-$ nh ))."
";
$ Nh = $ SetH;
}
$ Nw = intval ($ nw );
$ Nh = intval ($ nh );
Echo "+ corrected size: w $ nw, h $ nh
";

$ Px = ($ SetW-$ nw)/2;
$ Py = ($ SetH-$ nh)/2;
Echo "window size: w $ SetW, h $ SetH
";
Echo "+ offset correction: x $ px, y $ py
";

$ NewPhoto = imagecreatetruecolor ($ SetW, $ SetH );
Imagecopyresized ($ NewPhoto, $ ThisPhoto, $ px, $ py, 0, $ nw, $ nh, $ width, $ height );
ImageJpeg ($ NewPhoto, $ SaveTo );
Return true;
}

?>

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.