imageClipCornersTransparent()

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

imageClipCornersTransparent()

Post by onion2k »

I had reason to revisit my imageClipCorners() code this evening to make a version that can do transparent corners ... It'll only work for 24bit or 32bit images (jpeg and RGBA png basically), and it'll destroy any existing alpha channel in a png if you use that, and it'll only output a 32bit PNG. It's a bit of a work in progress... but don't hold your breathe for any updates because this one was over 2 years coming! :)

Code: Select all

<?php
 
    //Image Clip Corners Transparent
 
    $file = "image.jpg";
 
    $image = imagecreatefromjpeg($file);
 
    imageClipCornersTransparent(&$image);
 
    header("Content-type: image/png");
    imagepng($image);
 
 
    function imageClipCornersTransparent($image, $radius=20) {
 
        imagesavealpha($image, true);
        imagealphablending($image,false);
        $image_x = imagesx($image);
        $image_y = imagesy($image);
 
        $gdCorner = imagecreatefromstring(base64_decode(cornerpng()));
 
        $corner = imageCreateTrueColorTransparent($radius, $radius);
        imagecopyresampled($corner, $gdCorner, 0,0, 0,0, $radius,$radius, imagesx($gdCorner),imagesy($gdCorner));
 
        $corner_x = $radius;
        $corner_y = $radius;
 
        for ($y=0;$y<$corner_y;$y++) {
 
            for ($x=0;$x<$corner_x;$x++) {
 
                for ($c=0; $c<4; $c++) {
 
                    switch ($c) {
                        case 0:
                            $xo = 0; $yo = 0;
                            $cxo = $x; $cyo = $y;
                            break;
                        case 1:
                            $xo = ($image_x-$corner_x); $yo = 0;
                            $cxo = $corner_x-$x; $cyo = $y;
                            break;
                        case 2:
                            $xo = ($image_x-$corner_x); $yo = ($image_y-$corner_y);
                            $cxo = $corner_x-$x; $cyo = $corner_y-$y;
                            break;
                        case 3:
                            $xo = 0; $yo = ($image_y-$corner_y);
                            $cxo = $x; $cyo = $corner_y-$y;
                            break;
                    }
 
                    $irgb = imagecolorat($image, $xo+$x, $yo+$y);
                    $r   = ($irgb >> 16) & 0xFF;
                    $g   = ($irgb >> 8) & 0xFF;
                    $b   =  $irgb & 0xFF;
 
                    $crgb = imagecolorat($corner,$cxo,$cyo);
                    $a = ($crgb >> 24) & 0xFF;
 
                    $colour = imagecolorallocatealpha($image, $r, $g, $b, $a);
 
                    switch ($c) {
 
                        case 0: imagesetpixel($image,$x,$y,$colour);         break;
                        case 1: imagesetpixel($image,$xo+$x,$y,$colour);     break;
                        case 2: imagesetpixel($image,$xo+$x,$yo+$y,$colour); break;
                        case 3: imagesetpixel($image,$x,$yo+$y,$colour);     break;
 
                    }
 
                }
 
            }
 
        }
 
    }
 
    function imageCreateTrueColorTransparent($x,$y) {
        $i = imagecreatetruecolor($x,$y);
        $b = imagecreatefromstring(base64_decode(blankpng()));
        imagealphablending($i,false);
        imagesavealpha($i,true);
        imagecopyresized($i,$b,0,0,0,0,$x,$y,imagesx($b),imagesy($b));
        return $i;
    }
 
    function cornerpng() {
 
        $c  = "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29m";
        $c .= "dHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAH2SURBVHjaYvz//z/DQAFGRkZNIAXCakCsBMSyQCwJ";
        $c .= "xCJAzA/EnAABxEJnB8kDKWsgtgBiMyA2AmJWfHoAAoiFDo4ChYQ7ELsBsTMQK5CiHyCAWGjoMBUgFQDE";
        $c .= "fkBsS645AAHEQgOHKQKpCCAOA2IDSs0DCCAGUCahBgYCDiBOB+KjQPyfWhgggKjlODsgXkxVh0ExQABR";
        $c .= "6jBGIM4B4ms0cRwQAwQQJY4D5cYJNHMYFAMEELmOA5Vhq2nuOCAGCCByHOcCxPvo4jggBgggUh3nAcTH";
        $c .= "6eY4IAYIIFJDjr6OA2KAACIlze2ju+OAGCCAiM2tqwfEcUAMEEDElHMTBsxxQAwQQIQcmDOgjgNigAAi";
        $c .= "VH1dG2gHAgQQvop/8YA7DogBAgiXA9MHheOAGCCAmHC05+IYBgkACCAmLGKgxqbVYHEgQAAxYWmmhzEM";
        $c .= "IgAQQOghGECVZjoVAUAAMaH1vvwYBhkACCDknBs2WHIuMgYIIOQodmMYhAAggJiQevzOg9GBAAEEC0Fr";
        $c .= "Unv89AIAAQRzoAXDIAUAAQRzoNlgdSBAADFAh79+DcYcDMIAAcQEdSDrYA09gABigg4eDloAEEBM0JHN";
        $c .= "QQsAAogJOuw6aAFAADFBx4QHLQAIICbogPWgBQABxAQdTR+0ACCAQP3eP0DMPFgdCBBgAJ273bQUqcwV";
        $c .= "AAAAAElFTkSuQmCC";
 
        return $c;
 
    }
 
 
    function blankpng() {
 
        $c  = "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29m";
        $c .= "dHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADqSURBVHjaYvz//z/DYAYAAcTEMMgBQAANegcCBNCg";
        $c .= "dyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAAN";
        $c .= "egcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQ";
        $c .= "oHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAA";
        $c .= "DXoHAgTQoHcgQAANegcCBNCgdyBAgAEAMpcDTTQWJVEAAAAASUVORK5CYII=";
 
        return $c;
 
    }
 
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: imageClipCornersTransparent()

Post by josh »

How about an example? 8)

What about rounded corners, or arbitrary user defined "cutout masks" like zig zags :-D

If you wrote me a railroad drawing function you can do it!!!! hah, cool snippet though

Edit: ooh it does take a transparent png as the corner input, nice.
aleeza
Forum Newbie
Posts: 1
Joined: Sun Dec 21, 2014 10:57 pm

Re: imageClipCornersTransparent()

Post by aleeza »

Ofcourse you can add this and other things that you have written to mimic PHP 5 behaviour. But this is again an option. I have found the for loop quite interesting in my programming career ;)

Hope this is helpful.

Thanks

Jyot Vakharia
Are you interested in JN0-643 brain dumps itil? Get our self paced testking.us - 1Y0-A20 brain dumps ccna and passguide itil study packages to pass your rasmussen without any difficulty in www.usuhs.mil lsat.
Post Reply