1: <?php defined('_JOOS_CORE') or exit();
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class joosImage
17: {
18: 19: 20: 21: 22: 23: 24: 25: 26:
27: public static function get_image_from_text($text, $default_image = null)
28: {
29: $matches = array();
30: $regex = '#<img[^>]*src=(["\'])([^"\']*)\1[^>]*>#is';
31: if (preg_match($regex, $text, $matches)) {
32: $img = $matches[2];
33:
34: return $img;
35: } elseif ($default_image) {
36: return '/images/noimage.jpg';
37: } else {
38: return false;
39: }
40: }
41:
42: }
43:
44: 45: 46: 47:
48: define('THUMBNAIL_METHOD_SCALE_MAX', 0);
49:
50: 51: 52: 53:
54: define('THUMBNAIL_METHOD_SCALE_MIN', 1);
55:
56: 57: 58: 59:
60: define('THUMBNAIL_METHOD_CROP', 2);
61:
62: 63: 64:
65: define('THUMBNAIL_ALIGN_CENTER', 0);
66: define('THUMBNAIL_ALIGN_LEFT', -1);
67: define('THUMBNAIL_ALIGN_RIGHT', +1);
68: define('THUMBNAIL_ALIGN_TOP', -1);
69: define('THUMBNAIL_ALIGN_BOTTOM', +1);
70:
71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85:
86: class Thumbnail
87: {
88: private static $debug = array();
89:
90: public static function get_debug()
91: {
92: return self::$debug;
93: }
94:
95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109:
110: public static function imageCreate($input)
111: {
112: if (joosFile::exists($input)) {
113: return Thumbnail::imageCreateFromFile($input);
114: } elseif (is_string($input)) {
115: return Thumbnail::imageCreateFromString($input);
116: } else {
117: return $input;
118: }
119: }
120:
121: 122: 123: 124: 125: 126: 127: 128: 129:
130: public static function imageCreateFromFile($filename)
131: {
132: if (!joosFile::exists($filename) || !joosFile::is_readable($filename)) {
133: throw new joosImageLibrariesException('Unable to open file "' . $filename . '"', E_USER_NOTICE);
134: }
135:
136:
137: list(, , $type) = getimagesize($filename);
138:
139: switch ($type) {
140:
141: case IMAGETYPE_JPEG:
142: return imagecreatefromjpeg($filename);
143: break;
144:
145: case IMAGETYPE_GIF:
146: return imagecreatefromgif($filename);
147: break;
148:
149: case IMAGETYPE_PNG:
150: return imagecreatefrompng($filename);
151: break;
152: }
153: throw new joosImageLibrariesException('Unsupport image type', E_USER_NOTICE);
154: }
155:
156: 157: 158: 159: 160: 161: 162: 163: 164:
165: public static function imageCreateFromString($string)
166: {
167: if (!is_string($string) || empty($string)) {
168: throw new joosImageLibrariesException('Invalid image value in string', E_USER_NOTICE);
169: }
170:
171: return imagecreatefromstring($string);
172: }
173:
174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196:
197: public static function output($input, $output = null, $options = array())
198: {
199:
200: $renderImage = Thumbnail::render($input, $options);
201: if (!$renderImage) {
202: throw new joosImageLibrariesException('Error rendering image', E_USER_NOTICE);
203: }
204:
205:
206:
207: $type = isset($options['type']) ? $options['type'] : IMAGETYPE_PNG;
208: $quality = isset($options['quality']) ? $options['quality'] : ($type == IMAGETYPE_PNG ? 8 : 80);
209: $quality = ($type == IMAGETYPE_PNG ? (int) $quality / 10 : $quality);
210:
211: if (empty($output)) {
212: $content_type = image_type_to_mime_type($type);
213: if (!headers_sent()) {
214: joosRequest::send_headers('Content-Type: ' . $content_type);
215: } else {
216: throw new joosImageLibrariesException('Headers have already been sent. Could not display image.', E_USER_NOTICE);
217: }
218: } else {
219: $content_type = 'ERROR';
220: }
221:
222: switch ($type) {
223: case IMAGETYPE_GIF:
224: $result = empty($output) ? imagegif($renderImage) : imagegif($renderImage, $output);
225: break;
226:
227: case IMAGETYPE_PNG:
228: $result = empty($output) ? imagepng($renderImage) : imagepng($renderImage, $output, $quality);
229: break;
230:
231: case IMAGETYPE_JPEG:
232: $result = empty($output) ? imagejpeg($renderImage) : imagejpeg($renderImage, $output, $quality);
233: break;
234: default:
235: throw new joosImageLibrariesException('Image type ' . $content_type . ' not supported by PHP', E_USER_NOTICE);
236: }
237:
238: if (!$result) {
239: throw new joosImageLibrariesException('Error output image', E_USER_NOTICE);
240: }
241:
242:
243: imagedestroy($renderImage);
244:
245: return true;
246: }
247:
248: 249: 250: 251: 252: 253: 254: 255: 256: 257:
258: public static function render($input, $options = array())
259: {
260:
261: $sourceImage = Thumbnail::imageCreate($input);
262: if (!is_resource($sourceImage)) {
263: throw new joosImageLibrariesException('Invalid image resource', E_USER_NOTICE);
264: }
265: $sourceWidth = imagesx($sourceImage);
266: $sourceHeight = imagesy($sourceImage);
267:
268:
269: static $defOptions = array('width' => 150, 'height' => 150, 'method' => THUMBNAIL_METHOD_SCALE_MAX, 'percent' => 0, 'halign' => THUMBNAIL_ALIGN_CENTER, 'valign' => THUMBNAIL_ALIGN_CENTER, 'check_size' => 0, 'resize' => 1);
270: foreach ($defOptions as $k => $v) {
271: if (!isset($options[$k])) {
272: $options[$k] = $v;
273: }
274: }
275:
276: $resize = 1;
277: if (($options['check_size'] == 1 && $sourceWidth <= $options['width'] && $sourceHeight <= $options['height']) || $options['resize'] == 0) {
278: $resize = 0;
279: }
280:
281: if ($resize) {
282:
283:
284: if ($options['method'] == THUMBNAIL_METHOD_CROP) {
285: if ($options['percent']) {
286: $W = floor($options['percent'] * $sourceWidth);
287: $H = floor($options['percent'] * $sourceHeight);
288: } else {
289: $W = $options['width'];
290: $H = $options['height'];
291: }
292:
293: $width = $W;
294: $height = $H;
295:
296: $Y = Thumbnail::_coord($options['valign'], $sourceHeight, $H);
297: $X = Thumbnail::_coord($options['halign'], $sourceWidth, $W);
298: } else {
299: $X = 0;
300: $Y = 0;
301:
302: $W = $sourceWidth;
303: $H = $sourceHeight;
304:
305: if ($options['percent']) {
306: $width = floor($options['percent'] * $W);
307: $height = floor($options['percent'] * $H);
308: } else {
309: $width = $options['width'];
310: $height = $options['height'];
311:
312: if ($options['method'] == THUMBNAIL_METHOD_SCALE_MIN) {
313: $Ww = $W / $width;
314: $Hh = $H / $height;
315: if ($Ww > $Hh) {
316: $W = floor($width * $Hh);
317: $X = Thumbnail::_coord($options['halign'], $sourceWidth, $W);
318: } else {
319: $H = floor($height * $Ww);
320: $Y = Thumbnail::_coord($options['valign'], $sourceHeight, $H);
321: }
322: } else {
323: if ($H > $W) {
324: $width = floor($height / $H * $W);
325: } else {
326: $height = floor($width / $W * $H);
327: }
328: }
329: }
330: }
331: } else {
332: $W = $sourceWidth;
333: $H = $sourceHeight;
334: $width = $sourceWidth;
335: $height = $sourceHeight;
336: $X = 0;
337: $Y = 0;
338: }
339:
340:
341: if (function_exists('imagecreatetruecolor')) {
342: $targetImage = imagecreatetruecolor($width, $height);
343: } else {
344: $targetImage = imagecreate($width, $height);
345: }
346: if (!is_resource($targetImage)) {
347: throw new joosImageLibrariesException('Cannot initialize new GD image stream', E_USER_NOTICE);
348:
349: return false;
350: }
351:
352: if ($options['method'] == THUMBNAIL_METHOD_CROP && isset($options['x']) && isset($options['y'])) {
353: $X = $options['x'];
354: $Y = $options['y'];
355: }
356:
357:
358: if ($options['method'] == THUMBNAIL_METHOD_CROP) {
359: $result = imagecopy($targetImage, $sourceImage, 0, 0, $X, $Y, $W, $H);
360: } elseif (function_exists('imagecopyresampled')) {
361: $result = imagecopyresampled($targetImage, $sourceImage, 0, 0, $X, $Y, $width, $height, $W, $H);
362: } else {
363: $result = imagecopyresized($targetImage, $sourceImage, 0, 0, $X, $Y, $width, $height, $W, $H);
364: }
365:
366: if (!$result) {
367:
368: throw new joosImageLibrariesException('Cannot resize image', E_USER_NOTICE);
369: }
370:
371:
372: imagedestroy($sourceImage);
373:
374: return $targetImage;
375: }
376:
377: private static function _coord($align, $param, $src)
378: {
379: if ($align < THUMBNAIL_ALIGN_CENTER) {
380: $result = 0;
381: } elseif ($align > THUMBNAIL_ALIGN_CENTER) {
382: $result = $param - $src;
383: } else {
384: $result = ($param - $src) >> 1;
385: }
386:
387: return $result;
388: }
389:
390: 391: 392: 393: 394: 395: 396: 397:
398: public static function create_thumbs($original, $path, $params, $ext = 'jpg', $quality = 80)
399: {
400:
401: list($width, $height) = getimagesize($original);
402: if ($width > $height) {
403: $o = 'album';
404: } elseif ($height > $width) {
405: $o = 'portret';
406: } else {
407: $o = 'square';
408: }
409:
410: $thumb_params = array();
411: $thumb_params['check_size'] = 1;
412: $thumb_params['quality'] = $quality;
413: $thumb_params['type'] = IMAGETYPE_JPEG;
414:
415: foreach ($params as $key => $thumb) {
416: $thumb_params['resize'] = 1;
417:
418: if ($thumb[0] && $thumb[0] > 0) {
419: $thumb_params['width'] = $thumb[0];
420: }
421:
422: if (isset($thumb[1])) {
423: $thumb_params['height'] = $thumb[1];
424: }
425:
426:
427: if (isset($thumb_params['width']) && !isset($thumb_params['height'])) {
428:
429:
430: if ($width <= $thumb_params['width']) {
431: $thumb_params['resize'] = 0;
432: }
433:
434: switch ($o) {
435: case 'album':
436: case 'square':
437: default:
438:
439: $thumb_params['method'] = THUMBNAIL_METHOD_SCALE_MAX;
440: break;
441:
442: case 'portret':
443:
444: $thumb_params['method'] = THUMBNAIL_METHOD_SCALE_MIN;
445: $thumb_params['height'] = floor(($height * $thumb_params['width']) / $width);
446: break;
447: }
448: }
449:
450:
451: else if (isset($thumb_params['height']) && !isset($thumb_params['width'])) {
452:
453:
454: if ($height <= $thumb_params['height']) {
455: $thumb_params['resize'] = 0;
456: }
457:
458: switch ($o) {
459: case 'album':
460: case 'square':
461: default:
462:
463: $thumb_params['method'] = THUMBNAIL_METHOD_SCALE_MIN;
464:
465: break;
466:
467: case 'portret':
468:
469: $thumb_params['method'] = THUMBNAIL_METHOD_SCALE_MAX;
470: break;
471: }
472: }
473:
474:
475: else if (isset($thumb_params['width']) && isset($thumb_params['height'])) {
476:
477: switch ($o) {
478: case 'album':
479: case 'square':
480: default:
481: if ($width > $thumb_params['width']) {
482:
483: $thumb_params['method'] = THUMBNAIL_METHOD_SCALE_MIN;
484: Thumbnail::output($original, $path . '/' . $key . '.' . $ext, $thumb_params);
485: } else {
486: $thumb_params['resize'] = 0;
487: }
488:
489: break;
490:
491: case 'portret':
492: if ($height > $thumb_params['height']) {
493:
494: $thumb_params['method'] = THUMBNAIL_METHOD_SCALE_MIN;
495: Thumbnail::output($original, $path . '/' . $key . '.' . $ext, $thumb_params);
496: } else {
497: $thumb_params['resize'] = 0;
498: }
499: break;
500: }
501:
502:
503: $thumb_params['method'] = THUMBNAIL_METHOD_CROP;
504:
505: if ($thumb_params['resize'] == 1) {
506: $original = $path . '/' . $key . '.' . $ext;
507: }
508: }
509:
510: Thumbnail::output($original, $path . '/' . $key . '.' . $ext, $thumb_params);
511: }
512: }
513:
514: }
515:
516: 517: 518: 519:
520: class joosImageLibrariesException extends joosException
521: {
522: }
523: