Image processing functions: Zoom, cut, frame, watermark, sharpen, rotate, flip, transparency, invert color, process and save the history of the idea: when there are changes in the picture to automatically generate a new picture, naming method can be considered in the original image based on the addition of steps, such as: Picture name +__ step. You can refer to this class in some Web applications that require image advanced processing capabilities.
- /*
- Image processing functions: Zoom, cut, frame, watermark, sharpen, rotate, flip, transparency, invert color
- Process and save the history of the idea: 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
- */
- Class picture
- {
- var $PICTURE _url;//Pictures to process
- var $DEST _url= "temp__01.jpg";//Generate target picture position
- var $PICTURE _create;//The picture you want to create
- var $TURE _color;//A new true color image
- var $PICTURE _width;//Original image width
- var $PICTURE _height;//Original Image Height
- /*
- The type of watermark, which is the default watermark text
- */
- var $MARK _type=1;
- var $WORD;//The text after UTF-8
- 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 the angle of =0;//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 Image
- var $FORCE _x=0;//Watermark Horizontal
- var $FORCE _y=0;//Watermark Ordinate
- var $FORCE _start_x=0;//-Cut watermark image
- var $FORCE _start_y=0;//-Cut watermark image ordinate
- var $PICTURE _type;//Picture Type
- var $PICTURE the head of the _mime;//output
- /*
- Scale to zoom height and width by 1
- */
- var $ZOOM =1;//Scaling Type
- var $ZOOM _multiple;//Zoom ratio
- var $ZOOM _width;//Scale Relaxation
- var $ZOOM _height;//Zoom Height
- /*
- Cut, proportional and fixed length, width
- */
- var $CUT _type=1;//Cut Type
- var $CUT the horizontal axis of _x=0;//cutting
- var $CUT the ordinate of _y=0;//trimming
- var $CUT _;//width of the crop
- var $CUT _height=100;//Cutting Height
- /*
- Sharpening
- */
- var $SHARP = "7.0";//Sharpening degree
- /*
- Transparency processing
- */
- var $ALPHA = ' 100 ';//transparency between 0-127
- var $ALPHA _x= "90";
- var $ALPHA _y= "50";
- /*
- Rotate 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 information of the original picture, first detect whether the picture 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_;
- $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
- /*
- Converts 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_;gt; 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 a 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 that uses 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, $TEXT 2 , $this->font_path, $this->word);
- }
- /*
- Watermark Picture
- */
- function _mark_picture ()
- {
- /*
- Get information about a watermark picture
- */
- @ $SIZE =getimagesize ($this->force_url);
- if (! $SIZE)
- {
- Exit ($this->error[' unalviable ');
- }
- $FORCE _picture_;
- $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= $this->picture_create;
- if ($FORCE _picture_width> $this->picture_width)
- {
- $CREATE _;gt; force_start_x;
- }
- Else
- {
- $CREATE _;gt; Picture_width;
- }
- if ($FORCE _picture_height> $this->picture_height)
- {
- $CREATE _height= $FORCE _picture_height-$this->force_start_y;
- }
- Else
- {
- $CREATE _height= $this->picture_height;
- }
- /*
- 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_ HEIGHT);
- /*
- Copy the target picture 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= $this->picture_create;
- }
- }
- }
- /*
- 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->pictur E_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->picture _width, 1);
- }
- }
- /*
- Rotate 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 Picture_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);
- }
- }
- }
- /*
- Reverse color processing of pictures??
- */
- 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_ HEIGHT);
- $this->true_color= $NEW _picture_create;
- }
- /*
- Generate a target picture and display
- */
- 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 = $this->picture_type;
- $OUT ($this->true_color);
- }
- /*
- Create a target picture and save it
- */
- function Save_picture ()
- {
- Output images to a browser or file in JPEG format
- $OUT = $this->picture_type;
- 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);
- }
- }
- }
- /*
- Destructors: Releasing pictures
- */
- function __destruct ()
- {
- /* Release Picture */
- Imagedestroy ($this->true_color);
- Imagedestroy ($this->picture_create);
- }
- #end of Class
- }
- ?>
Copy Code
|