PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Dec 17, 2017 10:48 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Apr 18, 2008 3:04 pm 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com
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! :)

Syntax: [ Download ] [ Hide ]
<?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;
 
    }
 


Top
 Profile  
 
PostPosted: Wed Sep 17, 2008 6:47 pm 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
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.


Top
 Profile  
 
PostPosted: Mon Dec 22, 2014 12:10 am 
Offline
Forum Newbie

Joined: Sun Dec 21, 2014 11:57 pm
Posts: 1
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group