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 ).