Currently, only three functions are available: 1: image scaling, 2: Image cropping, and 3: adding an image watermark.
In instantiation, different functions are implemented by passing different values to the second parameter.
Copy codeThe Code is as follows:
<? Php
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
Copy codeThe Code is as follows:
<? Php
/* 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 );
}
}
?>