How to Use php to create a high-definition thumbnail

Source: Internet
Author: User
Tags image identifier

Php tutorial how to create a high-definition thumbnail

1. Use imagecreatetruecolor and imagecopyresampled functions to replace imagecreate and imagecopyresized
2. Add 100 to the third parameter of imagejpeg (for example, imagejpeg ($ ni, $ tofile, 100 ))

Imagecreatetruecolor-create a true color image
Description
Resource imagecreatetruecolor (int x_size, int y_size)
Imagecreatetruecolor () returns an image identifier, representing a black image of x_size and y_size.

*/

Header ("content-type: image/png ");
$ Im = @ imagecreatetruecolor (50,100)
Or die ("cannot initialize new gd image stream ");
$ Text_color = imagecolorallocate ($ im, 233, 14, 91 );
Imagestring ($ im, 1, 5, 5, "a simple text string", $ text_color );
Imagepng ($ im );
Imagedestroy ($ im );

/*


If you use the common imagecreate () function, the image quality will be distorted. You can search the Internet for a solution by replacing the imagecreateruecolor () function with the imagecreate () function.
*/

Function createpreview ($ img, $ name, $ path, $ maxwidth, $ maxheight, $ quality) {// image, save name, save path, maximum width, maximum height, quality
$ Widthratio = 0;
$ Heightratio = 0;
$ Width = imagesx ($ img );
$ Height = imagesy ($ img );
// Start to calculate the scale-down ratio
If ($ width> $ maxwidth | $ height> $ maxheight ){
If ($ width> $ maxwidth ){
$ Widthratio = $ maxwidth/$ width;
}
If ($ height> $ maxheight ){
$ Heightratio = $ maxheight/$ height;
}
If ($ widthratio> 0 & $ heightratio> 0 ){
If ($ widthratio <$ heightratio ){
$ Ratio = $ widthratio;
} Else {
$ Ratio = $ heightratio;
}
} Elseif ($ widthratio> 0 ){
$ Ratio = $ widthratio;
} Elseif ($ heightratio> 0 ){
$ Ratio = $ heightratio;
}
// Calculate the width and height of the thumbnail Based on the obtained ratio.
$ Newwidth = $ ratio * $ width;
$ Newheight = $ ratio * $ height;
$ Newimg = imagecreatetruecolor ($ newwidth, $ newheight); // create a Target Image
Imagecopyresized ($ newimg, $ img, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height );
Imagejpeg ($ newimg, $ path. "s _". $ name, $ quality );
Imagedestroy ($ newimg );
} Else {
Imagejpeg ($ img, $ path. "s _". $ name, $ quality );
}
}
/*

Imagecopyresamples (), the edge of the image obtained by the pixel interpolation algorithm is smoother and the quality is better (but the speed of this function is slower than that of imagecopyresized ).

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.