imageClipCorners()

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

imageClipCorners()

Post by onion2k »

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..

Code: Select all

<?

	$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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Note: The forum code is borking variable names by turning them into $are.
Fixed ;)
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Great stuff, not only because it's handy, but also because it's an eye-opener as to the abilities of GD.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to Code Snipplets.
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

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

Post by Nathaniel »

GD newb here.

What must change for this function to be able to work on gif images? I changed

Code: Select all

$i = imagecreatefromjpeg("xxx.jpg");
$i = imageClipCorners($i);
    
header("Content-type: image/jpeg");
imagejpeg($i);
to

Code: Select all

$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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Ah.. yes.. it assumes that it's working on a true color image. The quick solution would be:

Code: Select all

$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)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Added to useful bookmarks ;0 Can see this being used in the futur
Li0rE
Forum Commoner
Posts: 41
Joined: Wed Jun 07, 2006 6:26 am

Post by Li0rE »

really, really nice. been looking for something like this for my scripts!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Lets try to avoid reviving 2 year old threads, please.
Post Reply