Home >Backend Development >PHP Tutorial >PHP watermark and thumbnail implementation code (1/2)_PHP tutorial

PHP watermark and thumbnail implementation code (1/2)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:45:51844browse

When uploading pictures, we may add watermarks to the pictures or reduce the size of the pictures in order to promote the website. The code provided below has this function.

When uploading pictures, we may add watermarks to the pictures or reduce the size of the pictures in order to promote the website. The code provided below has this function.

/************************************
//Function: watermark($bigimg, $smallimg, $coord = 1)
//Function: Add watermark
//Parameters:
$bigimg Required. Large pictures--pictures to be watermarked
$smallimg Required. Small picture
$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 pictures and convert them into the encoding format recognized by PHP,
//Equal to the imagecreate function, except that 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 to the calculation of coordinate values ​​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 according to 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)
//Function: Generate a thumbnail
//Parameters:
// $srcimg 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 images in gif, jpg, and png formats.
//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 abbreviation 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;
}

1 2

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632998.htmlTechArticleWhen uploading pictures, in order to promote the website, we may add watermarks to the pictures, or reduce the size of the pictures, as provided below The code has this function. Uploading pictures in order to promote the website...
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