Ask questions about generating thumbnails in php!

Source: Internet
Author: User
Ask questions about generating thumbnails in php! The following code can generate image thumbnails, but the image quality will be reduced. who can help modify the image to control the quality of the generated image?



/*
* Function: PHP image watermark (watermarks support images or text)
* Parameters:
* $ GroundImage: specifies the background image to be Watermark. Currently, only GIF, JPG, and PNG formats are supported;
* $ WaterPos watermark position, which has 10 statuses. 0 indicates a random position;
* 1 is the top left, 2 is the top center, and 3 is the top right;
* 4: center left, 5: center, and 6: center right;
* 7 indicates that the bottom is left, 8 indicates that the bottom is center, and 9 indicates that the bottom is right;
* $ WaterImage: specifies the image Watermark. Currently, only the GIF, JPG, and PNG formats are supported;
* $ WaterText text watermark: uses text as a watermark. it supports ASCII codes and does not support Chinese characters;
* $ FontSize: the text size. The value is 1, 2, 3, 4, or 5. the default value is 5;
* $ TextColor: text color. The value is a hexadecimal color value. The default value is # CCCCCC (white gray );
* $ Fontfile ttf font file, which is used to set the font of the text watermark. Windows users are in the system disk directory.
* Search *. ttf to get the font files installed in the system and copy the files to the appropriate directory on the website,
* The default value is arial. ttf in the current directory.
* $ XOffset horizontal offset, that is, this value is added based on the default watermark coordinate value. The default value is 0.
* You can set this value for the top margin of the outlet square. for example, 2 indicates that two units are shifted to the right based on the default value.-2 indicates that two units are shifted to the left.
* $ YOffset vertical offset, that is, add this value based on the default watermark coordinate value. The default value is 0.
* You can set this value for the margin in the vertical direction. for example, 2 indicates that two units are moved down based on the default value.-2 indicates that two units are moved up.
* Return value:
* 0 watermark successful
* 1 the watermark image format is not supported currently.
* 2 the background image to be watermark does not exist
* 3 the watermark cannot be generated because the length or width of the image to be watermark is smaller than that of the watermark image or text area.
* 4 the font file does not exist.
* 5 incorrect watermark text color format
* 6 currently, the watermark background image format is not supported.
* Modification record:
*
* Note: Support GD 2.0, Support FreeType, GIF Read, GIF Create, JPG, and PNG
* $ WaterImage and $ waterText should not be used at the same time. select one of them and use $ waterImage first.
* When $ waterImage is valid, $ waterString, $ stringFont, and $ stringColor are invalid.
* The name of the watermark image is the same as that of $ groundImage.
* Prepared by: gaoxilin
* Date: 2007-4-28
* Note: This program is rewritten based on longware.
*/

// Call method:
// $ ImageWaterMark = imageWaterMark ("1.jpg", 9," 3.png", "", "-50", "-50 ");



Function imageWaterMark ($ groundImage, $ waterPos = 0, $ waterImage = "", $ waterText = "", $ fontSize = 12, $ textColor = "# CCCCCC ", $ fontfile = '. /arial. ttf', $ xOffset = 0, $ yOffset = 0)
{
$ IsWaterImage = FALSE;
// Read the watermark file
If (! Empty ($ waterImage) & file_exists ($ waterImage )){
$ IsWaterImage = TRUE;
$ Water_info = getimagesize ($ waterImage );
$ Water_w = $ water_info [0]; // Obtain the watermark image width.
$ Water_h = $ water_info [1]; // Get the watermark image height

Switch ($ water_info [2]) {// Obtain the watermark image format
Case 1: $ water_im = imagecreatefromgif ($ waterImage); break;
Case 2: $ water_im = imagecreatefromjpeg ($ waterImage); break;
Case 3: $ water_im = imagecreatefrompng ($ waterImage); break;
Default: return 1;
}
}

// Read the background image
If (! Empty ($ groundImage) & file_exists ($ groundImage )){
$ Ground_info = getimagesize ($ groundImage );
$ Ground_w = $ ground_info [0]; // Obtain the width of the background image.
$ Ground_h = $ ground_info [1]; // get the height of the background image

Switch ($ ground_info [2]) {// get the background image format
Case 1: $ ground_im = imagecreatefromgif ($ groundImage); break;
Case 2: $ ground_im = imagecreatefromjpeg ($ groundImage); break;
Case 3: $ ground_im = imagecreatefrompng ($ groundImage); break;
Default: return 1;
}
} Else {
Return 2;
}

// Watermark Position
If ($ isWaterImage) {// image watermark
$ W = $ water_w;
$ H = $ water_h;
$ Label = "image ";
} Else {
// Text watermark
If (! File_exists ($ fontfile) return 4;
$ Temp = imagettfbbox ($ fontSize, 0, $ fontfile, $ waterText); // get the text range using the TrueType font
$ W = $ temp [2]-$ temp [6];
$ H = $ temp [3]-$ temp [7];
Unset ($ temp );
}
If ($ ground_w <$ w) | ($ ground_h <$ h )){
Return 3;
}
Switch ($ waterPos ){
Case 0: // random
$ PosX = rand (0, ($ ground_w-$ w ));
$ PosY = rand (0, ($ ground_h-$ h ));
Break;
Case 1: // 1 is the top left
$ PosX = 0;
$ PosY = 0;
Break;
Case 2: // 2 center the top
$ PosX = ($ ground_w-$ w)/2;
$ PosY = 0;
Break;
Case 3: // 3: top right
$ PosX = $ ground_w-$ w;
$ PosY = 0;
Break;
Case 4: // 4 is left in the middle
$ PosX = 0;
$ PosY = ($ ground_h-$ h)/2;
Break;
Case 5: // 5 center in the middle
$ PosX = ($ ground_w-$ w)/2;
$ PosY = ($ ground_h-$ h)/2;
Break;
Case 6: // 6 is the center and right
$ PosX = $ ground_w-$ w;
$ PosY = ($ ground_h-$ h)/2;
Break;
Case 7: // 7 is left at the bottom
$ PosX = 0;
$ PosY = $ ground_h-$ h;
Break;
Case 8: // 8 is centered at the bottom
$ PosX = ($ ground_w-$ w)/2;
$ PosY = $ ground_h-$ h;
Break;
Case 9: // 9: right
$ PosX = $ ground_w-$ w;
$ PosY = $ ground_h-$ h;
Break;
Default: // random
$ PosX = rand (0, ($ ground_w-$ w ));
$ PosY = rand (0, ($ ground_h-$ h ));
Break;
}

// Set the mixed color mode of the image
Imagealphablending ($ ground_im, true );

If ($ isWaterImage) {// image watermark
Imagecopy ($ ground_im, $ water_im, $ posX + $ xOffset, $ posY + $ yOffset, 0, 0, $ water_w, $ water_h); // Copy the watermark to the target file.
} Else {// text watermark
If (! Empty ($ textColor) & (strlen ($ textColor) = 7 )){
$ R = hexdec (substr ($ textColor, 1, 2 ));
$ G = hexdec (substr ($ textColor, 3, 2 ));
$ B = hexdec (substr ($ textColor, 5 ));
} Else {
Return 5;
}
Imagettftext ($ ground_im, $ fontSize, 0, $ posX + $ xOffset, $ posY + $ h + $ yOffset, imagecolorallocate ($ ground_im, $ R, $ G, $ B ), $ fontfile, $ waterText );
}

// The image after the watermark is generated
@ Unlink ($ groundImage );
Switch ($ ground_info [2]) {// get the background image format
Case 1: imagegif ($ ground_im, $ groundImage); break;
Case 2: imagejpeg ($ ground_im, $ groundImage); break;
Case 3: imagepng ($ ground_im, $ groundImage); break;
Default: return 6;
}

// Release the memory
If (isset ($ water_info) unset ($ water_info );
If (isset ($ water_im) imagedestroy ($ water_im );
Unset ($ ground_info );
Imagedestroy ($ ground_im );
//
Return 0;
}
?>


Reply to discussion (solution)

Case 1: imagegif ($ ground_im, $ groundImage); break;
Case 2: imagejpeg ($ ground_im, $ groundImage); break;
Case 3: imagepng ($ ground_im, $ groundImage); break;

Changed:
$ Quality = 100;
Case 1: imagegif ($ ground_im, $ groundImage, $ quality); break;
Case 2: imagejpeg ($ ground_im, $ groundImage, $ quality); break;
Case 3: imagepng ($ ground_im, $ groundImage, (int) ($ quality-1)/10); break;

Available on the second floor. thank you.

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.