PHP image processing class for scaling, cutting, photo frame, watermark, sharpening, rotation, flip, transparency, reversed color and other functions

Source: Internet
Author: User
Tags imagecopy

It is good to use PHP to process a piece of images. Below is a PHP image processing class, allows you to scale, cut, frame, watermark, sharpen, rotate, flip, transparency, and reversed colors of images.

<? PHP/* image processing functions: scaling, cropping, photo frames, watermarks, sharpening, rotation, turning, transparency, reversed color processing, and history record keeping: when an image is changed, a new image is automatically generated. You can add steps based on the original image to name the image. For example, add the following steps to the image name + _ Step 1: power by: antsnet. nete-mail: antsnet.net@gmail.com URL: http://www.antsnet.net */class picture {var $ picture_url; // var $ dest_url = "temp%01.jpg "; // generate the Target Image Position var $ picture_create; // var $ ture_color of the image to be created; // create a true color image var $ picture_width; // var $ picture_height of the original image width; // Original Image Height/* watermark type. The default value is watermark text */var $ Ma. Rk_type = 1; var $ word; // text var $ word_x after UTF-8; // 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; // text angle, the default value is 0 var $ font_color = "#000000"; // text color var $ font_path = "Font/simkai. TTF "; // font library, default: var $ force_url; // watermark image var $ force_x = 0; // watermark abscissa var $ force_y = 0; // watermark y coordinate var $ force_start_x = 0; // The Y coordinate var $ force_start_y = 0; // The Y coordinate var $ picture_typ E; // image type var $ picture_mime; // The output Header/* If the zoom ratio is 1, the image is scaled by the zoom height and width */var $ zoom = 1; // zoom type var $ zoom_multiple; // zoom ratio var $ zoom_width; // zoom width var $ zoom_height; // zoom height/* crop, proportional and fixed length, width */var $ cut_type = 1; // cropping type var $ cut_x = 0; // the x-axis var $ cut_y = 0 of cropping; // crop the ordinate var $ cut_width = 100; // crop the width var $ cut_height = 100; // crop the height/* sharpen */var $ sharp = "7.0 "; // sharpen Degree/* Transparency Processing */var $ alpha = '000000'; // var $ alpha_x = "90"; var $ Alph A_y = "50";/* rotate at any angle */var $ Circumrotate = "90.0"; // note, it must be a floating point/* error message */var $ error = array ('unalviable' => '. No related image is 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. Otherwise, the corresponding information is provided. */@ $ 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_width = $ size [0]; $ 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 x/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]); }}# E Nd of _ construct/* converts a hexadecimal color to a decimal (R, G, B) */function hex2dec () {preg_match_all ("/([0-f]) {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. If the height and width are not specified, scale the image by function zoom () {// zoom size if ($ this-> zoom = 0) {$ This-> zoom_width = $ this-> 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, $ this-> zoom_width, $ this-> zoom_height, $ white); imagecopyresized ($ this-> true_col Or, $ this-> picture_create, 0, 0, 0, $ this-> zoom_width, $ this-> zoom_height, $ this-> picture_width, $ this-> picture_height );} # End of zoom // crop an image by coordinate or automatic 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 watermark text on the image */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-> T Rue_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_width = $ size [0]; $ 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); $ response = "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 size of the generated image is the size of the watermark image. 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_width = $ force_picture_width-$ this-> force_start_x ;} else {$ create_width = $ this-> 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 = imagecreatetruecol Or ($ create_width, $ create_height); $ white = 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, $ force_picture_width, $ force_pi Cture_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> 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 */function turn_y () {$ this-> true_color = imagecreatetruecolor ($ th Is-> 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_creat Ate, $ 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) ;}}/ * process the image in Reversed colors ?? */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 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 the target image and display */function show () {// judge the browser, if it is IE, no header is 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 the target image and save */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, it will not send the header 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 the image */function _ destruct () {/* release image */imagedestroy ($ this-> true_color); imagedestroy ($ this-> picture_create); }# end of class}?>

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.