Php common image processing class, _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php image processing class ,. Php image processing class ,? Php * known issues: 1. in the image scaling function, use the imagecreatetruecolor function to create a canvas and use transparent processing algorithms. However, images in PNG format cannot be commonly used in php image processing,
 Types = $ types; $ this-> image = $ this-> imagesources ($ imageaddress); $ this-> width = $ this-> imagesizex (); $ this-> height = $ this-> imagesizey (); $ this-> value1 = $ value1; $ this-> value2 = $ value2; $ this-> endaddress = $ endaddress;} function outimage () {// different function switches ($ this-> types) {case 1 based on the input type value: $ this-> scaling (); break; case 2: $ this-> clipping (); break; case 3: $ this-> imagewater (); break; default: return false ;}} private function imagewater (){// http://www.hzhuti.com Add an image watermark // use a function to obtain the length and width of the watermark file $ imagearrs = $ this-> getimagearr ($ this-> value1 ); // call the function to calculate the position where the watermark is loaded. $ positionarr = $ this-> position ($ this-> value2, $ imagearrs [0], $ imagearrs [1]); // add the watermark imagecopy ($ this-> image, $ this-> imagesources ($ this-> value1), $ positionarr [0], $ positionarr [1], 0, 0, $ imagearrs [0], $ imagearrs [1]); // call the output method to save $ this-> output ($ this-> image);} private function clipping () {// Image cropping function // assign the passed values to the variable list ($ s) Rc_x, $ src_y) = explode (",", $ this-> value1); list ($ dst_w, $ dst_h) = explode (",", $ this-> value2); if ($ this-> width <$ src_x + $ dst_w | $ this-> height <$ src_y + $ dst_h) {// this judgment means that the canvas resource $ newimg = imagecreatetruecolor ($ dst_w, $ dst_h) cannot be captured outside the image to return false;} // create a new canvas resource $ newimg ); // crop imagecopyresampled ($ newimg, $ this-> image, 0, 0, $ src_x, $ src_y, $ dst_w, $ dst_h, $ dst_w, $ dst_h ); // call the output method to save $ this-> output ($ newimg);} priva Te function scaling () {// image scaling function // Get the width and height of proportional scaling $ this-> proimagesize (); // scale according to the parameter, and call the output function to save the processed file $ this-> output ($ this-> imagescaling ();} private function imagesources ($ imgad) {// Obtain the image type and open the image resource $ imagearray = $ this-> getimagearr ($ imgad); switch ($ imagearray [2]) {case 1: // gif $ this-> imgtype = 1; $ img = imagecreatefromgif ($ imgad); break; case 2: // jpeg $ this-> imgtype = 2; $ img = imagecreatefromjpeg ($ imgad); break; c Ase 3: // png $ this-> imgtype = 3; $ img = imagecreatefrompng ($ imgad); break; default: return false;} return $ img ;} private function imagesizex () {// Get image width return imagesx ($ this-> image);} private function imagesizey () {// Retrieve image height return imagesy ($ this-> image);} private function proimagesize () {// calculate the width and height of the scaled image. if ($ this-> value1 & ($ this-> width <$ this-> height )) {// proportional scaling algorithm $ this-> value1 = round ($ this-> value2 /$ This-> height) * $ this-> width);} else {$ this-> value2 = round ($ this-> value1/$ this-> width) * $ this-> height) ;}} private function imagescaling () {// image scaling function, returns the processed image resource $ newimg = imagecreatetruecolor ($ this-> value1, $ this-> value2); $ tran = imagecolortransparent ($ this-> image ); // process the transparent algorithm if ($ tran >=0 & $ tran <imagecolorstotal ($ this-> image) {$ tranarr = imagecolorsforindex ($ this-> image, $ tran); $ newcolor = imageco Lorallocate ($ newimg, $ tranarr ['red'], $ tranarr ['green'], $ tranarr ['blue']); imagefill ($ newimg, 0, 0, $ newcolor); imagecolortransparent ($ newimg, $ newcolor);} imagecopyresampled ($ newimg, $ this-> image, 0, 0, 0, 0, 0, $ this-> value1, $ this-> value2, $ this-> width, $ this-> height); return $ newimg;} private function output ($ image) {// switch ($ this-> imgtype) {case 1: imagegif ($ image, $ this-> endaddress); br Eak; case 2: imagejpeg ($ image, $ this-> endaddress); break; case 3: imagepng ($ image, $ this-> endaddress); break; default: return false ;}} private function getimagearr ($ imagesou) {// return Image attribute array method return getimagesize ($ imagesou);} private function position ($ num, $ width, $ height) {// returns the coordinates of a position based on the input number. $ width and $ height indicate the width and height of the inserted Image respectively. switch ($ num) {case 1: $ positionarr [0] = 0; $ positionarr [1] = 0; break; case 2: $ posit Ionarr [0] = ($ this-> width-$ width)/2; $ positionarr [1] = 0; break; case 3: $ positionarr [0] = $ this-> width-$ width; $ positionarr [1] = 0; break; case 4: $ positionarr [0] = 0; $ positionarr [1] = ($ this-> height-$ height)/2; break; case 5: $ positionarr [0] = ($ this-> width-$ width) /2; $ positionarr [1] = ($ this-> height-$ height)/2; break; case 6: $ positionarr [0] = $ this-> width-$ width; $ positionarr [1] = ($ this-> height-$ height)/2; brea K; case 7: $ positionarr [0] = 0; $ positionarr [1] = $ this-> height-$ height; break; case 8: $ positionarr [0] = ($ this-> width-$ width)/2; $ positionarr [1] = $ this-> height-$ height; break; case 9: $ positionarr [0] = $ this-> width-$ width; $ positionarr [1] = $ this-> height-$ height; break; case 0: $ positionarr [0] = rand (0, $ this-> width-$ width); $ positionarr [1] = rand (0, $ this-> height-$ height ); break;} return $ positionarr;} func Tion _ destruct () {imagedestroy ($ this-> image) ;}}?>

  

Success ,? Php/* known issues: 1. in the image scaling function, use the imagecreatetruecolor function to create a canvas and use transparent processing algorithms. However, images in PNG format cannot be used...

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.