(Advanced) common php image processing

Source: Internet
Author: User
* Known problems: 1. in the image scaling function, use the imagecreatetruecolor function to create a canvas and use a transparent processing algorithm. However, images in PNG format cannot be transparent. Using the imagecreate function to create a canvas can solve this problem, but the scaled image color is too small. (Advanced) common 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 () {// add an image watermark function // 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 cli Pping () {// Image cropping function // assign the passed values to the variable list ($ src_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);} private function scaling () {// image scaling function // Get the width and height of proportional scaling $ this-> proimagesize (); // scale according to parameters, 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; $ I Mg = imagecreatefromjpeg ($ imgad); break; case 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) {// and so on Scaling Algorithm $ this-> value1 = round ($ this-> value2/$ this-> height) * $ this-> width );} else {$ this-> value2 = round ($ this-> value1/$ this-> width) * $ this-> height) ;}} private function imagescaling () {// The 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 = imagecolorallocate ($ newimg, $ tranarr ['red'], $ tranarr ['green'], $ tranarr ['blue']); imagefill ($ newimg, 0, 0, $ newcolor); imagecolortransparent ($ newimg, $ newcolor);} imagecopyresampled ($ newimg, $ this-> image, 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); break; case 2: imagejpeg ($ image, $ this-> endaddress); break; case 3: imagepng ($ image, $ this-> endaddress); break; default: return false ;}} private function getimagearr ($ imagesou) {// return the 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. switch ($ num) {case 1: $ positionarr [0] = 0; $ Positionarr [1] = 0; break; case 2: $ positionarr [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; $ positi Onarr [1] = ($ this-> height-$ height)/2; break; 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-$ heigh T); break;} return $ positionarr;} function _ destruct () {imagedestroy ($ this-> image) ;}}?>

The above is the content of common php image processing classes. For more information, see The PHP Chinese website (www.php1.cn )!

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.