Image processing function functions: scaling, cropping, photo frames, watermarks, sharpening, rotation, turning, transparency, reversed colors, and ideas for processing and saving historical records: when an image is changed, a new image is automatically generated. you can add steps to the original image based on the name method, for example, the first step of the image name +. You can refer to this class in some Web applications that require advanced image processing.
- /*
- Image processing functions: scaling, cropping, photo frames, watermarks, sharpening, rotation, turning, transparency, and reversed colors
- The idea of processing and saving historical records: when an image is changed, a new image is automatically generated. the naming method can be based on the original image. for example: picture name + _ step 1
- */
- Class picture
- {
- Var $ PICTURE_URL; // the image to be processed
- Var $ DEST_URL = "temp000001.jpg"; // Generate the target image location
- Var $ PICTURE_CREATE; // the image to be created
- Var $ TURE_COLOR; // create a true color image
-
- Var $ PICTURE_WIDTH; // original image width
- Var $ PICTURE_HEIGHT; // original image height
-
- /*
- Watermark type. the default value is watermark text.
- */
- Var $ MARK_TYPE = 1;
- Var $ WORD; // text after UTF-8
- Var $ WORD_X; // text abscissa
- Var $ WORD_Y; // text ordinate
- Var $ FONT_TYPE; // font type
- Var $ FONT_SIZE = "12"; // font size
- Var $ FONT_WORD; // text
- Var $ ANGLE = 0; // The text ANGLE. the default value is 0.
- Var $ FONT_COLOR = "#000000"; // text color
- Var $ FONT_PATH = "font/simkai. ttf"; // The font Library. the default value is .
- Var $ FORCE_URL; // watermark image
- Var $ FORCE_X = 0; // horizontal coordinate of the watermark
- Var $ FORCE_Y = 0; // watermark ordinate
- Var $ FORCE_START_X = 0; // the horizontal coordinate of the watermark image.
- Var $ FORCE_START_Y = 0; // cut the vertical coordinate of the watermark image.
-
- Var $ PICTURE_TYPE; // image type
- Var $ PICTURE_MIME; // output header
-
- /*
- If the scaling ratio is 1, scale by the zoom height and width.
- */
- Var $ ZOOM = 1; // scaling type
- Var $ ZOOM_MULTIPLE; // scaling ratio
- Var $ ZOOM_WIDTH; // zoom width
- Var $ ZOOM_HEIGHT; // zoom height
-
- /*
- Crop, according to ratio and fixed length, width
- */
- Var $ CUT_TYPE = 1; // cropping type
- Var $ CUT_X = 0; // the abscissa of the cropping.
- Var $ CUT_Y = 0; // y coordinate of the cropping
- Var $ CUT _; // width of the cropping
- Var $ CUT_HEIGHT = 100; // crop height
-
- /*
- Sharpen
- */
- Var $ SHARP = "7.0"; // sharpen degree
-
- /*
- Transparency processing
- */
- Var $ ALPHA = '000000'; // The transparency is between 0 and 100.
- Var $ ALPHA_X = "90 ";
- Var $ ALPHA_Y = "50 ";
-
- /*
- Rotate at any angle
- */
- Var $ CIRCUMROTATE = "90.0"; // note that the value must be a floating point.
-
- /*
- Error message
- */
- Var $ ERROR = array (
- 'Unalviable' => 'no related image found! '
- );
-
- /*
- Constructor: function initialization
- */
- Function _ construct ($ PICTURE_URL)
- {
- $ This-> get_info ($ PICTURE_URL );
- }
-
- Function get_info ($ PICTURE_URL)
- {
- /*
- Process the information of the original image. First, check whether the image exists. If the image does not exist, the corresponding information is displayed.
- */
- @ $ SIZE = getimagesize ($ PICTURE_URL );
- If (! $ SIZE)
- {
- Exit ($ this-> ERROR ['unalviable']);
- }
-
- // Obtain the information type, width, and height of the original image.
- $ This-> PICTURE_MIME = $ SIZE ['Mime '];
- $ This-> PICTURE _;
- $ This-> PICTURE_HEIGHT = $ SIZE [1];
-
- // Create an image
- 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;
- }
-
- /*
- Convert text color to hexadecimal
- */
- 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 the hexadecimal color to the hexadecimal (R, G, B) color)
- */
- 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]);
- }
- }
-
- // Scaling type
- Function zoom_type ($ ZOOM_TYPE)
- {
- $ This-> ZOOM = $ ZOOM_TYPE;
- }
-
- // Scale the image without specifying the height or width
- Function zoom ()
- {
- // Resize
- If ($ this-> ZOOM = 0)
- {
- $ This-> ZOOM _; gt; PICTURE_WIDTH * $ this-> ZOOM_MULTIPLE;
- $ This-> ZOOM_HEIGHT = $ this-> PICTURE_HEIGHT * $ this-> ZOOM_MULTIPLE;
- }
- // Create a 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, $ this-> ZOOM_WIDTH, $ this-> ZOOM_HEIGHT, $ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT );
- }
-
- # End of zoom
- // Crop the image 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 image on the image
- 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 ');
- /*
- Obtain the text range using the TrueType font
- */
- $ 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 bottom 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 );
- $ TEXT2 = 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, $ TEXT2, $ this-> FONT_PATH, $ this-> WORD );
- }
-
- /*
- Watermark Image
- */
- Function _ mark_picture ()
- {
- /*
- Obtain watermark image information
- */
- @ $ SIZE = getimagesize ($ this-> FORCE_URL );
- If (! $ SIZE)
- {
- Exit ($ this-> ERROR ['unalviable']);
- }
- $ FORCE_PICTURE _;
- $ FORCE_PICTURE_HEIGHT = $ SIZE [1];
- // Create a watermark image
- 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 watermark image size and generate the target image size. if the watermark is larger than the image size, the generated image size is the watermark image size. Otherwise, the size of the generated image is the size of the original image.
- */
- $ 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 );
- $ Whitelist = imagecolorallocate ($ NEW_PICTURE_CREATE, 255,255,255 );
- /*
- Copy the background image to the canvas.
- */
- Imagecopy ($ NEW_PICTURE_CREATE, $ this-> PICTURE_CREATE, 0, 0, 0, 0, $ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT );
-
- /*
- 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, $ region, $ region );
- $ 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> 16) & 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;
- }
- }
- }
-
- /*
- Image rotation:
- Rotate along 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-> 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, $ 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 );
- }
- }
- }
-
- /*
- Will the image be reversed ??
- */
- Function return_color ()
- {
- /*
- Create a canvas
- */
- $ NEW_PICTURE_CREATE = imagecreate ($ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT );
- $ Whitelist = imagecolorallocate ($ NEW_PICTURE_CREATE, 255,255,255 );
- /*
- Copy the background image 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 and display the target image
- */
- Function show ()
- {
- // Judge the browser. if IE is used, no headers are sent.
- 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 );
- }
-
- /*
- Generate and save the target image
- */
- Function save_picture ()
- {
- // Output the image to a browser or file in JPEG format
- $ OUT = $ this-> PICTURE_TYPE;
- If (function_exists ($ OUT ))
- {
- // Judge the browser. if IE is used, no headers are sent.
- 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 Images
- */
- Function _ destruct ()
- {
- /* Release the image */
- Imagedestroy ($ this-> TRUE_COLOR );
- Imagedestroy ($ this-> PICTURE_CREATE );
- }
- # End of class
- }
- ?>
|