4 * Enter description here ...
5 * @link http://www.electrictoolbox.com/php-generate-thumbnails/
11 * thumbnail generator for creating smaller sized images and either saving them to disk
12 * or streaming them directly to the browser
15 * // Create a thumbnail from /tmp/big.jpg and stream it to the browser with maximum dimensions of 100x100
16 * // this will default to jpg format and will send the necessary image type header:
17 * $tg = new thumbnailGenerator;
18 * $tg->generate('/tmp/big.jpg', 100, 100);
20 * // Create a thumbnail from /tmp/big.png and save it to /tmp/small.png with maximum dimensions of 100x100:
21 * $tg = new thumbnailGenerator;
22 * $tg->generate('/tmp/big.jpg', 100, 100, '/tmp/small.jpg');
27 var $allowableTypes = array (
32 public function imageCreateFromFile($filename, $imageType)
36 return imagecreatefromgif($filename);
38 return imagecreatefromjpeg($filename);
40 return imagecreatefrompng($filename);
47 * Generates a thumbnail image using the file at $sourceFilename and either writing it
48 * out to a new file or directly to the browser.
50 * @param string $sourceFilename
51 * Filename for the image to have thumbnail made from
52 * @param integer $maxWidth
53 * The maxium width for the resulting thumbnail
54 * @param integer $maxHeight
55 * The maxium height for the resulting thumbnail
56 * @param string $targetFormatOrFilename
57 * Either a filename extension (gif|jpg|png) or the
59 * bool set to true to set the image to the exact size given (stretching if necessary)
60 * filename the resulting file should be written to. This is optional and if not specified
61 * will send a jpg to the browser.
62 * @return boolean true if the image could be created, false if not
64 public function generate($sourceFilename, $maxWidth, $maxHeight, $targetFormatOrFilename = 'jpg', $useExactSize = false)
66 $size = getimagesize($sourceFilename); // 0 = width, 1 = height, 2 = type
68 // check to make sure source image is in allowable format
69 if (! in_array($size [2], $this->allowableTypes
)) {
73 // work out the extension, what target filename should be and output function to call
74 $pathinfo = pathinfo($targetFormatOrFilename);
75 if ($pathinfo ['basename'] == $pathinfo ['filename']) {
76 $extension = strtolower($targetFormatOrFilename);
77 // set target to null so writes out to browser
78 $targetFormatOrFilename = null;
80 $extension = strtolower($pathinfo ['extension']);
85 $function = 'imagegif';
88 $function = 'imagepng';
91 $function = 'imagejpeg';
95 // load the image and return false if didn't work
96 $source = $this->imageCreateFromFile($sourceFilename, $size [2]);
101 // write out the appropriate HTTP headers if going to browser
102 if ($targetFormatOrFilename == null) {
103 if ($extension == 'jpg') {
104 header("Content-Type: image/jpeg");
106 header("Content-Type: image/$extension");
110 // if the source fits within the maximum then no need to resize
111 if ($useExactSize == false && $size [0] <= $maxWidth && $size [1] <= $maxHeight) {
112 $function ( $source, $targetFormatOrFilename );
118 $newWidth = $maxWidth;
119 $newHeight = $maxHeight;
121 $ratioWidth = $maxWidth / $size [0];
122 $ratioHeight = $maxHeight / $size [1];
124 // use smallest ratio
125 if ($ratioWidth < $ratioHeight) {
126 $newWidth = $maxWidth;
127 $newHeight = round($size [1] * $ratioWidth);
129 $newWidth = round($size [0] * $ratioHeight);
130 $newHeight = $maxHeight;
134 $target = imagecreatetruecolor($newWidth, $newHeight);
135 imagecopyresampled($target, $source, 0, 0, 0, 0, $newWidth, $newHeight, $size [0], $size [1]);
136 $function ( $target, $targetFormatOrFilename );