1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: use Xoops\Core\Service\AbstractContract;
13: use Xoops\Core\Service\Contract\ThumbnailInterface;
14: use Xoops\Core\Service\Response;
15: use Xoops\Html\Img;
16:
17: /**
18: * Thumbnail provider for service manager
19: *
20: * @category ServiceProvider
21: * @package ThumbsProvider
22: * @author Richard Griffith <richard@geekwright.com>
23: * @copyright 2014 XOOPS Project (http://xoops.org)
24: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25: * @link http://xoops.org
26: */
27: class ThumbsProvider extends AbstractContract implements ThumbnailInterface
28: {
29: /** @var string $renderScript */
30: protected $renderScript = 'modules/thumbs/include/thumbrender.php';
31:
32: /**
33: * getName - get a short name for this service provider. This should be unique within the
34: * scope of the named service, so using module dirname is suggested.
35: *
36: * @return string - a unique name for the service provider
37: */
38: public function getName()
39: {
40: return 'thumbs';
41: }
42:
43: /**
44: * getDescription - get human readable description of the service provider
45: *
46: * @return string
47: */
48: public function getDescription()
49: {
50: return 'Thumbnail generation using stefangabos/zebra_image';
51: }
52:
53:
54: /**
55: * getThumbnailUrl
56: *
57: * @param string $imgPath path to image to be thumbed
58: * @param integer $width maximum width of thumbnail in pixels, 0 to use default
59: * @param integer $height maximum height of thumbnail in pixels, 0 to use default
60: *
61: * @return string URL to obtain QR Code image of $qrText
62: */
63: private function getThumbnailUrl($imgPath, $width, $height)
64: {
65: $xoops = \Xoops::getInstance();
66: $helper = $xoops->getModuleHelper('thumbs');
67: $thumbPath = $helper->buildThumbPath($imgPath, $width, $height);
68:
69: $originalMtime = filemtime($xoops->path($imgPath));
70: $thumbMtime = filemtime($xoops->path($thumbPath));
71: if (false===$thumbMtime || $originalMtime>$thumbMtime) {
72: $params = array(
73: 'img' => (string) $imgPath,
74: );
75: if ($height) {
76: $params['h'] = $height;
77: }
78: if ($width) {
79: $params['w'] = $width;
80: }
81: $url = $xoops->buildUrl($xoops->url($this->renderScript), $params);
82: } else {
83: $url = $xoops->url($thumbPath);
84: }
85:
86: return $url;
87: }
88:
89: /**
90: * getImgUrl - get URL to a thumbnail of the supplied image
91: *
92: * @param Response $response \Xoops\Core\Service\Response object
93: * @param string $imgPath path to image to be thumbed
94: * @param integer $width maximum width of thumbnail in pixels, 0 to use default
95: * @param integer $height maximum height of thumbnail in pixels, 0 to use default
96: *
97: * @return void - response->value set to URL string
98: */
99: public function getImgUrl(Response $response, $imgPath, $width = 0, $height = 0)
100: {
101: $response->setValue($this->getThumbnailUrl($imgPath, $width, $height));
102: }
103:
104: /**
105: * getImgTag - get a full HTML img tag to display a thumbnail of the supplied image
106: *
107: * @param Response $response \Xoops\Core\Service\Response object
108: * @param string $imgPath path to image to be thumbed
109: * @param integer $width maximum width of thumbnail in pixels, 0 to use default
110: * @param integer $height maximum height of thumbnail in pixels, 0 to use default
111: * @param array $attributes array of attribute name => value pairs for img tag
112: *
113: * @return void - response->value set to image tag
114: */
115: public function getImgTag(
116: Response $response,
117: $imgPath,
118: $width = 0,
119: $height = 0,
120: $attributes = array()
121: ) {
122: $url = $this->getThumbnailUrl($imgPath, $width, $height);
123: if (!is_array($attributes)) {
124: $attributes = array();
125: }
126:
127: $imgTag = new Img(array('src' => $url));
128: $imgTag->setMerge($attributes);
129: $response->setValue($imgTag->render());
130: }
131: }
132: