Highway to PSR2
[openemr.git] / portal / patient / fwk / libs / util / thumbnail.php
blobf3875b6c1c24d75fe6964187486a4e8a50cc26de
1 <?php
2 /**
4 * Enter description here ...
5 * @link http://www.electrictoolbox.com/php-generate-thumbnails/
6 * @author Chris Hope
8 */
10 /**
11 * thumbnail generator for creating smaller sized images and either saving them to disk
12 * or streaming them directly to the browser
14 * @example <pre>
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');
23 * </pre>
25 class thumbnail
27 var $allowableTypes = array (
28 IMAGETYPE_GIF,
29 IMAGETYPE_JPEG,
30 IMAGETYPE_PNG
32 public function imageCreateFromFile($filename, $imageType)
34 switch ($imageType) {
35 case IMAGETYPE_GIF:
36 return imagecreatefromgif($filename);
37 case IMAGETYPE_JPEG:
38 return imagecreatefromjpeg($filename);
39 case IMAGETYPE_PNG:
40 return imagecreatefrompng($filename);
41 default:
42 return false;
46 /**
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
58 * @param
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)) {
70 return false;
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;
79 } else {
80 $extension = strtolower($pathinfo ['extension']);
83 switch ($extension) {
84 case 'gif':
85 $function = 'imagegif';
86 break;
87 case 'png':
88 $function = 'imagepng';
89 break;
90 default:
91 $function = 'imagejpeg';
92 break;
95 // load the image and return false if didn't work
96 $source = $this->imageCreateFromFile($sourceFilename, $size [2]);
97 if (! $source) {
98 return false;
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");
105 } else {
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 );
113 } else {
114 $newWidth = 0;
115 $newHeight = 0;
117 if ($useExactSize) {
118 $newWidth = $maxWidth;
119 $newHeight = $maxHeight;
120 } else {
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);
128 } else {
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 );
139 return true;