wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
function.image.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: function.image.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/util/class.GraphicsUtil.php");
20 require_once(BASE."wcmf/lib/util/class.URIUtil.php");
21 
22 /*
23 * Smarty plugin
24 * -------------------------------------------------------------
25 * File: function.image.php
26 * Type: function
27 * Name: image
28 * Purpose: Renders an image tag, if the 'src' value points to an image file or the 'default' parameter is
29 * given. If 'width' or 'height' are given, the image will be resized to that values. The resize method
30 * depends on the 'sizemode' parameter. If the image size will be physically changed, a copy will be created
31 * in the cache directory that is used by the View class, which means that invalidating the View
32 * cache invalidates the image cache too. The content of the 'params' parameter will be put as is in the
33 * created image tag. If the image url has to be translated, use the 'base' parameter (see
34 * smarty_function_translate_url).
35 * The 'sizemode' parameter can have one of the followig values:
36 * - resize: The browser scales the image to fit inside the given dimensions
37 * - resample: The image will be physically scaled to fit inside the given dimensions
38 * - crop: The image will be clipped from the middle to fit inside the given dimensions
39 * If the parameter is not given, it defaults to resample.
40 * The 'valueMode' parameter can have one of the following values:
41 * - fitInto: The image is resized if it's width or height exceeds one of the given values. Image proportions will be kept.
42 * - scaleTo: The image is resized if it's width or height differs from the given values. Image proportions will be kept.
43 * - default: The image is resized if it's width or height differs from the given values. Image proportions will be ignored.
44 * The image will be resized according to the given sizemode. The image won't be cropped with 'valuemode' set to anything else than default.
45 * If the parameter is not given, default is used. Size attributes may be skipped using the nosizeoutput parameter.
46 * Usage: {image src=$image->getFile() base="cms/application/" width="100" alt="Image 1" params='border="0"'
47 * default="images/blank.gif" sizemode="resize" nosizeoutput=true}
48 * -------------------------------------------------------------
49 */
50 function smarty_function_image($params, &$smarty)
51 {
52  $file = $params['src'];
53  $base = $params['base'];
54  $default = $params['default'];
55  $sizemode = $params['sizemode'];
56  $valuemode = $params['valuemode'];
57 
58  if (strlen($file) == 0 && strlen($default) == 0) {
59  return;
60  }
61 
62  // translate the file url using base
63  if (isset($params['base']))
64  {
65  // translate file url
66  $urls = URIUtil::translate($file, $base);
67  $file = $urls['relative'];
68  // translate default file url
69  $urls = URIUtil::translate($default, $base);
70  $default = $urls['relative'];
71  }
72 
73  // check if the file exists
74  if (!is_file($file))
75  {
76  // try the default
77  $file = $default;
78  if (!is_file($file)) {
79  return;
80  }
81  }
82 
83  // get the image size in order to see if we have to resize
84  $imageSize = getimagesize($file);
85  if ($imageSize == false)
86  {
87  // the file is no image
88  return;
89  }
90 
91  $requestedWidth = isset($params['width']) ? $params['width']: null;
92  $requestedHeight = isset($params['height']) ? $params['height']: null;
93 
94  // calculate new dimensions if value mode is set
95  if ($valuemode == 'scaleTo' || $valuemode == 'fitInto')
96  {
97  if ($valuemode == 'fitInto' && $requestedHeight && $requestedHeight > $imageSize[1])
98  {
99  // if image should fit into a rectangle and it's height is smaller than the requested, leave image untouched
100  $requestedHeight = $imageSize[1];
101  }
102  else if ($valuemode == 'fitInto' && $requestedWidth && $requestedWidth > $imageSize[0])
103  {
104  // if image should fit into a rectangle and it's width is smaller than the requested, leave image untouched
105  $requestedWidth = $imageSize[0];
106  }
107  if ($requestedHeight == null)
108  {
109  // calculate height if only width is given
110  $requestedHeight = floor(($imageSize[1] * $requestedWidth) / $imageSize[0]);
111  }
112  else if ($requestedWidth == null)
113  {
114  // calculate width if only height is given
115  $requestedWidth = floor(($imageSize[0] * $requestedHeight) / $imageSize[1]);
116  }
117  else
118  {
119  // calculate either width or height depending on the ratio
120  $requestedAspectRatio = $requestedHeight / $requestedWidth;
121  $imageAspectRatio = $imageSize[1] / $imageSize[0];
122  if ($requestedAspectRatio >= $imageAspectRatio) {
123  // scale based on width, keep requestedWidth
124  $requestedHeight = ($imageSize[1] * $requestedWidth) / $imageSize[0];
125  }
126  else {
127  // scale based on height, keep requestedHeight
128  $requestedWidth = ($imageSize[0] * $requestedHeight) / $imageSize[1];
129  }
130  }
131  }
132 
133  if (strlen($sizemode) == 0) {
134  $sizemode = 'resample';
135  }
136 
137  // don't resize big images, because of resource limits
138  if (filesize($file) > 1500000) {
139  $sizemode = 'resize';
140  }
141 
142  if (($sizemode != 'resize') && ($requestedWidth != null || $requestedHeight != null) &&
143  ($requestedWidth < $imageSize[0] || $requestedHeight < $imageSize[1]))
144  {
145  // if 'width' or 'height' are given and they differ from the image values,
146  // we have to resize the image
147 
148  // get the file extension
149  preg_match('/\.(\w+)$/', $file, $matches);
150  $extension = $matches[1];
151 
152  $destName = $smarty->cache_dir.md5($file.filectime($file).$requestedWidth.$requestedHeight.$sizemode).'.'.$extension;
153 
154  // if the file does not exist in the cache, we have to create it
155  $dateOrig = fileatime($file);
156  $dateCache = fileatime($destName);
157  if (!file_exists($destName) || $dateOrig > $dateCache)
158  {
159  $graphicsUtil = new GraphicsUtil();
160  if ($sizemode == 'resample') {
161  $graphicsUtil->createThumbnail($file, $destName, $requestedWidth, $requestedHeight);
162  }
163  else {
164  $graphicsUtil->cropImage($file, $destName, $requestedWidth, $requestedHeight);
165  }
166  }
167 
168  // use the cached file
169  if (file_exists($destName)) {
170  $file = $destName;
171  }
172  }
173 
174  $widthStr = "";
175  $heightStr = "";
176  if (!isset($params['nosizeoutput']) || $params['nosizeoutput'] == false) {
177  if ($requestedWidth != null) {
178  $widthStr = ' width="'.$requestedWidth.'"';
179  }
180  if ($requestedHeight != null) {
181  $heightStr = ' height="'.$requestedHeight.'"';
182  }
183  }
184 
185  echo '<img src="'.$file.'"'.$widthStr.$heightStr.' alt="'.$params['alt'].'" '.$params['params'].'>';
186 }
187 ?>
GraphicsUtil provides support for graphic manipulation.
smarty_function_image($params, &$smarty)
translate($rel_uri, $base)