1: <?php
2:
3: namespace Alpha\Util\Image;
4:
5: use Alpha\Exception\IllegalArguementException;
6:
7: /**
8: * A utility class for carrying out various image file tasks.
9: *
10: * @since 1.1
11: *
12: * @author John Collins <dev@alphaframework.org>
13: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
14: * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
15: * All rights reserved.
16: *
17: * <pre>
18: * Redistribution and use in source and binary forms, with or
19: * without modification, are permitted provided that the
20: * following conditions are met:
21: *
22: * * Redistributions of source code must retain the above
23: * copyright notice, this list of conditions and the
24: * following disclaimer.
25: * * Redistributions in binary form must reproduce the above
26: * copyright notice, this list of conditions and the
27: * following disclaimer in the documentation and/or other
28: * materials provided with the distribution.
29: * * Neither the name of the Alpha Framework nor the names
30: * of its contributors may be used to endorse or promote
31: * products derived from this software without specific
32: * prior written permission.
33: *
34: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
39: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47: * </pre>
48: */
49: class ImageUtils
50: {
51: /**
52: * Generates a perfectly square thumbnail from the supplied original image file.
53: *
54: * @param string $original The path to the original file
55: * @param string $thumbnail The path to the new thumbnail file to generate
56: * @param int $dimensions The width/height of the generated thumbnail
57: *
58: * @throws Alpha\Exception\IllegalArguementException
59: *
60: * @since 1.1
61: */
62: public static function generateSquareThumbnail($original, $thumbnail, $dimensions)
63: {
64: if ($dimensions <= 0) {
65: throw new IllegalArguementException('Illegal dimensions value provided ['.$dimensions.'], should be greater than zero');
66: }
67:
68: $newImage = imagecreatetruecolor($dimensions, $dimensions);
69: $imageInfo = getimagesize($original);
70: $originalX = 0;
71:
72: switch ($imageInfo['mime']) {
73: case 'image/jpeg':
74: $type = 'jpg';
75: $originalImage = imagecreatefromjpeg($original);
76: break;
77: case 'image/gif':
78: $type = 'gif';
79: $originalImage = imagecreatefromgif($original);
80: break;
81: case 'image/png':
82: $type = 'png';
83: $originalImage = imagecreatefrompng($original);
84: break;
85: default:
86: throw new IllegalArguementException('Unsupported image format ['.$imageInfo['mime'].']');
87: }
88:
89: // in case the destination type is different from the source...
90: $pathParts = pathinfo($thumbnail);
91: if (!isset($pathParts['extension'])) {
92: $type = $pathParts['extension'];
93: }
94:
95: list($originalWidth, $originalHeight) = $imageInfo;
96:
97: if ($originalWidth > $originalHeight) {
98: $originalX = floor(($originalWidth - $originalHeight) / 2);
99: $sourceWidth = $sourceHeight = $originalHeight;
100: } else {
101: $sourceWidth = $sourceHeight = $originalWidth;
102: }
103:
104: imagecopyresampled($newImage, $originalImage, 0, 0, $originalX, 0, $dimensions, $dimensions, $sourceWidth, $sourceHeight);
105:
106: return self::saveImage($newImage, $type, $thumbnail);
107: }
108:
109: /**
110: * Saves the GD image resource to the file path indicated.
111: *
112: * @param image $imageResource The GD image resource to save
113: * @param string $type The image type (jpg, png, or gif)
114: * @param string $destination The desination file path of the image file to create
115: *
116: * @throws Alpha\Exception\IllegalArguementException
117: *
118: * @since 1.1
119: */
120: public static function saveImage($imageResource, $type, $destination)
121: {
122: if (!in_array($type, array('jpg', 'png', 'gif'))) {
123: throw new IllegalArguementException('Illegal image type ['.$type.'], cannot create file');
124: }
125:
126: if (($type == 'jpg')) {
127: imagejpeg($imageResource, $destination);
128: } else {
129: $function = 'image'.$type;
130:
131: $function($imageResource, $destination);
132: }
133:
134: // free up memory
135: imagedestroy($imageResource);
136: }
137: }
138: