Php image processing code sharing _ PHP Tutorial

Source: Internet
Author: User
Php image processing code sharing. Currently, only three functions are implemented: 1: image scaling, 2: Image cropping, and 3: adding an image watermark to the instance. by passing different values to the second parameter, so that different features can be copied. Currently, only three functions are available: 1: image scaling, 2: Image cropping, and 3: Adding image watermarks.
In instantiation, different functions are implemented by passing different values to the second parameter.

The code is as follows:


Include "image. class. php ";
$ Image = new image ("2.png", 1," 300 "," 500 "," 5.png"); // use the image scaling function
$ Image = new image ("2.png", 2," "," 50 "," 5.png"); // use the image cropping function
$ Image = new image ("2.png", 3," 1.png", "0", "5.png"); // use the image watermark function
$ Image-> outimage ();
?>


PHP code

The code is as follows:


/* Known issues: 1. in the image scaling function, use the imagecreatetruecolor function to create a canvas and use a transparent processing algorithm. However, images in PNG format cannot be transparent. Using the imagecreate function to create a canvas can solve this problem, but the scaled image color is too small.
*
*
* Type value:
* (1): indicates that the image scaling function is used. $ value1 indicates the width of the scaled image, and $ value2 indicates the height of the scaled image.
* (2): indicates that the image cropping function is used. $ value1 indicates the coordinates of the starting point of the cropping. for example, starting from the origin, it is "0, 0", followed by the x axis, followed by the Y axis, separated by commas (,). $ value2 indicates the width and height of the crop. it is also used in the form of "20, 20 ".
* (3): adds an image Watermark. $ value1 indicates the watermark image file name, and $ value2 indicates the watermark position in the image. you can select 10 values, 1 stands for top left, 2 stands for middle left, 3 stands for left and right, 4 stands for center left, 5 stands for center, 6 stands for center right, 7 stands for bottom, 8 stands for bottom center, 9 stands for bottom right, 0 represents a random location
*
*/
Class image {
Private $ types; // The ID of the function used. the value 1 indicates the image scaling function. the value 2 indicates the image cropping function. the value 3 indicates the image watermarking function.
Private $ imgtype; // Image format
Private $ image; // image resources
Private $ width; // image width
Private $ height; // Image height
Private $ value1; // $ value1 represents different values based on the type value.
Private $ value2; // $ value2 represents different values based on different type values.
Private $ endaddress; // output address + file name
Function _ construct ($ imageaddress, $ types, $ value1 = "", $ value2 = "", $ endaddress ){
$ This-> types = $ types;
$ This-> image = $ this-> imagesources ($ imageaddress );
$ This-> width = $ this-> imagesizex ();
$ This-> height = $ this-> imagesizey ();
$ This-> value1 = $ value1;
$ This-> value2 = $ value2;
$ This-> endaddress = $ endaddress;
}
Function outimage () {// outputs different functions based on the input type value
Switch ($ this-> types ){
Case 1:
$ This-> scaling ();
Break;
Case 2:
$ This-> clipping ();
Break;
Case 3:
$ This-> imagewater ();
Break;
Default:
Return false;
}
}
Private function imagewater () {// Add image watermark function
// Use a function to obtain the length and width of the watermark file
$ Imagearrs = $ this-> getimagearr ($ this-> value1 );
// Call the function to calculate the position where the watermark is loaded.
$ Positionarr = $ this-> position ($ this-> value2, $ imagearrs [0], $ imagearrs [1]);
// Add a watermark
Imagecopy ($ this-> image, $ this-> imagesources ($ this-> value1), $ positionarr [0], $ positionarr [1], 0, 0, $ imagearrs [0], $ imagearrs [1]);
// Call the output method to save
$ This-> output ($ this-> image );
}
Private function clipping () {// Image cropping function
// Assign the passed values to the variables respectively.
List ($ src_x, $ src_y) = explode (",", $ this-> value1 );
List ($ dst_w, $ dst_h) = explode (",", $ this-> value2 );
If ($ this-> width <$ src_x + $ dst_w | $ this-> height <$ src_y + $ dst_h) {// this judgment means that the image cannot be captured outside the image.
Return false;
}
// Create a new canvas resource
$ Newimg = imagecreatetruecolor ($ dst_w, $ dst_h );
// Crop
Imagecopyresampled ($ newimg, $ this-> image, 0, 0, $ src_x, $ src_y, $ dst_w, $ dst_h, $ dst_w, $ dst_h );
// Call the output method to save
$ This-> output ($ newimg );
}
Private function scaling () {// image scaling function
// Obtain the width and height of proportional scaling
$ This-> proimagesize ();
// Scale according to parameters and call the output function to save the processed files
$ This-> output ($ this-> imagescaling ());
}
Private function imagesources ($ imgad) {// Obtain the image type and open the image resource
$ Imagearray = $ this-> getimagearr ($ imgad );
Switch ($ imagearray [2]) {
Case 1: // gif
$ This-> imgtype = 1;
$ Img = imagecreatefromgif ($ imgad );
Break;
Case 2: // jpeg
$ This-> imgtype = 2;
$ Img = imagecreatefromjpeg ($ imgad );
Break;
Case 3: // png
$ This-> imgtype = 3;
$ Img = imagecreatefrompng ($ imgad );
Break;
Default:
Return false;
}
Return $ img;
}
Private function imagesizex () {// Obtain the image width
Return imagesx ($ this-> image );
}
Private function imagesizey () {// Obtain the image height
Return imagesy ($ this-> image );
}
Private function proimagesize () {// calculate the width and height of the scaled image.
If ($ this-> value1 & ($ this-> width <$ this-> height) {// proportional scaling algorithm
$ This-> value1 = round ($ this-> value2/$ this-> height) * $ this-> width );
} Else {
$ This-> value2 = round ($ this-> value1/$ this-> width) * $ this-> height );
}
}
Private function imagescaling () {// image scaling function to return processed image resources
$ Newimg = imagecreatetruecolor ($ this-> value1, $ this-> value2 );
$ Tran = imagecolortransparent ($ this-> image); // process transparent algorithms
If ($ tran >=0 & $ tran <imagecolorstotal ($ this-> image )){
$ Tranarr = imagecolorsforindex ($ this-> image, $ tran );
$ Newcolor = imagecolorallocate ($ newimg, $ tranarr ['red'], $ tranarr ['green'], $ tranarr ['blue']);
Imagefill ($ newimg, 0, 0, $ newcolor );
Imagecolortransparent ($ newimg, $ newcolor );
}
Imagecopyresampled ($ newimg, $ this-> image, 0, 0, 0, 0, $ this-> value1, $ this-> value2, $ this-> width, $ this-> height );
Return $ newimg;
}
Private function output ($ image) {// output image
Switch ($ this-> imgtype ){
Case 1:
Imagegif ($ image, $ this-> endaddress );
Break;
Case 2:
Imagejpeg ($ image, $ this-> endaddress );
Break;
Case 3:
Imagepng ($ image, $ this-> endaddress );
Break;
Default:
Return false;
}
}
Private function getimagearr ($ imagesou) {// returns an array of image attributes
Return getimagesize ($ imagesou );
}
Private function position ($ num, $ width, $ height) {// returns the coordinates of a position based on the input number. $ width and $ height indicate the width and height of the inserted Image respectively.
Switch ($ num ){
Case 1:
$ Positionarr [0] = 0;
$ Positionarr [1] = 0;
Break;
Case 2:
$ Positionarr [0] = ($ this-> width-$ width)/2;
$ Positionarr [1] = 0;
Break;
Case 3:
$ Positionarr [0] = $ this-> width-$ width;
$ Positionarr [1] = 0;
Break;
Case 4:
$ Positionarr [0] = 0;
$ Positionarr [1] = ($ this-> height-$ height)/2;
Break;
Case 5:
$ Positionarr [0] = ($ this-> width-$ width)/2;
$ Positionarr [1] = ($ this-> height-$ height)/2;
Break;
Case 6:
$ Positionarr [0] = $ this-> width-$ width;
$ Positionarr [1] = ($ this-> height-$ height)/2;
Break;
Case 7:
$ Positionarr [0] = 0;
$ Positionarr [1] = $ this-> height-$ height;
Break;
Case 8:
$ Positionarr [0] = ($ this-> width-$ width)/2;
$ Positionarr [1] = $ this-> height-$ height;
Break;
Case 9:
$ Positionarr [0] = $ this-> width-$ width;
$ Positionarr [1] = $ this-> height-$ height;
Break;
Case 0:
$ Positionarr [0] = rand (0, $ this-> width-$ width );
$ Positionarr [1] = rand (0, $ this-> height-$ height );
Break;
}
Return $ positionarr;
}
Function _ destruct (){
Imagedestroy ($ this-> image );
}
}
?>

In the instantiation process, different values are passed to the second parameter to achieve different function replication...

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.