Detailed description of php-generated high-definition thumbnail instances

Source: Internet
Author: User
Tags imagejpeg
This article mainly introduces how to generate high-definition thumbnails in php, analyzes in detail the solution to distortion when generating thumbnails in php, and provides a complete example for summary and analysis, for more information about generating high-definition thumbnails in php, see the following example. We will share this with you for your reference. The details are as follows:

When using php functions to generate thumbnails,Thumbnails may be distorted in many cases. in this case, some corresponding solutions are required..

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

The following are specific functions.

Function CreateSmallImage ($ OldImagePath, $ NewImagePath, $ NewWidth = 154, $ NewHeight = 134) {// Retrieve the source image and obtain the image information. getimagesize parameter description: 0 (width ), 1 (high), 2 (1gif/2jpg/3png), 3 (width = "638" height = "340") $ OldImageInfo = getimagesize ($ OldImagePath ); if ($ OldImageInfo [2] = 1) $ OldImg = @ imagecreatefromgif ($ OldImagePath); elseif ($ OldImageInfo [2] = 2) $ OldImg = @ imagecreatefromjpeg ($ OldImagePath); else $ OldImg = @ imagecreatefrompng ($ OldImagePath); // create a graph. description of the imagecreate parameter: width, high $ NewImg = imagecreatetruecolor ($ NewWidth, $ NewHeight); // create a color, parameter: Graphic, red (0-255), green (0-255 ), blue (0-255) $ black = ImageColorAllocate ($ NewImg, 0, 0); // black $ white = ImageColorAllocate ($ NewImg, 255,255,255 ); // White $ red = ImageColorAllocate ($ NewImg, 255, 0, 0); // red $ blue = ImageColorAllocate ($ NewImg, 0, 0,255 ); // Blue $ other = ImageColorAllocate ($ NewImg, 0,255, 0 ); // New Image width processing $ WriteNewWidth = $ NewHeight * ($ OldImageInfo [0]/$ OldImageInfo [1]); // The height to be written $ WriteNewHeight = $ NewWidth * ($ OldImageInfo [1]/$ OldImageInfo [0]); // the width of the image to be written // The proportion of the image to be written will be out of proportion, but the background can be filled with if ($ OldImageInfo [0]/$ NewWidth> $ org_info [1]/$ NewHeight) {$ WriteNewWidth = $ NewWidth; $ WriteNewHeight = $ NewWidth/($ OldImageInfo [0]/$ OldImageInfo [1]);} else {$ WriteNewWidth = $ NewHeight * ($ OldImageInfo [0]/$ OldImageInfo [1]); $ WriteNewHeight = $ NewHeight;} // based on $ NewHeight, if the new width is less than or equal to $ NewWidth, if ($ WriteNewWidth <= $ NewWidth) {$ WriteNewWidth = $ WriteNewWidth; // The size after judgment $ WriteNewHeight = $ NewHeight; // use the specified size $ WriteX = floor ($ NewWidth-$ WriteNewWidth)/2); // calculate $ WriteY = 0 at the X position written on the new image ;} else {$ WriteNewWidth = $ NewWidth; // use the specified size $ WriteNewHeight = $ WriteNewHeight; // use the determined size $ WriteX = 0; $ WriteY = floor ($ NewHeight-$ WriteNewHeight)/2); // calculate the X position written to the new image} // after the old image is reduced, write to the new graph (copy), imagecopyresized parameter description: New and old, new xy old xy, new width and height old width and height @ imageCopyreSampled ($ NewImg, $ OldImg, $ WriteX, $ WriteY, 0, 0, $ WriteNewWidth, $ WriteNewHeight, $ OldImageInfo [0], $ OldImageInfo [1]); // Save the file // @ imagegif ($ NewImg, $ NewImagePath ); @ imagejpeg ($ NewImg, $ NewImagePath, 100); // end Image @ imagedestroy ($ NewImg);} CreateSmallImage (". /images/jiexie.jpg ",". /images/jiexie.small.jpg ", 200,300); CreateSmallImage (". /images/jiexie.jpg ",". /images/jiexie.middle.jpg ", 400,500 );

I hope this article will help you with php programming.

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.