Php image processing

Source: Internet
Author: User
Tags learn php programming
This article mainly introduces common php image processing classes for you to study. interested parties can refer to the common php image processing classes shared in this article for your reference, the details are 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. resize the image. 2. crop the image. 3. add an image Watermark. private $ imgtype. // The format of the image 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; // according to different type values, $ 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 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 ($ 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; $ img = imagecreatefromjpeg ($ imgad); break; case 3: // png $ this-> imgtype = 3; $ img = imagere Atefrompng ($ imgad); break; default: return false;} return $ img;} private function imagesizex () {// Retrieve 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 = imagecolorallocate ($ newimg, $ tranarr ['red'], $ tranarr ['green'], $ tranarr ['B Lue ']); 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) of the output image {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; $ 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.

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.