ossn.lib.image.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Open Source Social Network
4  *
5  * @package (Informatikon.com).ossn
6  * @author OSSN Core Team <[email protected]>
7  * @copyright 2014 iNFORMATIKON TECHNOLOGIES
8  * @license General Public Licence http://www.opensource-socialnetwork.org/licence
9  * @link http://www.opensource-socialnetwork.org/licence
10  */
11 
12 /**
13  * Calculate the parameters for resizing an image
14  *
15  * @param int $width Width of the original image
16  * @param int $height Height of the original image
17  * @param array $options See $defaults for the options
18  *
19  * @return resource or FALSE
20  */
21 function ossn_image_resize_parameters($width, $height, $options) {
22 
23  $defaults = array(
24  'maxwidth' => 100,
25  'maxheight' => 100,
26 
27  'square' => FALSE,
28  'upscale' => FALSE,
29 
30  'x1' => 0,
31  'y1' => 0,
32  'x2' => 0,
33  'y2' => 0,
34  );
35 
36  $options = array_merge($defaults, $options);
37 
38  extract($options);
39 
40  // crop image first?
41  $crop = TRUE;
42  if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 == 0) {
43  $crop = FALSE;
44  }
45 
46  // how large a section of the image has been selected
47  if ($crop) {
48  $selection_width = $x2 - $x1;
49  $selection_height = $y2 - $y1;
50  } else {
51  // everything selected if no crop parameters
52  $selection_width = $width;
53  $selection_height = $height;
54  }
55 
56  // determine cropping offsets
57  if ($square) {
58  // asking for a square image back
59 
60  // detect case where someone is passing crop parameters that are not for a square
61  if ($crop == TRUE && $selection_width != $selection_height) {
62  return FALSE;
63  }
64 
65  // size of the new square image
66  $new_width = $new_height = min($maxwidth, $maxheight);
67 
68  // find largest square that fits within the selected region
69  $selection_width = $selection_height = min($selection_width, $selection_height);
70 
71  // set offsets for crop
72  if ($crop) {
73  $widthoffset = $x1;
74  $heightoffset = $y1;
75  $width = $x2 - $x1;
76  $height = $width;
77  } else {
78  // place square region in the center
79  $widthoffset = floor(($width - $selection_width) / 2);
80  $heightoffset = floor(($height - $selection_height) / 2);
81  }
82  } else {
83  // non-square new image
84  $new_width = $maxwidth;
85  $new_height = $maxheight;
86 
87  // maintain aspect ratio of original image/crop
88  if (($selection_height / (float)$new_height) > ($selection_width / (float)$new_width)) {
89  $new_width = floor($new_height * $selection_width / (float)$selection_height);
90  } else {
91  $new_height = floor($new_width * $selection_height / (float)$selection_width);
92  }
93 
94  // by default, use entire image
95  $widthoffset = 0;
96  $heightoffset = 0;
97 
98  if ($crop) {
99  $widthoffset = $x1;
100  $heightoffset = $y1;
101  }
102  }
103 
104  if (!$upscale && ($selection_height < $new_height || $selection_width < $new_width)) {
105  // we cannot upscale and selected area is too small so we decrease size of returned image
106  if ($square) {
107  $new_height = $selection_height;
108  $new_width = $selection_width;
109  } else {
110  if ($selection_height < $new_height && $selection_width < $new_width) {
111  $new_height = $selection_height;
112  $new_width = $selection_width;
113  }
114  }
115  }
116 
117  $params = array(
118  'newwidth' => $new_width,
119  'newheight' => $new_height,
120  'selectionwidth' => $selection_width,
121  'selectionheight' => $selection_height,
122  'xoffset' => $widthoffset,
123  'yoffset' => $heightoffset,
124  );
125 
126  return $params;
127 }
128 
129 /**
130  * Gets the jpeg contents of the resized version of an already uploaded image
131  * (Returns false if the file was not an image)
132  *
133  * @param string $input_name The name of the file on the disk
134  * @param int $maxwidth The desired width of the resized image
135  * @param int $maxheight The desired height of the resized image
136  * @param bool $square If set to true, takes the smallest of maxwidth and
137  * maxheight and use it to set the dimensions on the new image.
138  * If no crop parameters are set, the largest square that fits
139  * in the image centered will be used for the resize. If square,
140  * the crop must be a square region.
141  * @param int $x1 x coordinate for top, left corner
142  * @param int $y1 y coordinate for top, left corner
143  * @param int $x2 x coordinate for bottom, right corner
144  * @param int $y2 y coordinate for bottom, right corner
145  * @param bool $upscale Resize images smaller than $maxwidth x $maxheight?
146  *
147  * @return false|string The contents of the resized image, or false on failure
148  */
149 function ossn_resize_image($input_name, $maxwidth, $maxheight, $square = FALSE, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0, $upscale = FALSE) {
150 
151  // Get the size information from the image
152  $imgsizearray = getimagesize($input_name);
153  if ($imgsizearray == false) {
154  return false;
155  }
156 
157  $width = $imgsizearray[0];
158  $height = $imgsizearray[1];
159 
160  $accepted_formats = array(
161  'image/jpeg' => 'jpeg',
162  'image/pjpeg' => 'jpeg',
163  'image/png' => 'png',
164  'image/x-png' => 'png',
165  'image/gif' => 'gif'
166  );
167 
168  // make sure the function is available
169  $load_function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']];
170  if (!is_callable($load_function)) {
171  return false;
172  }
173 
174  // get the parameters for resizing the image
175  $options = array(
176  'maxwidth' => $maxwidth,
177  'maxheight' => $maxheight,
178  'square' => $square,
179  'upscale' => $upscale,
180  'x1' => $x1,
181  'y1' => $y1,
182  'x2' => $x2,
183  'y2' => $y2,
184  );
185  $params = ossn_image_resize_parameters($width, $height, $options);
186  if ($params == false) {
187  return false;
188  }
189 
190  // load original image
191  $original_image = call_user_func($load_function, $input_name);
192  if (!$original_image) {
193  return false;
194  }
195 
196  // allocate the new image
197  $new_image = imagecreatetruecolor($params['newwidth'], $params['newheight']);
198  if (!$new_image) {
199  return false;
200  }
201 
202  // color transparencies white (default is black)
203  imagefilledrectangle($new_image, 0, 0, $params['newwidth'], $params['newheight'], imagecolorallocate($new_image, 255, 255, 255));
204 
205  $rtn_code = imagecopyresampled($new_image, $original_image, 0, 0, $params['xoffset'], $params['yoffset'], $params['newwidth'], $params['newheight'], $params['selectionwidth'], $params['selectionheight']);
206  if (!$rtn_code) {
207  return false;
208  }
209 
210  // grab a compressed jpeg version of the image
211  ob_start();
212  imagejpeg($new_image, null, 90);
213  $jpeg = ob_get_clean();
214 
215  imagedestroy($new_image);
216  imagedestroy($original_image);
217 
218  return $jpeg;
219 }
220 
221 /**
222  * Convert image to jpeg and compress it
223  *
224  * @return The contents of generate image
225  */
226 function ossn_save_image($file, $destination) {
227  $info = getimagesize($file);
228  if ($info['mime'] == 'image/jpeg')
229  $image = imagecreatefromjpeg($file);
230 
231  elseif ($info['mime'] == 'image/gif')
232  $image = imagecreatefromgif($file);
233 
234  elseif ($info['mime'] == 'image/png')
235  $image = imagecreatefrompng($file);
236  imagejpeg($image, $destination, 90);
237 }
238 
239 /**
240  * Get image crop sizes for profile picture
241  *
242  * @return The contents of generate image
243  */
245  return array(
246  'topbar' => '20x20',
247  'small' => ' 50x50',
248  'smaller' => '32x32',
249  'large' => '100x100',
250  'larger' => '170x170',
251  );
252 }