When uploading images to push and expand websites, we may add watermarks to images or scale down the images. The following Code provides this function.
When uploading images to push and expand websites, we may add watermarks to images or scale down the images. The following Code provides this function.
<? Php tutorial
/************************************
// 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;
}
1 2