PHP image processing: Add watermark, thumbnail implementation (custom functions: watermark, thumbnail) _php tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg
Nonsense not to say, paste code:
Copy CodeThe code is as follows:
/************************************
Function: Watermark ($bigimg, $smallimg, $coord = 1)
Role: Adding watermarks
Parameters:
$bigimg must be selected. Big picture--a picture to add a watermark
$smallimg must be selected. Small picture
$coord is optional. The position of the watermark in the larger image,
1 upper left corner, 2 right upper corner, 3 right lower corner, 4 lower left corner, 5 middle
Example: Watermark (' datu.png ', ' Xiaotu.png ', 3); Watermark the datu.png in the lower right corner
*************************************/
function watermark ($bigimg, $smallimg, $coord = 1) {
Load two images and turn it into a PHP encoded format for identification,
is equivalent to the Imagecreate function, except that it is not an empty image created here.
$bi = getimagesize ($bigimg);
Switch ($bi [2]) {
Case 1:
$im 1 = imagecreatefromgif ($bigimg);
Case 2;
$im 1 = imagecreatefromjpeg ($bigimg);
Case 3;
$im 1 = imagecreatefrompng ($bigimg);
}
$si = getimagesize ($smallimg);
Switch ($si [2]) {
Case 1:
$im 2 = imagecreatefromgif ($smallimg);
Case 2;
$im 2 = imagecreatefromjpeg ($smallimg);
Case 3;
$im 2 = imagecreatefrompng ($smallimg);
}
Create a watermark-principle: Copy the small image onto the larger image. Note the calculation of the coordinate values here
Switch ($coord) {
Case 1:
Imagecopy ($im 1, $im 2, 0, 0, 0, 0, $si [0], $si [1]); Break
Case 2:
Imagecopy ($im 1, $im 2, $bi [0]-$si [0], 0, 0, 0, $si [0], $si [1]); Break
Case 3:
Imagecopy ($im 1, $im 2, $bi [0]-$si [0], $bi [1]-$si [1], 0, 0, $si [0], $si [1]); Break
Case 4:
Imagecopy ($im 1, $im 2, 0, $bi [1]-$si [1], 0, 0, $si [0], $si [1]); Break
Case 5:
Imagecopy ($im 1, $im 2, ($bi [0]-$si [0])/2, ($bi [1]-$si [1])/2, 0, 0, $si [0], $si [1]); Break
}
Generate picture files in different formats based on suffix name
Switch ($bi [2]) {
Case 1:
Imagegif ($im 1); break;
Case 2;
Imagejpeg ($im 1); break;
Case 3;
Imagepng ($im 1); break;
}
Imagedestroy ($im 1);
}
/************************************************
function: Thumbnail ($srcimg, $multiple)
Function: Generate a thumbnail image
Parameters:
$srcimg must be selected. Source Picture file name
$multiple is optional. The default is twice times, which is reduced to the original 1/2
Note: Only gif, JPG, PNG format pictures are supported.
Example: Thumbnail (' My pictures. jpg ', 5);
*************************************************/
function thumbnail ($srcimg, $multiple = 2) {
Load a picture and save its information to an array
$srcimg _arr = getimagesize ($srcimg);
Calculating the thumbnail multiples
$thumb _width = $srcimg _arr[0]/$multiple;
$thumb _height = $srcimg _arr[1]/$multiple;
Judging: What format to create the image (converted to PHP identification code)
Switch ($srcimg _arr[2]) {
Case 1:
$im = Imagecreatefromgif ($srcimg);
Case 2;
$im = Imagecreatefromjpeg ($srcimg);
Case 3;
$im = Imagecreatefrompng ($srcimg);
}
Start thumbnail 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 both functions at the same time for testing.
Watermark (' datu.png ', ' xiaotu.png ', 5);
Thumbnail (' Abc.png ', 3);
?>

http://www.bkjia.com/PHPjc/322620.html www.bkjia.com true http://www.bkjia.com/PHPjc/322620.html techarticle nonsense, paste code: Copy the Code as follows:? PHP/************************************//function: Watermark ($bigimg, $smallimg, $coord = 1)//function : Add Watermark/...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.