PHP image processing: Add watermark, thumbnail implementation (Custom function: watermark, thumbnail) _php skills

Source: Internet
Author: User
Tags imagecopy imagejpeg
The nonsense does not say, pastes the code:
Copy Code code as follows:

<?php
/************************************
Functions: Watermark ($bigimg, $smallimg, $coord = 1)
Role: Add watermarks
Parameters:
$bigimg must be selected. Big picture--a picture to add a watermark
$smallimg must be selected. Small picture
$coord Optional. The position of the watermark in the larger 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); Watermark to Datu.png, watermark position in lower right corner
*************************************/
function watermark ($bigimg, $smallimg, $coord = 1) {
Load two pictures and turn it into a coded format that PHP recognizes,
is equivalent to the Imagecreate function, except that it is not an empty picture 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 small images to larger images. Here, pay attention to the calculation of coordinate values
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 a picture file of different formats according to the suffix name
Switch ($bi [2]) {
Case 1:
Imagegif ($im 1);
Case 2;
Imagejpeg ($im 1);
Case 3;
Imagepng ($im 1);
}
Imagedestroy ($im 1);
}
/************************************************
Functions: Thumbnail ($srcimg, $multiple)
Function: Generate a thumbnail image
Parameters:
$srcimg must be selected. Source Picture file name
$multiple Optional. Thumbnail multiples, default to twice times, that is, reduced to the original 1/2
Note: Only gif, JPG, PNG-formatted pictures are supported.
Example: Thumbnail (' my picture. jpg ', 5);
*************************************************/
function thumbnail ($srcimg, $multiple = 2) {
Load a picture and save its information to an array
$srcimg _arr = getimagesize ($srcimg);
Calculate the thumbnail multiple
$thumb _width = $srcimg _arr[0]/$multiple;
$thumb _height = $srcimg _arr[1]/$multiple;
Judge: What format image to build (convert to PHP-recognized encoding)
Switch ($srcimg _arr[2]) {
Case 1:
$im = Imagecreatefromgif ($srcimg);
Case 2;
$im = Imagecreatefromjpeg ($srcimg);
Case 3;
$im = Imagecreatefrompng ($srcimg);
}
Start the 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 of these functions while testing.
Watermark (' datu.png ', ' xiaotu.png ', 5);
Thumbnail (' Abc.png ', 3);
?>

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.