PHP rounded corner Image

Source: Internet
Author: User

I used it in my work. I wrote one myself and shared it with people who need it. The class definition is in the front, and the second line is called.

 

Advantages:

External Image not required

Supports PNG transparency

Customizable Corner radius

 

Disadvantages:

Only one transparent color can be specified.

 

 

 

 

<? Php <br/> class roundedcorner {<br/> private $ _ r; <br/> private $ _ g; <br/> private $ _ B; <br/> private $ _ image_path; <br/> private $ _ radius; </P> <p> function _ construct ($ image_path, $ radius, $ r = 255, $ G = 0, $ B = 0) {<br/> $ this-> _ image_path = $ image_path; <br/> $ this-> _ radius = $ radius; <br/> $ this-> _ r = (INT) $ R; <br/> $ this-> _ G = (INT) $ g; <br/> $ this-> _ B = (INT) $ B; <br/>}</P> <p> private function _ get_lt_rou Nder_corner () {<br/> $ radius = $ this-> _ radius; <br/> $ IMG = imagecreatetruecolor ($ radius, $ radius ); <br/> $ bgcolor = imagecolorallocate ($ IMG, $ this-> _ r, $ this-> _ g, $ this-> _ B ); <br/> $ fgcolor = imagecolorallocate ($ IMG, 0, 0); <br/> imagefill ($ IMG, 0, 0, $ bgcolor ); <br/> imagefilledarc ($ IMG, $ radius * 2, $ radius * 2,180,270, $ fgcolor, img_arc_pie); <br/> imagecolortransparent ($ IMG, $ FGC Olor); <br/> return $ IMG; <br/>}</P> <p> private function _ load_source_image () {<br/> $ ext = substr ($ this-> _ image_path, strrpos ($ this-> _ image_path ,'. '); <br/> If (empty ($ ext) {<br/> return false; <br/>}< br/> switch (strtolower ($ ext) {<br/> case '.jpg ': <br/> $ IMG = @ imagecreatefromjpeg ($ this-> _ image_path); <br/> break; <br/> case '.gif ': <br/> $ IMG = @ imagecreatefromgif ($ this-> _ image_path); <br/> break; <Br/> case '.png ': <br/> $ IMG = @ imagecreatefrompng ($ this-> _ image_path); <br/> break; <br/> default: <br/> return false; <br/>}< br/> return $ IMG; </P> <p >}</P> <p> Public Function round_it () {<br/> // load the source image <br/> $ src_image = $ this-> _ load_source_image (); <br/> if ($ src_image = false) {<br/> die ('Sorry, CAN/'t load the image'); <br/>}< br/> $ image_width = imagesx ($ src_image ); <br/> $ image_hei Ght = imagesy ($ src_image); </P> <p> // create a new image, with src_width, src_height, and fill it with transparent color <br/> $ image = imagecreatetruecolor ($ image_width, $ image_height); <br/> $ trans_color = imagecolorallocate ($ image, $ this-> _ r, $ this-> _ g, $ this-> _ B); <br/> imagefill ($ image, 0, 0, $ trans_color ); </P> <p> // then overwirte the source image to the new created image <br/> imagecopymerge ($ imag E, $ src_image, 0, 0, 0, 0, $ image_width, $ image_height, 100 ); </P> <p> // then just copy all the rounded corner images to the 4 corners <br/> $ radius = $ this-> _ radius; <br/> // lt <br/> $ lt_corner = $ this-> _ get_lt_rounder_corner (); <br/> imagecopymerge ($ image, $ lt_corner, 0, 0, 0, 0, $ radius, $ radius, 100); <br/> // LB <br/> $ lb_corner = imagerotate ($ lt_corner, 90, $ trans_color ); <br/> imagecopymerge ($ Image, $ Lb_corner, 0, $ image_height-$ radius, 0, 0, $ radius, $ radius, 100 ); <br/> // RB <br/> $ rb_corner = imagerotate ($ lt_corner, 180, $ trans_color); <br/> imagecopymerge ($ image, $ rb_corner, $ image_width-$ radius, $ image_height-$ radius, 0, 0, $ radius, $ radius, 100 ); <br/> // RT <br/> $ rt_corner = imagerotate ($ lt_corner, 270, $ trans_color); <br/> imagecopymerge ($ image, $ rt_corner, $ image_width-$ radius, 0, 0, 0, $ radius, $ radius, 100); </P> <p> // set the transparency <br/> imagecolortransparent ($ image, $ trans_color ); <br/> // display it <br/> header ('content-type: image/PNG '); <br/> imagepng ($ image ); </P> <p> imagedestroy ($ src_image); <br/> imagedestroy ($ image); <br/> imagedestroy ($ lt_corner ); <br/> imagedestroy ($ lb_corner); <br/> imagedestroy ($ rb_corner); <br/> imagedestroy ($ rt_corner ); <br/>}< br/> $ R Ounder = new roundedcorner('test.png ', 20); <br/> $ rounder-> round_it (); <br/>?>

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.