1 <?php
2
3 /* --------------------------------------------------------------
4 LegacyProductImageProcessing.inc.php 2016-01-27
5 Gambio GmbH
6 http://www.gambio.de
7 Copyright (c) 2016 Gambio GmbH
8 Released under the GNU General Public License (Version 2)
9 [http://www.gnu.org/licenses/gpl-2.0.html]
10 --------------------------------------------------------------
11 */
12
13 /**
14 * Class LegacyProductImageProcessing
15 *
16 * @category System
17 * @package Product
18 * @subpackage Entities
19 */
20 class LegacyProductImageProcessing implements ProductImageProcessingInterface
21 {
22 /**
23 * Proceed Image
24 *
25 * Processes an image for the front end.
26 *
27 * @param FilenameStringType $image Image to proceed.
28 *
29 * @throws InvalidArgumentException if the provided image name is not valid.
30 * @throws FileNotFoundException if the provided image was not found.
31 * @throws RuntimeException if the PHP GD library is not installed.
32 *
33 * @return LegacyProductImageProcessing Same instance for chained method calls.
34 */
35 public function proceedImage(FilenameStringType $image)
36 {
37 if(!$this->_isGdLibraryInstalled())
38 {
39 // @codeCoverageIgnoreStart
40 throw new \RuntimeException('PHP GD library is not installed on your server.');
41 // @codeCoverageIgnoreEnd
42 }
43
44 // Variable name cannot be changed, because it is needed by the include files.
45 $products_image_name = $image->asString();
46
47 $this->_defineNeededConstantsForIncludes();
48
49 $this->_throwExceptionIfImageDoesNotExist($products_image_name);
50
51 require_once(DIR_FS_CATALOG . 'admin/includes/classes/' . IMAGE_MANIPULATOR);
52
53 // If an error emerges, the value will be set to true in one of the include files.
54 // This variable name also has to be named exactly like this.
55 $image_error = false;
56
57 // Images will be processed in these includes.
58 // product_popup_images.php has to be included first, because a function will be defined here
59 // if it does not exist yet.
60 include(DIR_FS_CATALOG . 'admin/includes/' . 'product_popup_images.php');
61 include(DIR_FS_CATALOG . 'admin/includes/' . 'product_info_images.php');
62 include(DIR_FS_CATALOG . 'admin/includes/' . 'product_thumbnail_images.php');
63 include(DIR_FS_CATALOG . 'admin/includes/' . 'product_gallery_images.php');
64
65 if($image_error)
66 {
67 // @codeCoverageIgnoreStart
68 throw new \InvalidArgumentException('Image: ' . $products_image_name
69 . ' could not be processed. Please check, if the provided name is valid.');
70 // @codeCoverageIgnoreEnd
71 }
72
73 return $this;
74 }
75
76
77 /**
78 * Define Needed Constants for Includes
79 *
80 * These constants are needed for the include files. The global constants defined in admin/includes/configure.php
81 * are not available, because the current file is within in the src/includes folder and thus, is using the
82 * src/includes/configure.php, in which those constants are either not defined or not correct for this
83 * class.
84 */
85 protected function _defineNeededConstantsForIncludes()
86 {
87 // Has to be defined as true, in order to include IMAGE_MANIPULATOR (image_manipulator_GD2.php),
88 // else an error of 'No direct access allowed' will be thrown.
89 if(!defined('_VALID_XTC'))
90 {
91 define(_VALID_XTC, true);
92 }
93
94 // Image folders.
95 if(!defined('DIR_FS_CATALOG_IMAGES'))
96 {
97 define('DIR_FS_CATALOG_IMAGES', DIR_FS_CATALOG . '/images/');
98 }
99 if(!defined('DIR_FS_CATALOG_ORIGINAL_IMAGES'))
100 {
101 define('DIR_FS_CATALOG_ORIGINAL_IMAGES', DIR_FS_CATALOG . 'images/product_images/original_images/');
102 }
103 if(!defined('DIR_FS_CATALOG_POPUP_IMAGES'))
104 {
105 define('DIR_FS_CATALOG_POPUP_IMAGES', DIR_FS_CATALOG . '/images/product_images/popup_images/');
106 }
107 if(!defined('DIR_FS_CATALOG_INFO_IMAGES'))
108 {
109 define('DIR_FS_CATALOG_INFO_IMAGES', DIR_FS_CATALOG . '/images/product_images/info_images/');
110 }
111 if(!defined('DIR_FS_CATALOG_THUMBNAIL_IMAGES'))
112 {
113 define('DIR_FS_CATALOG_THUMBNAIL_IMAGES', DIR_FS_CATALOG . '/images/product_images/thumbnail_images/');
114 }
115 if(!defined('DIR_FS_CATALOG_GALLERY_IMAGES'))
116 {
117 define('DIR_FS_CATALOG_GALLERY_IMAGES', DIR_FS_CATALOG . '/images/product_images/gallery_images/');
118 }
119 }
120
121
122 /**
123 * Is GD Library Installed
124 *
125 * Checks if the GD library is installed, which is necessary for the image processing.
126 * Returns true if the library is installed.
127 *
128 * @return bool Is the PHP GD library installed?
129 */
130 protected function _isGdLibraryInstalled()
131 {
132 return (extension_loaded('gd') && function_exists('gd_info'));
133 }
134
135
136 /**
137 * Throw Exception If Image Does Not Exist
138 *
139 * Checks if the image exist in the folder original_images.
140 * If the image does not exist, an exception will be thrown.
141 *
142 * @throws \FileNotFoundException if the image was not found.
143 *
144 * @param $image Image to check.
145 */
146 protected function _throwExceptionIfImageDoesNotExist($image)
147 {
148 if(!file_exists(DIR_FS_CATALOG_ORIGINAL_IMAGES . $image))
149 {
150 throw new \FileNotFoundException($image . ' does not exist in ' . DIR_FS_CATALOG_ORIGINAL_IMAGES);
151 }
152 }
153 }
154