Common php image processing class,

Source: Internet
Author: User
Tags image flip imagecopy learn php programming

Common php image processing class,

The common php image processing classes shared in this article are for your reference. The specific content is as follows:

<? 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 transparent. Using the imagecreate function to create a canvas can solve this problem, but the scaled image color is too small. *** type value: * (1): indicates that the image scaling function is used, $ value1 represents the width of the scaled image, and $ value2 represents the height of the scaled image * (2): represents the image cropping function. $ value1 represents the coordinates of the starting point of the cropping, for example, starting from the origin, that is, "0, 0", followed by the X axis and Y axis, separated by commas (,). $ value2 indicates the width and height of the crop, * (3) is also used in the form of "20, 20": indicates that the image watermark function is used. $ value1 indicates the watermark image file name, and $ value2 indicates the watermark position in the image, you can select 10 values. 1 indicates top left, 2 indicates middle left, 3 indicates left, 4 indicates middle left, 5 indicates middle, 6 indicates middle right, and 7 indicates lower, 8 indicates the Lower center, 9 indicates the lower right, and 0 indicates the random position **/class image {private $ types; // function ID used, 1 For Image Scaling 2 for image cropping 3 for images The image watermark function is private $ imgtype; // The image format is private $ image; // The image resource is private $ width; // The image width is private $ height; // The image height is private $ value1; // $ value1 indicates the private $ value2 value based on the type value. // The value varies depending on the type value, $ value2 represents different values, private $ endaddress; // output address + file name function _ construct ($ imageaddress, $ types, $ value1 = "", $ value2 = "", $ endaddress) {$ this-> 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 image watermark function // use a function to get 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 ($ src_x, $ src_y) = explode (",", $ this-> value1 ); list ($ dst_w, $ dst_h) = e Xplode (",", $ 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; $ img = imagecreatefromjpeg ($ imgad); break; case 3: // png $ this-> imgtype = 3; $ img = imagecreatefrompng ($ imgad); break; default: return false;} retur N $ 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) ;}} priv Ate 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 = imagecolorallocate ($ newimg, $ tranarr ['red'], $ tranarr ['green'], $ tranarr ['blue']); imagefill ($ newimg, 0, 0, $ newcolor); imagecolo Rtransparent ($ newimg, $ newcolor);} imagecopyresampled ($ newimg, $ this-> image, 0, 0, 0, 0, $ this-> value1, $ this-> value2, $ this-> width, $ this-> height); return $ newimg;} private function output ($ image) {// 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 ;}} priva Te function getimagearr ($ imagesou) {// return the image attribute array method return getimagesize ($ imagesou);} private function position ($ num, $ width, $ height) {// return 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; $ p Ositionarr [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; 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-$ height ); break;} return $ positionarr;} function _ destruct () {imagedestroy ($ this-> image) ;}}?>

The above is all the content of this article. I hope it will help you learn php programming.

Articles you may be interested in:
  • 5 Image Processing for learning and practicing Ajax + PHP
  • Php Image Processing: watermarks and thumbnails (custom functions: watermark and thumbnail)
  • PHP image processing phpThumb parameter usage
  • Php multi-function image processing class sharing (php image scaling class)
  • PHPThumb image processing instance
  • PHP Image Processing-image rotation and image flip instances
  • Use the imagecopy function to add an image watermark instance for PHP Image Processing
  • PHP Image Processing-image cropping using imagecopyresampled function example
  • Example of using imagecopyresampled function for image scaling in PHP
  • PHP Image Processing: Image background and canvas operations

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.