Php uploads an image watermark (image watermark, text watermark). This is a complete watermark that is automatically added to the image when the user uploads the image, this added watermark function can be used to add text and image watermarks.
/*
* Created on 2010-6-21
*
* The class for control image
*
* Made by s71ence
*
* @ $ Img_path: Image path
* @ $ Is_auto_reduce: whether the image is automatically compressed according to the size level. 1 is
* @ $ Is_appoint whether to manually compress or zoom in 1 is
* @ $ Multiple manually specify the compression/amplification ratio
* @ $ Is_water_str whether to add the watermark text 1 is
* @ $ Water_str watermark text
* @ $ Is_watermark whether to add watermark image 1 is
* @ $ Logo_path: watermark image path
* @ $ Is_display whether image 1 is displayed
* @ $ Is_create whether the compressed image 1 is generated
*
* Note:
* 1. When a new image is generated, the image cannot be displayed, that is, $ isdisplay and $ iscreate cannot be set to 1 at the same time.
* 2. When the image width or height is less than 1000, you must manually set the compression ratio for compression.
* 3. We do not recommend that you enable the watermark. To enable the watermark, we recommend that the size of the original image be less than 1000.
* 4. The watermark text cannot contain Chinese Characters
* 5. The newly generated images are in the original directory file and support n layers.
*/
Class image_control
{
Private $ img_path;
Private $ is_auto_reduce;
Private $ is_appoint;
Private $ multiple;
Private $ is_water_str;
Private $ water_str;
Private $ is_watermark;
Private $ logo_path;
Private $ is_display;
Private $ is_create;
Function _ construct ($ img_path, $ is_auto_reduce, $ is_appoint, $ multiple, $ is_water_str, $ water_str, $ is_watermark, $ logo_path, $ is_display, $ is_create)
{
$ This-> img_path = $ img_path;
$ This-> is_auto_reduce = $ is_auto_reduce;
$ This-> is_appoint = $ is_appoint;
$ This-> multiple = $ multiple;
$ This-> is_water_str = $ is_water_str;
$ This-> water_str = $ water_str;
$ This-> is_watermark = $ is_watermark;
$ This-> logo_path = $ logo_path;
$ This-> is_display = $ is_display;
$ This-> is_create = $ is_create;
}
Function img_control ()
{
// Obtain the source Image
$ Img_info = getimagesize ($ this-> img_path );
Switch ($ img_info [2])
{
Case 1:
$ Img_get = @ imagecreatefromgif ($ this-> img_path );
Break;
Case 2:
$ Img_get = @ imagecreatefromjpeg ($ this-> img_path );
Break;
Case 3:
$ Img_get = @ imagecreatefrompng ($ this-> img_path );
Break;
}
// Text watermark
If ($ this-> is_water_str = 1)
{
// Imagettftext (source image, text size, text rotation, watermark start coordinate x, watermark start coordinate y, $ te, 'simhei. ttf', $ str );
$ Te = imagecolorallocate ($ img_get, 255,255,255 );
$ Str = iconv ("gbk", "UTF-8", $ this-> water_str); // watermark text
Imagettftext ($ img_get, 16, 0, $ img_info [0]-200, $ img_info [1]-20, $ te, 'msyh. ttf', $ str );
}
// Image Watermark
If ($ this-> is_watermark = 1)
{
// Watermark image processing
$ Logo_info = getimagesize ($ this-> logo_path );
Switch ($ logo_info [2])
{
Case 1:
$ Logo = @ imagecreatefromgif ($ this-> logo_path );
Break;
Case 2:
$ Logo = @ imagecreatefromjpeg ($ this-> logo_path );
Break;
Case 3:
$ Logo = @ imagecreatefrompng ($ this-> logo_path );
Break;
}
// Watermark logo image
// Function Description: imagecopy (source image, watermark image, watermark coordinate x, watermark coordinate y, watermark image start coordinate x, watermark image start coordinate y, and 'watermark image width ', 'watermark image high ');
Imagecopy ($ img_get, $ logo, 0, 0, 0, $ logo_info [0], $ logo_info [1]);
}
// Automatic Image Compression: auto Compression Based on Image Size
// Imagecopyresized (canvas, source image, canvas start x coordinate, canvas start y coordinate, source image start x coordinate, source image start x coordinate, new image width, New Image Height, original image width, original Image Height );
If ($ this-> is_auto_reduce = 1)
{
If ($ img_info [0] >=3000 | $ img_info [1] >=3000)
{
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.03, $ img_info [1] * 0.03); // generate a canvas
Imagecopyresized ($ new_image_get, $ img_get, 0.03, 0.03, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
}
Else if ($ img_info [0]> = 2500 | $ img_info [1]> = 2500)
{
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.04, $ img_info [1] * 0.04 );
Imagecopyresized ($ new_image_get, $ img_get, 0.04, 0.04, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
}
Else if ($ img_info [0]> = 2000 | $ img_info [1]> = 2000)
{
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.05, $ img_info [1] * 0.05 );
Imagecopyresized ($ new_image_get, $ img_get, 0.05, 0.05, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
}
Else if ($ img_info [0]> = 1500 | $ img_info [1]> = 1500)
{
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.08, $ img_info [1] * 0.08 );
Imagecopyresized ($ new_image_get, $ img_get, 0.08, 0.08, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
}
Else if ($ img_info [0]> = 1000 | $ img_info [1]> = 1000)
{
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.1, $ img_info [1] * 0.1 );
Imagecopyresized ($ new_image_get, $ img_get, 0.1, 0.1, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
}
Else if ($ img_info [0]> = 500 | $ img_info [1]> = 500)
{
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.2, $ img_info [1] * 0.2 );
Imagecopyresized ($ new_image_get, $ img_get, 0.2, 0.2, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
}
Else if ($ img_info [0]> = 300 | $ img_info [1]> = 300)
{
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.3, $ img_info [1] * 0.3 );
Imagecopyresized ($ new_image_get, $ img_get, 0.3, 0.3, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
}
Else
{
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 1, $ img_info [1] * 1 );
Imagecopyresized ($ new_image_get, $ img_get, 0, 0, 0, $ img_info [0] * 1, $ img_info [1] * 1, $ img_info [0], $ img_info [1]);
}
}
// Manual Image Compression
// Imagecopyresized (canvas, source image, canvas start x coordinate, canvas start y coordinate, source image start x coordinate, source image start x coordinate, new image width, New Image Height, original image width, original Image Height );
If ($ this-> is_appoint)
{
$ New_image_get = imagecreatetruecolor ($ img_info [0] * $ this-> multiple, $ img_info [1] * $ this-> multiple); // generate a canvas
Imagecopyresized ($ new_image_get, $ img_get, 0, 0, 0, $ img_info [0] * $ this-> multiple, $ img_info [1] * $ this-> multiple, $ img_info [0], $ img_info [1]);
}
// Image output
If ($ this-> is_display = 1)
{
Header ("content-type: image/jpeg ");
Return imagejpeg ($ new_image_get );
}
// New Image Generation
If ($ this-> is_create = 1)
{
$ New_name = explode ("/", $ this-> img_path );
$ New_name_string = "";
For ($ I = 0; $ I <count ($ new_name)-1; $ I ++)
{
$ New_name_string. = $ new_name [$ I]. "/";
}
$ New_img_path = $ new_name_string. "new". $ new_name [$ I];
If (imagejpeg ($ new_image_get, $ new_img_path) & imagejpeg ($ img_get, $ this-> img_path ))
{
Setcookie ("img_new_path", $ new_img_path );
// Return "image generated successfully! <Br/> New Image: ". $ new_img_path." <br/> source image: ". $ this-> img_path;
}
Else
{
Return "Image Generation failed. Please check whether the configuration is correct! ";
}
}
}
Function _ desctruct ()
{
// Clear
}
}