PHP image processing: watermark thumbnail implementation (custom function: watermark thumbnail)
- 2020-03-31 21:17:53
- OfStack
Nonsense said, paste the code:
<?php
/************************************
//Function: watermark($bigimg, $smallimg, $coord = 1)
//Effect: add watermark
//Parameters:
$bigimg Will be selected. The big picture -- Image to be watermarked
$smallimg Will be selected. Small pictures
$coord Optional. The position of the watermark in the large image,
1 The top left corner. 2 The top right corner. 3 The lower right corner. 4 The lower left corner. 5 In the middle
// Example: watermark('datu.png', 'xiaotu.png', 3); // to datu.png Mark with a watermark in the lower right corner
*************************************/
function watermark($bigimg, $smallimg, $coord = 1){
//Load two images and convert them to the encoding recognized by PHP,
//It's the same thing as the imagecreate function, except instead of creating an empty image.
$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 - how it works: copy a small image onto a large one. Notice the calculation of the coordinates here
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 in different formats based on the suffix name
switch($bi[2]){
case 1:
imagegif($im1);break;
case 2;
imagejpeg($im1);break;
case 3;
imagepng($im1);break;
}
imagedestroy($im1);
}
/************************************************
//Function: thumbnail($srcimg, $multiple)
//Action: generate a thumbnail image
//Parameters:
//$srcimg will choose. Source image file name
//$multiple optional. The thumbnail multiple, by default, is 2, which is reduced to 1/2
//Note: only GIF, JPG and PNG images are supported.
//Example: thumbnail(' my picture.jpg ', 5);
*************************************************/
function thumbnail($srcimg, $multiple = 2){
//Load the image and save its information into an array
$srcimg_arr = getimagesize($srcimg);
//Calculate the thumbnail multiple
$thumb_width = $srcimg_arr[0] / $multiple;
$thumb_height = $srcimg_arr[1] / $multiple;
//Judgment: what format of image to create (converted to code recognized by PHP)
switch($srcimg_arr[2]){
case 1:
$im = imagecreatefromgif($srcimg);break;
case 2;
$im = imagecreatefromjpeg($srcimg);break;
case 3;
$im = imagecreatefrompng($srcimg);break;
}
//Start thumbnail
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresized($thumb, $im, 0, 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 when testing.
//watermark('datu.png','xiaotu.png',5);
thumbnail('abc.png',3);
?>