PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Sep 22, 2018 2:25 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: imageClipCorners()
PostPosted: Tue Aug 02, 2005 2:37 pm 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com
This is a function I wrote as part of PHPhoto 0.3 (coming soon!). It rounds the corners of an image. Might be cool to integrate it into a thumbnail generator or something..

Syntax: [ Download ] [ Hide ]
<?



        $i = imagecreatefromjpeg("xxx.jpg");

        $i = imageClipCorners($i);

       

        header("Content-type: image/jpeg");

        imagejpeg($i);







        //Corner rounding function.



        function imageClipCorners($i,$xrad=20,$yrad=20,$r=255,$g=255,$b=255) {



                $i_x = imagesx($i); $i_y = imagesy($i);



                $gdCornerSource = imagecreatefromstring(base64_decode(cornerpng()));

                $gdCorner_x = imagesx($gdCornerSource);

                $gdCorner_y = imagesy($gdCornerSource);



                $gdCorner1 = imagecreatefromstring(base64_decode(blankpng()));

                $gdCorner2 = imagecreatefromstring(base64_decode(blankpng()));

                $gdCorner3 = imagecreatefromstring(base64_decode(blankpng()));

                $gdCorner4 = imagecreatefromstring(base64_decode(blankpng()));



                for ($y=0;$y<imagesy($gdCornerSource);$y++) {

                        for ($x=0;$x<imagesx($gdCornerSource);$x++) {

                                $rgb = imagecolorat($gdCornerSource,$x,$y);

                                $a = ($rgb >> 24) & 0xFF;

                                $colour = imagecolorallocatealpha($gdCorner1,$r,$g,$b,$a);

                                imagesetpixel($gdCorner1,$x,$y,$colour);

                                $colour = imagecolorallocatealpha($gdCorner2,$r,$g,$b,$a);

                                imagesetpixel($gdCorner2,$gdCorner_x-$x,$y,$colour);

                                $colour = imagecolorallocatealpha($gdCorner3,$r,$g,$b,$a);

                                imagesetpixel($gdCorner3,$gdCorner_x-$x,$gdCorner_y-$y,$colour);

                                $colour = imagecolorallocatealpha($gdCorner4,$r,$g,$b,$a);

                                imagesetpixel($gdCorner4,$x,$gdCorner_y-$y,$colour);



                        }

                }



                imagecopyresampled($i,$gdCorner1,0,0,0,0,$xrad,$yrad,$gdCorner_x,$gdCorner_y);

                imagecopyresampled($i,$gdCorner2,$i_x-$xrad,0,0,0,$xrad,$yrad,$gdCorner_x,$gdCorner_y);

                imagecopyresampled($i,$gdCorner3,$i_x-$xrad,$i_y-$yrad,0,0,$xrad,$yrad,$gdCorner_x,$gdCorner_y);

                imagecopyresampled($i,$gdCorner4,0,$i_y-$yrad,0,0,$xrad,$yrad,$gdCorner_x,$gdCorner_y);



                return $i;



        }

       

        //Base64 encoded corner PNG image



        function cornerpng() {

       

                $c  = "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29m";

                $c .= "dHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAIBSURBVHjaYmRgYPjPMHDgOhTfAuJ7QPwYiJ8D8Rsg";

                $c .= "/gjE3wECiIXODnoIxEeB+AQQnwLic/////+NTwNAANHDgaCQ2AnEu4B4L9BBD0jRDBBAtHTgHSDeAMSb";

                $c .= "gI46TK4hAAFECwfeB+IVQLwK6LALlBoGEEAM0ExCDfwdiGcAsRXQYQzUwgABRC0HHgTiGGo6DIYBAohS";

                $c .= "B/4D4slArEkLx4EwQABR4kBQWsunlcNgGCCAyHXgSSAOobXjQBgggMhx4G4gdqSH40AYIIBIdeB2ILag";

                $c .= "l+NAGCCAGEgMObo6DoQBAoiBhDTnSG/HgTBAADEQmVtDBsJxIAwQQExE1DITgArXDFR7DCCACIXg5IEK";

                $c .= "ORgGCCB8IXgIiKcxDDAACCBcDvwBxLOBPrg+0A4ECCBcDlwIdNwShkEAAAKICUd7bhHDIAEAAYTNgSuA";

                $c .= "oXdssDgQIICYsDTTVzEMIgAQQOgO3ECtZjq1AEAAMaH1vjYxDDIAEEDIDtxJSe+LVgAggJAduIthEAKA";

                $c .= "AGJC6vHvHYwOBAggmAOPktrjpxcACCCYA08wDFIAEEAwB54arA4ECCAm6PDXucHqQIAAAjuQ0BDYQAKA";

                $c .= "AGKCDh4OWgAQQEzQkc1BCwACiAk67DpoAUAAMUHHhActAAggJuiA9aAFAAHEBG3FDFoAEEBM0JHRQQsA";

                $c .= "AgwAlTI79UaGSUQAAAAASUVORK5CYII=";

       

                return $c;

       

        }



        //Base64 encoded 127 alpha PNG image



        function blankpng() {



                $c  = "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29m";

                $c .= "dHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADqSURBVHjaYvz//z/DYAYAAcTEMMgBQAANegcCBNCg";

                $c .= "dyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAAN";

                $c .= "egcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQ";

                $c .= "oHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAA";

                $c .= "DXoHAgTQoHcgQAANegcCBNCgdyBAgAEAMpcDTTQWJVEAAAAASUVORK5CYII=";



                return $c;



        }



?>


Note: The forum code is borking variable names by turning them into $are.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 3:35 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
Quote:
Note: The forum code is borking variable names by turning them into $are.


Fixed ;)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 23, 2005 6:48 am 
Offline
Forum Regular

Joined: Wed Sep 28, 2005 10:08 am
Posts: 613
Great stuff, not only because it's handy, but also because it's an eye-opener as to the abilities of GD.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 26, 2005 10:14 am 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
Moved to Code Snipplets.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 2:29 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Aug 31, 2005 5:58 pm
Posts: 396
Location: Arkansas, USA
I just used this to create perfectly curved borders for a site I'm working on. Thanks, onion!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 9:54 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Aug 31, 2005 5:58 pm
Posts: 396
Location: Arkansas, USA
GD newb here.

What must change for this function to be able to work on gif images? I changed
Syntax: [ Download ] [ Hide ]
$i = imagecreatefromjpeg("xxx.jpg");

$i = imageClipCorners($i);

   

header("Content-type: image/jpeg");

imagejpeg($i);


to

Syntax: [ Download ] [ Hide ]
$i = imagecreatefromgif("xxx.gif");

$i = imageClipCorners($i);

   

header("Content-type: image/gif");

imagegif($gif);


From a 100x100 tan gif box, the script turned it into this:

Image

What else needs to happen to the script?

- Nathaniel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 4:22 am 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com
Ah.. yes.. it assumes that it's working on a true color image. The quick solution would be:

Syntax: [ Download ] [ Hide ]
$t = imagecreatefromgif("xxx.gif");

$i = imagecreatetruecolor(imagesx($t),imagesy($t));

imagecopy($i,$t,0,0,0,0,imagesx($t),imagesy($t));

$i = imageClipCorners($i);

imagetruecolortopalette($i,0,256);

header("Content-type: image/gif");

imagegif($i);

(Untested code)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 4:35 am 
Offline
Tranquility In Moderation
User avatar

Joined: Sun Feb 06, 2005 8:18 pm
Posts: 5001
Location: Indiana
genius. ive never seen anything like this done before.

_________________
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 1:21 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
Added to useful bookmarks ;0 Can see this being used in the futur


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 24, 2007 6:28 pm 
Offline
Forum Commoner

Joined: Wed Jun 07, 2006 6:26 am
Posts: 41
really, really nice. been looking for something like this for my scripts!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 24, 2007 6:34 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
Lets try to avoid reviving 2 year old threads, please.


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

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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:  
cron
Powered by phpBB® Forum Software © phpBB Group