1: <?php
2:
3: namespace Alpha\Util\Extension;
4:
5: use Alpha\Util\Config\ConfigProvider;
6: use Alpha\Util\Logging\Logger;
7: use Alpha\Util\Helper\Validator;
8: use Alpha\Controller\Front\FrontController;
9: use Alpha\View\Widget\Image;
10:
11: /**
12: * Custom version of the TCPDF library class, allowing for any required overrides.
13: *
14: * @since 1.0
15: *
16: * @author John Collins <dev@alphaframework.org>
17: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
18: * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
19: * All rights reserved.
20: *
21: * <pre>
22: * Redistribution and use in source and binary forms, with or
23: * without modification, are permitted provided that the
24: * following conditions are met:
25: *
26: * * Redistributions of source code must retain the above
27: * copyright notice, this list of conditions and the
28: * following disclaimer.
29: * * Redistributions in binary form must reproduce the above
30: * copyright notice, this list of conditions and the
31: * following disclaimer in the documentation and/or other
32: * materials provided with the distribution.
33: * * Neither the name of the Alpha Framework nor the names
34: * of its contributors may be used to endorse or promote
35: * products derived from this software without specific
36: * prior written permission.
37: *
38: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
39: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
40: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
43: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
49: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51: * </pre>
52: */
53: class TCPDF extends \TCPDF
54: {
55: /**
56: * Trace logger.
57: *
58: * @var Alpha\Util\Logging\Logger
59: *
60: * @since 1.0
61: */
62: private static $logger = null;
63:
64: /**
65: * Overrides the TCPDF::Image method to decrypt encrypted $file paths from the Image widget, then pass
66: * them to the normal TCPDF::Image along with all of the other (unmodified) parameters.
67: *
68: * @param string $file Name of the file containing the image.
69: * @param float $x Abscissa of the upper-left corner.
70: * @param float $y Ordinate of the upper-left corner.
71: * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
72: * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
73: * @param string $type Image format. Possible values are (case insensitive): JPEG and PNG (whitout GD library) and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;. If not specified, the type is inferred from the file extension.
74: * @param mixed $link URL or identifier returned by AddLink().
75: * @param string $align Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
76: * @param bool $resize If true resize (reduce) the image to fit $w and $h (requires GD library).
77: * @param int $dpi dot-per-inch resolution used on resize
78: * @param string $palign Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
79: * @param bool $ismask true if this image is a mask, false otherwise
80: * @param mixed $imgmask image object returned by this function or false
81: * @param mixed $border Indicates if borders must be drawn around the image. The value can be either a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul>or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul>
82: *
83: * @since 1.0
84: */
85: public function Image($file, $x = '', $y = '', $w = 0, $h = 0, $type = '', $link = '', $align = '', $resize = false, $dpi = 300, $palign = '', $ismask = false, $imgmask = false, $border = 0)
86: {
87: if (self::$logger == null) {
88: self::$logger = new Logger('TCPDF');
89: }
90:
91: $config = ConfigProvider::getInstance();
92:
93: self::$logger->debug('Processing image file URL ['.$file.']');
94:
95: try {
96: if (mb_strpos($file, '/tk/') !== false) {
97: $start = mb_strpos($file, '/tk/') + 3;
98: $end = mb_strlen($file);
99: $tk = mb_substr($file, $start + 1, $end - ($start + 1));
100: $decoded = FrontController::getDecodeQueryParams($tk);
101:
102: parent::Image($decoded['source'], $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $ismask, $imgmask, $border);
103: } else {
104: // it has no query string, so threat as a regular image URL
105: if (Validator::isURL($file)) {
106: parent::Image($config->get('app.root').'/'.Image::convertImageURLToPath($file), $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $ismask, $imgmask, $border);
107: } else {
108: parent::Image($file, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $ismask, $imgmask, $border);
109: }
110: }
111: } catch (\Exception $e) {
112: self::$logger->error('Error processing image file URL ['.$file.'], error ['.$e->getMessage().']');
113: throw $e;
114: }
115: }
116: }
117: