function.image.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
13 /**
14  * Render an responsive image tag using srcset and sizes attributes. The plugin
15  * will prepend the frontend cache directory (_FrontendCache_ configuration section)
16  * to the image locations in the srcset attribute.
17  *
18  * Example:
19  * @code
20  * {res_image src='images/poster.jpg' widths="1600,960,640" type="w"
21  * sizes="(min-width: 50em) 33vw, (min-width: 28em) 50vw, 100vw"
22  * alt="Image 1" default="images/blank.gif"}
23  * @endcode
24  *
25  * @note The plugin is capable of resizing the image according to the srcset sizes,
26  * but this feature is disabled by default, because it might be very memory consuming
27  * (see __generate__ option). A better option is to resize the images on demand
28  * by a separate script. To do this, add the following lines to the .htaccess file
29  * in the application's root directory:
30  *
31  * @code
32  * # responsive images (cache/ is the frontend cache directory)
33  * RewriteCond %{REQUEST_URI} cache/
34  * RewriteCond %{REQUEST_FILENAME} !-f
35  * RewriteRule ^cache/(.+)$ image.php?file=$1 [NC,L]
36  * @endcode
37  *
38  * The resize script would could like this:
39  *
40  * @code
41  * <?php
42  * error_reporting(E_ERROR);
43  *
44  * define('WCMF_BASE', realpath("./cms/")."/");
45  * require_once(WCMF_BASE."/vendor/autoload.php");
46  *
47  * use wcmf\lib\config\impl\InifileConfiguration;
48  * use wcmf\lib\core\ClassLoader;
49  * use wcmf\lib\core\impl\DefaultFactory;
50  * use wcmf\lib\core\impl\MonologFileLogger;
51  * use wcmf\lib\core\LogManager;
52  * use wcmf\lib\core\ObjectFactory;
53  * use wcmf\lib\io\ImageUtil;
54  *
55  * new ClassLoader(WCMF_BASE);
56  *
57  * $configPath = WCMF_BASE.'app/config/';
58  *
59  * // setup logging
60  * $logger = new MonologFileLogger('main', $configPath.'log.ini');
61  * LogManager::configure($logger);
62  *
63  * // setup configuration
64  * $configuration = new InifileConfiguration($configPath);
65  * $configuration->addConfiguration('frontend.ini');
66  *
67  * // setup object factory
68  * ObjectFactory::configure(new DefaultFactory($configuration));
69  * ObjectFactory::registerInstance('configuration', $configuration);
70  *
71  * // the cache location is stored in the 'file' request parameter
72  * $location = filter_input(INPUT_GET, 'file', FILTER_SANITIZE_STRING);
73  * ImageUtil::getCachedImage($location);
74  * ?>
75  * @endcode
76  *
77  * @param $params Array with keys:
78  * - src: The image file
79  * - widths: Comma separated, sorted list of width values to be used in the srcset attribute
80  * - type: Indicates how width values should be used (optional, default: w)
81  * - w: Values will be used as pixels, e.g. widths="1600,960" results in srcset="... 1600w, ... 960w"
82  * - x: Values will be used as pixel ration, e.g. widths="1600,960" results in srcset="... 2x, ... 1x"
83  * - sizes: Media queries to define image size in relation of the viewport (optional)
84  * - useDataAttributes: Boolean indicating whether to replace src, srcset, sizes by data-src, data-srcset, data-sizes (optional, default: __false__)
85  * - alt: Alternative text (optional)
86  * - class: Image class (optional)
87  * - title: Image title (optional
88  * - data: Associative array of key/value pairs to be used as data attributes
89  * - width: Width in pixels to output for the width attribute, the height attribute will be calculated according to the aspect ration (optional)
90  * - default: The default file, if src does not exist (optional)
91  * - generate: Boolean indicating whether to generate the images or not (optional, default: __false__)
92  * @param $template Smarty_Internal_Template
93  * @return String
94  */
95 function smarty_function_image($params, Smarty_Internal_Template $template) {
96  $file = $params['src'];
97  $default = isset($params['default']) ? $params['default'] : '';
98  $widths = array_map('trim', isset($params['widths']) ? explode(',', $params['widths']) : []);
99  $type = isset($params['type']) ? $params['type'] : 'w';
100  $sizes = isset($params['sizes']) ? $params['sizes'] : '';
101  $useDataAttributes = isset($params['useDataAttributes']) ? $params['useDataAttributes'] : false;
102  $generate = isset($params['generate']) ? $params['generate'] : false;
103  $alt = isset($params['alt']) ? $params['alt'] : '';
104  $class = isset($params['class']) ? $params['class'] : '';
105  $title = isset($params['title']) ? $params['title'] : '';
106  $data = isset($params['data']) && is_array($params['data']) ? $params['data'] : [];
107  $width = isset($params['width']) ? $params['width'] : null;
108 
109  return ImageUtil::getImageTag($file, $widths, $type, $sizes,
110  $useDataAttributes, $alt, $class, $title, $data, $width, $default, $generate);
111 }
112 ?>
ImageUtil provides support for image handling.
Definition: ImageUtil.php:29