Image processing: the implementation of watermarking and thumbnails (custom functions: watermark and thumbnail) code is a bit long, but not complex. Don't talk nonsense. paste the code:
The code is as follows:
/************************************
// Function: watermark ($ bigimg, $ smallimg, $ coord = 1)
// Function: add a watermark
// Parameters:
$ Bigimg is required. Large Image-watermark image
$ Smallimg is required. Small image
$ Coord is optional. Position of the watermark in a large image,
1 upper left corner; 2 upper right corner; 3 lower right corner; 4 lower left corner; 5 Middle Corner
// Example: watermark('datu.png ', 'xiaotu.png', 3); // add a watermark to datu.png. the watermark position is in the lower right corner.
*************************************/
Function watermark ($ bigimg, $ smallimg, $ coord = 1 ){
// Load two images and convert them to the encoding format recognized by php,
// It is equivalent to the imagecreate function, except that an empty image is created here.
$ Bi = getimagesize ($ bigimg );
Switch ($ bi [2]) {
Case 1:
$ Im1 = imagecreatefromgif ($ bigimg); break;
Case 2;
$ Im1 = imagecreatefromjpeg ($ bigimg); break;
Case 3;
$ Im1 = imagecreatefrompng ($ bigimg); break;
}
$ Si = getimagesize ($ smallimg );
Switch ($ si [2]) {
Case 1:
$ Im2 = imagecreatefromgif ($ smallimg); break;
Case 2;
$ Im2 = imagecreatefromjpeg ($ smallimg); break;
Case 3;
$ Im2 = imagecreatefrompng ($ smallimg); break;
}
// Create a watermark-principle: copy a small image to a large image. Pay attention to the calculation of coordinate values.
Switch ($ coord ){
Case 1:
Imagecopy ($ im1, $ im2, 0, 0, 0, 0, $ si [0], $ si [1]); break;
Case 2:
Imagecopy ($ im1, $ im2, $ bi [0]-$ si [0], 0, 0, 0, $ si [0], $ si [1]); break;
Case 3:
Imagecopy ($ im1, $ im2, $ bi [0]-$ si [0], $ bi [1]-$ si [1], 0, 0, $ si [0], $ si [1]); break;
Case 4:
Imagecopy ($ im1, $ im2, 0, $ bi [1]-$ si [1], 0, 0, $ si [0], $ si [1]); break;
Case 5:
Imagecopy ($ im1, $ im2, ($ bi [0]-$ si [0])/2, ($ bi [1]-$ si [1])/2, 0, 0, $ si [0], $ si [1]); break;
}
// Generate image files of different formats based on the suffix
Switch ($ bi [2]) {
Case 1:
Imagegif ($ im1); break;
Case 2;
Imagejpeg ($ im1); break;
Case 3;
Imagepng ($ im1); break;
}
Imagedestroy ($ im1 );
}
/*************************************** *********
// Function: thumbnail ($ srcimg, $ multiple)
// Function: generate a thumbnail
// Parameters:
// $ Srcimg is required. SOURCE image file name
// $ Multiple is optional. The scaling factor. the default value is 2 times, which is reduced to 1/2 of the original one.
// Note: only images in gif, jpg, and png formats are supported.
// Example: thumbnail('my Image .jpg ', 5 );
**************************************** *********/
Function thumbnail ($ srcimg, $ multiple = 2 ){
// Load the image and save its information to the array
$ Srcimg_arr = getimagesize ($ srcimg );
// Calculate the scaling factor
$ Thumb_width = $ srcimg_arr [0]/$ multiple;
$ Thumb_height = $ srcimg_arr [1]/$ multiple;
// Determine the format of the image to be created (converted to php-recognized encoding)
Switch ($ srcimg_arr [2]) {
Case 1:
$ Im = imagecreatefromgif ($ srcimg); break;
Case 2;
$ Im = imagecreatefromjpeg ($ srcimg); break;
Case 3;
$ Im = imagecreatefrompng ($ srcimg); break;
}
// Start scaling down
$ Thumb = imagecreatetruecolor ($ thumb_width, $ thumb_height );
Imagecopyresized ($ thumb, $ im, 0, 0, 0, $ thumb_width, $ thumb_height, $ srcimg_arr [0], $ srcimg_arr [1]);
Switch ($ srcimg_arr [2]) {
Case 1:
Imagegif ($ thumb); break;
Case 2;
Imagejpeg ($ thumb); break;
Case 3;
Imagepng ($ thumb); break;
}
Imagepng ($ thumb );
Imagedestroy ($ thumb );
}
// Do not use both functions during testing.
// Watermark('datu.png', 'xiaotu.png ', 5 );
Thumbnail('abc.png ', 3 );
?>