fix: x12837 billing 5 or 9 digit zip check (#7760)
[openemr.git] / portal / patient / fwk / libs / util / thumbnail.php
blobedb5f0350fd7cd15ffcc198dba07be98c86936fd
1 <?php
3 /**
5 * Enter description here ...
6 * @link http://www.electrictoolbox.com/php-generate-thumbnails/
7 * @author Chris Hope
9 */
11 /**
12 * thumbnail generator for creating smaller sized images and either saving them to disk
13 * or streaming them directly to the browser
15 * @example <pre>
16 * // Create a thumbnail from /tmp/big.jpg and stream it to the browser with maximum dimensions of 100x100
17 * // this will default to jpg format and will send the necessary image type header:
18 * $tg = new thumbnailGenerator;
19 * $tg->generate('/tmp/big.jpg', 100, 100);
21 * // Create a thumbnail from /tmp/big.png and save it to /tmp/small.png with maximum dimensions of 100x100:
22 * $tg = new thumbnailGenerator;
23 * $tg->generate('/tmp/big.jpg', 100, 100, '/tmp/small.jpg');
24 * </pre>
26 class thumbnail
28 var $allowableTypes = array (
29 IMAGETYPE_GIF,
30 IMAGETYPE_JPEG,
31 IMAGETYPE_PNG
33 public function imageCreateFromFile($filename, $imageType)
35 switch ($imageType) {
36 case IMAGETYPE_GIF:
37 return imagecreatefromgif($filename);
38 case IMAGETYPE_JPEG:
39 return imagecreatefromjpeg($filename);
40 case IMAGETYPE_PNG:
41 return imagecreatefrompng($filename);
42 default:
43 return false;
47 /**
48 * Generates a thumbnail image using the file at $sourceFilename and either writing it
49 * out to a new file or directly to the browser.
51 * @param string $sourceFilename
52 * Filename for the image to have thumbnail made from
53 * @param integer $maxWidth
54 * The maxium width for the resulting thumbnail
55 * @param integer $maxHeight
56 * The maxium height for the resulting thumbnail
57 * @param string $targetFormatOrFilename
58 * Either a filename extension (gif|jpg|png) or the
59 * @param
60 * bool set to true to set the image to the exact size given (stretching if necessary)
61 * filename the resulting file should be written to. This is optional and if not specified
62 * will send a jpg to the browser.
63 * @return boolean true if the image could be created, false if not
65 public function generate($sourceFilename, $maxWidth, $maxHeight, $targetFormatOrFilename = 'jpg', $useExactSize = false)
67 $size = getimagesize($sourceFilename); // 0 = width, 1 = height, 2 = type
69 // check to make sure source image is in allowable format
70 if (! in_array($size [2], $this->allowableTypes)) {
71 return false;
74 // work out the extension, what target filename should be and output function to call
75 $pathinfo = pathinfo($targetFormatOrFilename);
76 if ($pathinfo ['basename'] == $pathinfo ['filename']) {
77 $extension = strtolower($targetFormatOrFilename);
78 // set target to null so writes out to browser
79 $targetFormatOrFilename = null;
80 } else {
81 $extension = strtolower($pathinfo ['extension']);
84 switch ($extension) {
85 case 'gif':
86 $function = 'imagegif';
87 break;
88 case 'png':
89 $function = 'imagepng';
90 break;
91 default:
92 $function = 'imagejpeg';
93 break;
96 // load the image and return false if didn't work
97 $source = $this->imageCreateFromFile($sourceFilename, $size [2]);
98 if (! $source) {
99 return false;
102 // write out the appropriate HTTP headers if going to browser
103 if ($targetFormatOrFilename == null) {
104 if ($extension == 'jpg') {
105 header("Content-Type: image/jpeg");
106 } else {
107 header("Content-Type: image/$extension");
111 // if the source fits within the maximum then no need to resize
112 if ($useExactSize == false && $size [0] <= $maxWidth && $size [1] <= $maxHeight) {
113 $function($source, $targetFormatOrFilename);
114 } else {
115 $newWidth = 0;
116 $newHeight = 0;
118 if ($useExactSize) {
119 $newWidth = $maxWidth;
120 $newHeight = $maxHeight;
121 } else {
122 $ratioWidth = $maxWidth / $size [0];
123 $ratioHeight = $maxHeight / $size [1];
125 // use smallest ratio
126 if ($ratioWidth < $ratioHeight) {
127 $newWidth = $maxWidth;
128 $newHeight = round($size [1] * $ratioWidth);
129 } else {
130 $newWidth = round($size [0] * $ratioHeight);
131 $newHeight = $maxHeight;
135 $target = imagecreatetruecolor($newWidth, $newHeight);
136 imagecopyresampled($target, $source, 0, 0, 0, 0, $newWidth, $newHeight, $size [0], $size [1]);
137 $function($target, $targetFormatOrFilename);
140 return true;