Browse code

Add getResizedImages method to Pageimage class

Benjamin Roth authored on28/01/2016 15:08:14
Showing1 changed files
... ...
@@ -143,6 +143,72 @@ class Pageimage extends \Frontend
143 143
 
144 144
     }
145 145
 
146
+    public static function getResizedImages()
147
+    {
148
+        $arrImages = static::getImages();
149
+        $arrResizedImages = array();
150
+
151
+        foreach ($arrImages as $key=>$arrItem)
152
+        {
153
+            try
154
+            {
155
+                $objFile = new \File($arrItem['singleSRC'], true);
156
+            }
157
+            catch (\Exception $e)
158
+            {
159
+                $objFile = new \stdClass();
160
+                $objFile->imageSize = false;
161
+            }
162
+
163
+            $imgSize = $objFile->imageSize;
164
+            $size = deserialize($arrItem['size']);
165
+
166
+            $intMaxWidth = (TL_MODE == 'BE') ? 320 : \Config::get('maxImageWidth');
167
+
168
+            // Adjust the image size
169
+            if ($intMaxWidth > 0)
170
+            {
171
+                if ($size[0] > $intMaxWidth || (!$size[0] && !$size[1] && $imgSize[0] > $intMaxWidth))
172
+                {
173
+                    // See #2268 (thanks to Thyon)
174
+                    $ratio = ($size[0] && $size[1]) ? $size[1] / $size[0] : $imgSize[1] / $imgSize[0];
175
+
176
+                    $size[0] = $intMaxWidth;
177
+                    $size[1] = floor($intMaxWidth * $ratio);
178
+                }
179
+            }
180
+
181
+            try
182
+            {
183
+                $src = \Image::create($arrItem['singleSRC'], $size)->executeResize()->getResizedPath();
184
+                $picture = \Picture::create($arrItem['singleSRC'], $size)->getTemplateData();
185
+
186
+                if ($src !== $arrItem['singleSRC'])
187
+                {
188
+                    $objFile = new \File(rawurldecode($src), true);
189
+                }
190
+            }
191
+            catch (\Exception $e)
192
+            {
193
+                \System::log('Image "' . $arrItem['singleSRC'] . '" could not be processed: ' . $e->getMessage(), __METHOD__, TL_ERROR);
194
+
195
+                $src = '';
196
+                $picture = array('img'=>array('src'=>'', 'srcset'=>''), 'sources'=>array());
197
+            }
198
+
199
+            $arrResizedImages[$key] = array
200
+            (
201
+                'name'      => $arrItem['name'],
202
+                'singleSRC' => $objFile->path,
203
+                'alt'       => $arrItem['alt'],
204
+                'imageUrl'  => $arrItem['imageUrl'],
205
+                'caption'   => $arrItem['caption']
206
+            );
207
+        }
208
+
209
+        return $arrResizedImages;
210
+    }
211
+
146 212
     public static function getPageImage($strType='images')
147 213
     {
148 214
         $Hook = new HookPageimage();