imagethumbnail and imagethumbnail_corners

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

imagethumbnail and imagethumbnail_corners

Post by onion2k »

I was a little bored this evening so I wrote a class to generate thumbnails.

Usage:

Code: Select all

$i = new imagethumbnail();

	$i->open("xxx.jpg");
	$i->setX(100);
	
	header("Content-type: image/jpeg;");
	$i->imagejpeg();
To set the dimensions of the thumbnail you should set either x, or y, or both. If x is set then the image will have that value as it's width. Similarly, if y is set then the image will be scaled to have that height. If both x and y are set then the image will have those exact dimensions. Imagegif, imagejpeg, and imagepng work in a similar way to their PHP counterparts, expect theres no need to pass in an image handle.

Code: Select all

class imagethumbnail {
	
		var $filename;
		var $x;
		var $y;
		var $image;
		var $thumbnail;

		function imagethumbnail() {

		}
		
		function open($filename) {

			$this->filename = $filename;
			$imageinfo = array();
			$imageinfo = getimagesize($this->filename,$imageinfo);
			
			$this->old_x = $imageinfo[0];
			$this->old_y = $imageinfo[1];
						
			switch ($imageinfo[2]) {
				case "1": $this->image = imagecreatefromgif($this->filename); break;
				case "2": $this->image = imagecreatefromjpeg($this->filename); break;
				case "3": $this->image = imagecreatefrompng($this->filename); break;
			}
			
		}

		function setX($x="") {
			if (isset($x)) { $this->x = $x; }
			return $this->x;
		}

		function setY($y="") {
			if (isset($y)) { $this->y = $y; }
			return $this->y;
		}

		function generate() {

			if ($this->x > 0 and $this->y > 0) {
				$new_x = $this->x;
				$new_y = $this->y;
			} elseif ($this->x > 0 and $this->x != "") {
				$new_x = $this->x;
				$new_y = ($this->x/$this->old_x)*$this->old_y;
			} else {
				$new_x = ($this->y/$this->old_y)*$this->old_x;
				$new_y = $this->y;
			}

			$this->thumbnail = imagecreatetruecolor($new_x,$new_y);
			$white = imagecolorallocate($this->thumbnail,255,255,255);
			imagefill($this->thumbnail,0,0,$white);

			imagecopyresampled ( $this->thumbnail, $this->image, 0, 0, 0, 0, $new_x, $new_y, $this->old_x, $this->old_y);

		}

		function imagegif($filename="") {
			if (!isset($this->thumbnail)) { $this->generate(); }
			imagetruecolortopalette($this->thumbnail,0,256);
			if ($filename=="") {
				imagegif($this->thumbnail);
			} else {
				imagegif($this->thumbnail,$filename);
			}
		}

		function imagejpeg($filename="",$quality=80) {
			if (!isset($this->thumbnail)) { $this->generate(); }
			imagejpeg($this->thumbnail,$filename,$quality);
		}

		function imagepng($filename="") {
			if (!isset($this->thumbnail)) { $this->generate(); }
			if ($filename=="") {
				imagepng($this->thumbnail);
			} else {
				imagepng($this->thumbnail,$filename);
			}
		}

	}
As a little bonus (I'm so kind..) I've written an extension to the class that will clip the corners off your thumbnail.

Usage:

Code: Select all

$i = new imagethumbnail_corners();

	$i->open("xxx.jpg");
	$i->setX(100);
	$i->clipcorners();

	header("Content-type: image/jpeg;");
	$i->imagejpeg();
It's exactly the same as imagethumbnail exept you need to call clipcorners() to clip the corners. You can also set a radius value to make the clipping bigger, and a color value to change the background color of the thumbnail (color should be a 6 digit hex value eg FFFFFF or FF0000 .. no # at the front).

Code: Select all

class imagethumbnail_corners extends imagethumbnail {

		var $radius;
		var $color;

		function imagethumbnail_corners() {
			$this->radius = 10;
			$this->color = "FFFFFF";
		}
		
		function setRadius($radius="") {
			if (isset($radius)) { $this->radius = $radius; }
			return $this->radius;
		}
		
		function setColor($color="") {
			if (isset($color)) { $this->color = $color; }
			return $this->color;
		}

		function clipcorners() {

			if (!isset($this->thumbnail)) { $this->generate(); }
			
			$i_x = imagesx($this->thumbnail);
			$i_y = imagesy($this->thumbnail);
			$xrad = $this->radius;
			$yrad = $this->radius;
			$r = hexdec(substr($this->color,0,2));
			$g = hexdec(substr($this->color,2,2));
			$b = hexdec(substr($this->color,4,2));

			$gdCornerSource = imagecreatefromstring(base64_decode($this->cornerpng()));
			$gdCorner_x = imagesx($gdCornerSource);
			$gdCorner_y = imagesy($gdCornerSource);

			$gdCorner1 = imagecreatefromstring(base64_decode($this->blankpng()));
			$gdCorner2 = imagecreatefromstring(base64_decode($this->blankpng()));
			$gdCorner3 = imagecreatefromstring(base64_decode($this->blankpng()));
			$gdCorner4 = imagecreatefromstring(base64_decode($this->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($this->thumbnail,$gdCorner1,0,0,0,0,$xrad,$yrad,$gdCorner_x,$gdCorner_y);
			imagecopyresampled($this->thumbnail,$gdCorner2,$i_x-$xrad,0,0,0,$xrad,$yrad,$gdCorner_x,$gdCorner_y);
			imagecopyresampled($this->thumbnail,$gdCorner3,$i_x-$xrad,$i_y-$yrad,0,0,$xrad,$yrad,$gdCorner_x,$gdCorner_y);
			imagecopyresampled($this->thumbnail,$gdCorner4,0,$i_y-$yrad,0,0,$xrad,$yrad,$gdCorner_x,$gdCorner_y);

		}

		//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;

		}
	}
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
Post Reply