PHP Create HD Thumbnail details how to use _php tutorial

Source: Internet
Author: User
Tags image identifier imagejpeg
PHP Tutorial Create HD Thumbnail details how to use

1. Replace Imagecreate and imagecopyresized with Imagecreatetruecolor and imagecopyresampled functions respectively
2. Take the third parameter to imagejpeg 100 (example: imagejpeg ($ni, $tofile, 100))

Imagecreatetruecolor--Create a new true color image
Description
Resource Imagecreatetruecolor (int x_size, int y_size)
Imagecreatetruecolor () returns an image identifier that represents a black image with a size 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 using the normal imagecreate () function will result in distorted picture quality, the solution is searched from the Internet, by replacing the imagecreate () function with the Imagecreateruecolor () function.
*/

function Createpreview ($img, $name, $path, $maxwidth, $maxheight, $quality) {//Picture, save name, save path, maximum width, maximum height, quality
$widthratio = 0;
$heightratio = 0;
$width =imagesx ($IMG);
$height =imagesy ($IMG);
Start calculating the scale-down
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;
}
Recalculate the width and height of thumbnails based on the resulting proportions
$newwidth = $ratio * $width;
$newheight = $ratio * $height;
$newimg =imagecreatetruecolor ($newwidth, $newheight); Create a target diagram
Imagecopyresized ($newimg, $img, 0,0,0,0, $newwidth, $newheight, $width, $height);
Imagejpeg ($newimg, $path. " S_ ". $name, $quality);
Imagedestroy ($NEWIMG);
}else{
Imagejpeg ($img, $path. " S_ ". $name, $quality);
}
}
/*

Imagecopyresamples (), the pixel interpolation algorithm gets a smoother image edge. Good quality (but the function is slower than imagecopyresized ().)

http://www.bkjia.com/PHPjc/633003.html www.bkjia.com true http://www.bkjia.com/PHPjc/633003.html techarticle PHP Tutorial Create HD Thumbnail details using Method 1. Replace Imagecreate and imagecopyresized 2 with the Imagecreatetruecolor and imagecopyresampled functions respectively. Give imagejpeg a third parameter ...

  • Related Article

    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.