PHP image Processing Class (watermark, transparency, zoom, sharpen, rotate, flip, cut, invert)

Source: Internet
Author: User
Tags imagecopy watermark images
Very powerful PHP image processing class, you can customize the image watermark, transparency, image scaling, image sharpening, picture rotation, picture flipping, picture cutting, picture inverse color. The specific code is as follows:

/**
* Image processing functions: Zoom, cut, frame, watermark, sharpen, rotate, flip, transparency, invert color
* Process and save the history of ideas: when there are changes to the image automatically generated a new picture, naming method can be considered in the original image based on the addition of steps, for example: Picture name +__ First Steps
* Website: http://www.scutephp.com
*/
Class picture{
var $PICTURE _url; Picture to work on
var $DEST _url = "Temp__01.jpg"; Generate Target Picture location
var $PICTURE _create; The picture you want to create
var $TURE _color; Create a new True color image


var $PICTURE _width; Original image width
var $PICTURE _height; Original image Height


/**
* The type of watermark, the default is the watermark text
*/
var $MARK _type = 1;
var $WORD; After UTF-8 the text
var $WORD _x; Text Horizontal
var $WORD _y; Text ordinate
var $FONT _type; Font type
var $FONT _size = "12"; Font size
var $FONT _word; Text
var $ANGLE = 0; The angle of the text, which defaults to 0
var $FONT _color = "#000000"; Text color
var $FONT _path = "Font/simkai.ttf"; Font library, default to Arial
var $FORCE _url; Watermark Picture
var $FORCE _x = 0; Watermark Horizontal
var $FORCE _y = 0; Watermark Ordinate
var $FORCE _start_x = 0; Cut the image of the watermark on the horizontal axis
var $FORCE _start_y = 0; Cut up the image of the watermark ordinate


var $PICTURE _type; Type of picture
var $PICTURE _mime; The head of the output


/**
* Zoom to zoom height and width by 1
*/
var $ZOOM = 1; Scale type
var $ZOOM _multiple; Zoom ratio
var $ZOOM _width; Ease of reduction
var $ZOOM _height; Zoom height


/**
* Cut, proportional and fixed length, width
*/
var $CUT _type = 1; Cutting type
var $CUT _x = 0; Cutting the horizontal axis
var $CUT _y = 0; Trimming the Ordinate
var $CUT _width = 100; Width of cut
var $CUT _height = 100; Height of crop cut


/**
* Sharpening
*/
var $SHARP = "7.0"; Degree of sharpening


/**
* Transparency Processing
*/
var $ALPHA = ' 100 '; Transparency is between 0-127
var $ALPHA _x = "90";
var $ALPHA _y = "50";

/**
* Rotation at any angle
*/
var $CIRCUMROTATE = "90.0"; Note that a floating-point number must be


/**
* Error message
*/
var $ERROR = Array (
' unalviable ' = ' didn't find the relevant picture! '
);

/**
* Constructor: Function initialization
*/
function __construct ($PICTURE _url) {

$this-Get_info ($PICTURE _url);

}
function Get_info ($PICTURE _url) {
/**
* Processing the original picture information, first detect whether the image exists, does not exist to give the corresponding information
*/
@ $SIZE = getimagesize ($PICTURE _url);
if (! $SIZE) {
Exit ($this-error[' unalviable ');
}

Get the information type, width, height of the original picture
$this-picture_mime = $SIZE [' MIME '];
$this-picture_width = $SIZE [0];
$this-Picture_height = $SIZE [1];

Create a picture
Switch ($SIZE [2]) {
Case 1:
$this-picture_create = Imagecreatefromgif ($PICTURE _url);
$this-Picture_type = "imagejpeg";
$this-picture_ext = "JPG";
Break
Case 2:
$this-picture_create = Imagecreatefromjpeg ($PICTURE _url);
$this-Picture_type = "Imagegif";
$this-picture_ext = "gif";
Break
Case 3:
$this-picture_create = Imagecreatefrompng ($PICTURE _url);
$this-Picture_type = "Imagepng";
$this-picture_ext = "png";
Break
}

/**
* Text color conversion 16 binary conversion to 10 binary
*/
Preg_match_all ("/([0-f]) {2,2}/i", $this-Font_color, $MATCHES);
if (count ($MATCHES) = = 3) {
$this, RED = Hexdec ($MATCHES [0][0]);
$this-GREEN = Hexdec ($MATCHES [0][1]);
$this BLUE = Hexdec ($MATCHES [0][2]);
}
}

# End of __construct
/**
* Convert 16 colors into 10 binary (R,G,B)
*/
function Hex2dec () {
Preg_match_all ("/([0-f]) {2,2}/i", $this-Font_color, $MATCHES);
if (count ($MATCHES) = = 3) {
$this, RED = Hexdec ($MATCHES [0][0]);
$this-GREEN = Hexdec ($MATCHES [0][1]);
$this BLUE = Hexdec ($MATCHES [0][2]);
}
}

Scale type
function Zoom_type ($ZOOM _type) {
$this, ZOOM = $ZOOM _type;
}

Scale the picture if you don't specify a height and width
function Zoom () {
Size of the scale
if ($this, ZOOM = = 0) {
$this-zoom_width = $this, Picture_width * $this, zoom_multiple;
$this-zoom_height = $this, Picture_height * $this, zoom_multiple;
}
Create a new True color image
$this-True_color = Imagecreatetruecolor ($this, Zoom_width, $this-zoom_height);
$WHITE = Imagecolorallocate ($this, True_color, 255, 255, 255);
Imagefilledrectangle ($this, true_color, 0, 0, $this-zoom_width, $this-Zoom_height, $WHITE);
Imagecopyresized ($this, True_color, $this-picture_create, 0, 0, 0, 0, $this-zoom_width, $this-zoom_ HEIGHT, $this-Picture_width, $this-picture_height);
}

# End of Zoom
Crop pictures, by coordinates or automatically
function Cut () {
$this-True_color = Imagecreatetruecolor ($this, Cut_width, $this-cut_width);
Imagecopy ($this-True_color, $this-picture_create, 0, 0, $this-cut_x, $this-cut_y, $this-Cut_ WIDTH, $this-cut_height);
}

# End of Cut
/**
* Put text or pictures on the picture
* Watermark Text
*/
function _mark_text () {
$this-True_color = Imagecreatetruecolor ($this, Picture_width, $this-picture_height);
$this WORD = mb_convert_encoding ($this, Font_word, ' utf-8 ', ' gb2312 ');
/**
* Get the range of text using TrueType fonts
*/
$TEMP = Imagettfbbox ($this-font_size, 0, $this, Font_path, $this-WORD);
$WORD _length = strlen ($this, WORD);
$WORD _width = $TEMP [2]-$TEMP [6];
$WORD _height = $TEMP [3]-$TEMP [7];
/**
* The default position of the text watermark is the lower right corner
*/
if ($this-word_x = = "") {
$this, word_x = $this, picture_width-$WORD _width;
}
if ($this-word_y = = "") {
$this, word_y = $this, picture_height-$WORD _height;
}
Imagesettile ($this, True_color, $this-picture_create);
Imagefilledrectangle ($this, true_color, 0, 0, $this, Picture_width, $this, Picture_height, img_color_tiled );
$TEXT 2 = imagecolorallocate ($this, True_color, $this, RED, $this-GREEN, $this-Blue);
Imagettftext ($this-True_color, $this-Font_size, $this-ANGLE, $this-word_x, $this-word_y, $TE XT2, $this, Font_path, $this, WORD);
}

/**
* Watermark Image
*/
function _mark_picture () {

/**
* Get information on watermark images
*/
@ $SIZE = getimagesize ($this-Force_url);
if (! $SIZE) {
Exit ($this-error[' unalviable ');
}
$FORCE _picture_width = $SIZE [0];
$FORCE _picture_height = $SIZE [1];
Create a watermark Picture
Switch ($SIZE [2]) {
Case 1:
$FORCE _picture_create = Imagecreatefromgif ($this, Force_url);
$FORCE _picture_type = "gif";
Break
Case 2:
$FORCE _picture_create = Imagecreatefromjpeg ($this, Force_url);
$FORCE _picture_type = "jpg";
Break
Case 3:
$FORCE _picture_create = Imagecreatefrompng ($this, Force_url);
$FORCE _picture_type = "png";
Break
}
/**
* Determine the size of the watermark image, and generate the size of the target image, if the watermark is larger than the picture, the image size is the size of the watermark picture. Otherwise, the resulting picture size is the original picture size.
*/
$this, new_picture = Picture_create, $this;
if ($FORCE _picture_width > $this-picture_width) {
$CREATE _width = $FORCE _picture_width-$this, force_start_x;
}else{
$CREATE _width = Picture_width, $this;
}
if ($FORCE _picture_height > $this-picture_height) {
$CREATE _height = $FORCE _picture_height-$this, force_start_y;
}else{
$CREATE _height = Picture_height, $this;
}
/**
* Create a canvas
*/
$NEW _picture_create = Imagecreatetruecolor ($CREATE _width, $CREATE _height);
$WHITE = Imagecolorallocate ($NEW _picture_create, 255, 255, 255);
/**
* Copy the background map to the canvas
*/
Imagecopy ($NEW _picture_create, $this-picture_create, 0, 0, 0, 0, $this-picture_width, $this-Picture_heig HT);

/**
* Copy the target image to the background image
*/
Imagecopy ($NEW _picture_create, $FORCE _picture_create, $this-force_x, $this-force_y, $this-Force_start_ X, $this-force_start_y, $FORCE _picture_width, $FORCE _picture_height);
$this-true_color = $NEW _picture_create;
}
# End of Mark
function Alpha_ () {
$this-True_color = Imagecreatetruecolor ($this, Picture_width, $this-picture_height);
$rgb = "#CDCDCD";
$tran _color = "#000000";
for ($j = 0; $j <= $this, picture_height-1; $j + +) {
for ($i = 0; $i <= $this-picture_width-1; $i + +)
{
$rgb = Imagecolorat ($this, Picture_create, $i, $j);
$r = ($rgb >>) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$now _color = imagecolorallocate ($this, Picture_create, $r, $g, $b);
if ($now _color = = $tran _color)
{
Continue
}
Else
{
$color = Imagecolorallocatealpha ($this-Picture_create, $r, $g, $b, $ALPHA);
Imagesetpixel ($this-picture_create, $ALPHA _x + $i, $ALPHA _y + $j, $color);
}
$this, True_color = Picture_create, $this;

}
}
}

/**
* Picture Rotation:
* Rotate along the y axis
*/
function turn_y () {
$this-True_color = Imagecreatetruecolor ($this, Picture_width, $this-picture_height);
for ($x = 0; $x < $this picture_width; $x + +)
{
Imagecopy ($this, True_color, $this, Picture_create, $this, picture_width-, $x-1, 0, $x, 0, 1, $this, Picture_height);
}
}
/**
* Rotate along the x axis
*/
function turn_x () {
$this-True_color = Imagecreatetruecolor ($this, Picture_width, $this-picture_height);
for ($y = 0; $y < $this, picture_height; $y + +) {
Imagecopy ($this, True_color, $this-picture_create, 0, $this, picture_height-$y-1, 0, $y, $this, P Icture_width, 1);
}
}

/**
* Rotation at any angle
*/
function Turn () {
$this-True_color = Imagecreatetruecolor ($this, Picture_width, $this-picture_height);
Imagecopyresized ($this, True_color, $this-picture_create, 0, 0, 0, 0, $this, Picture_width, $this, PI Cture_height, $this-Picture_width, $this-picture_height);
$WHITE = Imagecolorallocate ($this, True_color, 255, 255, 255);
$this-True_color = Imagerotate ($this, True_color, $this, Circumrotate, $WHITE);
}
/**
* Image Sharpening
*/
Function Sharp () {
$this-True_color = Imagecreatetruecolor ($this, Picture_width, $this-picture_height);
$cnt = 0;
for ($x = 0; $x < $this, picture_width; $x + +) {
for ($y = 0; $y < $this, picture_height; $y + +) {
$src _CLR1 = Imagecolorsforindex ($this, True_color, Imagecolorat ($this, Picture_create, $x-1, $y-1));
$src _CLR2 = Imagecolorsforindex ($this, True_color, Imagecolorat ($this, Picture_create, $x, $y));
$r = intval ($src _clr2["Red"] + $this, sharp * ($SRC _clr2["Red")-$src _clr1["Red"]));
$g = intval ($src _clr2["green" + $this, Sharp * ($SRC _clr2["green")-$src _clr1["green"]);
$b = intval ($src _clr2["Blue" + $this, Sharp * ($SRC _clr2["Blue")-$src _clr1["Blue"]));
$r = min (255, max ($r, 0));
$g = min (255, max ($g, 0));
$b = min (255, max ($b, 0));
if ($DST _clr = Imagecolorexact ($this, Picture_create, $r, $g, $b)) = =-1)
$DST _clr = imagecolorallocate ($this, Picture_create, $r, $g, $b);
$cnt + +;
if ($DST _clr = =-1) die ("Color allocate faile at $x, $y ($cnt).");
Imagesetpixel ($this, True_color, $x, $y, $DST _clr);
}
}
}
/**
* Anti-color image processing??
*/
function Return_color () {
/**
* Create a canvas
*/
$NEW _picture_create = imagecreate ($this, Picture_width, $this-picture_height);
$WHITE = Imagecolorallocate ($NEW _picture_create, 255, 255, 255);
/**
* Copy the background map to the canvas
*/
Imagecopy ($NEW _picture_create, $this-picture_create, 0, 0, 0, 0, $this-picture_width, $this-Picture_heig HT);
$this-true_color = $NEW _picture_create;
}

/**
* Generate the target picture and show
*/
Function Show () {
Judge Browser, if IE does not send headers
if (Isset ($_server[' http_user_agent '))
{
$ua = Strtoupper ($_server[' http_user_agent ');
if (!preg_match ('/^.*msie.*\) $/i ', $ua))
{
Header ("Content-type: $this->picture_mime");
}
}
$OUT = Picture_type, $this;
$OUT ($this, True_color);
}

/**
* Generate target picture and save
*/
function Save_picture () {
Output images to a browser or file in JPEG format
$OUT = Picture_type, $this;
if (function_exists ($OUT)) {
Judge Browser, if IE does not send headers
if (Isset ($_server[' http_user_agent '))
{
$ua = Strtoupper ($_server[' http_user_agent ');
if (!preg_match ('/^.*msie.*\) $/i ', $ua))
{
Header ("Content-type: $this->picture_mime");
}
}
if (! $this-True_color) {
Exit ($this-error[' unavilable ');
}else{
$OUT ($this, True_color, $this-Dest_url);
$OUT ($this, True_color);
}
}
}
/**
* destructor: Release picture
*/
function __destruct () {
/**
* Release Pictures
*/
Imagedestroy ($this-True_color);
Imagedestroy ($this-picture_create);
}
# End of Class
}
?>

  • Related Article

    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.