PHP adds rounded corners and remains transparent to the picture, making it a round head

Source: Internet
Author: User
Tags transparent color

Original link:Https://www.zhaokeli.com/article/8031.html

To add rounded corners to a picture,

The main (judging whether a point is within a circle) formula is used to generate a circular image in the above mentioned article.

Then scan the original image to draw a transparent picture of each of the appropriate pixels.

Generate a certain fillet based on the size of the fillet you want to add

First, according to the rounded corners to determine the square shape of the four corners of the picture, as long as the scan is not within these ranges of pixels to draw up

<? PHP if (($x$radius$x <= ($w$radius)) | | ($y$radius$y <= ($h$radius)) {    // not within the range of corners, draw Imagesetpixel directly    ($img$x$y $rgbColor );}? >

After filtering, there are only four corners left in the position without pixels.

Then the center radius of the Four Corners is known to determine whether the current pixel is within the circle if the pixel is drawn within the circle.

<?PHP/** blog:http://www.zhaokeli.com * processing fillet picture * @param string $imgpath source picture path * @param integer $radius fillet radius length defaults to 15, processed into a circle Type * @return [type] [description]*/functionRadius_img ($imgpath= './t.png ',$radius= 15) {    $ext=PathInfo($imgpath); $src _img=NULL; Switch($ext[' extension ']) {     Case' jpg ':$src _img= Imagecreatefromjpeg ($imgpath);  Break;  Case' PNG ':$src _img= Imagecreatefrompng ($imgpath);  Break; }    $WH=getimagesize($imgpath); $w=$WH[0]; $h=$WH[1]; //$radius = $radius = = 0? (Min ($w, $h)/2): $radius;    $img= Imagecreatetruecolor ($w,$h); //This sentence must haveImagesavealpha ($img,true); //pick a completely transparent color and the last parameter 127 is fully transparent    $BG= Imagecolorallocatealpha ($img, 255, 255, 255, 127); Imagefill ($img, 0, 0,$BG); $r=$radius;//Fillet radius     for($x= 0;$x<$w;$x++) {         for($y= 0;$y<$h;$y++) {            $rgbColor= Imagecolorat ($src _img,$x,$y); if(($x>=$radius&&$x<= ($w-$radius)) || ($y>=$radius&&$y<= ($h-$radius))) {                //not within the range of the corners, direct paintingImagesetpixel ($img,$x,$y,$rgbColor); } Else {                //Select Draw//top left in the range of Four Corners                $y _x=$r;//Center X coordinate                $y _y=$r;//Center Y coordinate                if(((($x-$y _x) * ($x-$y _x) + ($y-$y _y) * ($y-$y _y)) <= ($r*$r)) {Imagesetpixel ($img,$x,$y,$rgbColor); }                //Up Right                $y _x=$w-$r;//Center X coordinate                $y _y=$r;//Center Y coordinate                if(((($x-$y _x) * ($x-$y _x) + ($y-$y _y) * ($y-$y _y)) <= ($r*$r)) {Imagesetpixel ($img,$x,$y,$rgbColor); }                //Lower left                $y _x=$r;//Center X coordinate                $y _y=$h-$r;//Center Y coordinate                if(((($x-$y _x) * ($x-$y _x) + ($y-$y _y) * ($y-$y _y)) <= ($r*$r)) {Imagesetpixel ($img,$x,$y,$rgbColor); }                //Down Right                $y _x=$w-$r;//Center X coordinate                $y _y=$h-$r;//Center Y coordinate                if(((($x-$y _x) * ($x-$y _x) + ($y-$y _y) * ($y-$y _y)) <= ($r*$r)) {Imagesetpixel ($img,$x,$y,$rgbColor); }            }        }    }    return $img;}Header("Content-type:image/png");$IMGG= Radius_img ('./tt.png ', 20); Imagepng ($IMGG); Imagedestroy ($IMGG);?>

Original:

After processing

If the picture is a square, the fillet radius is directly half the width of the graph and can be processed into a circle.

Such as:

<? PHP Header ("Content-type:image/png"); $imgg = radius_img ('./tx.png ', 147); Imagepng ($imgg); Imagedestroy ($IMGG );? >

After processing:

PHP adds rounded corners and remains transparent to the picture, making it a round head

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.