Home  >  Article  >  Backend Development  >  PHP image processing: Implementation of adding watermarks and thumbnails (custom functions: watermark, thumbnail)_PHP tutorial

PHP image processing: Implementation of adding watermarks and thumbnails (custom functions: watermark, thumbnail)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:33:21796browse

Enough nonsense, post the code:

Copy the code The code is as follows:

/** **********************************
//Function: watermark($bigimg, $smallimg, $ coord = 1)
//Function: Add watermark
//Parameters:
$bigimg Required. Large picture--picture to be watermarked
$smallimg Required. Small image
$coord Optional. The position of the watermark in the large image,
1 upper left corner; 2 upper right corner; 3 lower right corner; 4 lower left corner; 5 middle
//Example: watermark('datu.png', 'xiaotu.png', 3); //Add 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 into the encoding format recognized by PHP,
//Equivalent to imagecreate function, but what is created here is not 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 watermark - principle: copy the small image to the large image. Pay attention here 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 pictures in different formats based on the suffix name File
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 image
//Parameters:
// $srcimg is required. Source image file name
// $multiple optional. Shrinking multiple, the default is 2 times, that is, reduced to 1/2 of the original size
//Note: Only supports gif, jpg, and png format images.
//Example: thumbnail('my picture.jpg', 5);
**************************** **********************/
function thumbnail($srcimg , $multiple = 2){
//Load the image and save its information to the array
$srcimg_arr = getimagesize($srcimg);
//Calculate the thumbnail multiple
$thumb_width = $srcimg_arr [0] / $multiple;
$thumb_height = $srcimg_arr[1] / $multiple;
//Judge: what format of image to create (convert to encoding 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 thumbnailing operation
$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 these two functions at the same time when testing.
//watermark('datu.png','xiaotu.png',5);
thumbnail('abc.png',3);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322620.htmlTechArticleStop talking nonsense, paste the code: Copy the code as follows: ?php /******** ****************************** //Function: watermark($bigimg, $smallimg, $coord = 1) //Function: Add Watermark/...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn