Cheetah
ChWsbImageResize.php
Go to the documentation of this file.
1 <?php
2 
12 {
13  $sClass = trim($sClass, '\\');
14  if (0 === strncmp('Intervention', $sClass, 12)) {
15  $sFile = CH_DIRECTORY_PATH_PLUGINS . 'intervention-image/' . str_replace('\\', '/', $sClass) . '.php';
16  if (file_exists($sFile))
17  require_once($sFile);
18  }
19 }
20 spl_autoload_register('ch_intervention_image_autoload');
21 
22 define('IMAGE_ERROR_SUCCESS', 0);
23 define('IMAGE_ERROR_WRONG_TYPE', 2);
24 
25 if (!defined('CH_WSB_FILE_RIGHTS'))
26  define('CH_WSB_FILE_RIGHTS', 0666);
27 
29 {
30  protected $w = 64, $h = 64;
31  protected $_isAutoCrop = false;
32  protected $_iJpegQuality = 90;
33  protected $_isSquareResize = false;
34  protected $_isUseGD;
35  protected $_oManager;
36  protected $_sError;
37 
38  function __construct()
39  {
40  if (isset($GLOBALS['chWsbClasses'][get_class($this)]))
41  trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
42 
43  $this->_isUseGD = getParam('enable_gd') == 'on' && extension_loaded('gd') ? true : false;
44 
45  $this->_oManager = new Intervention\Image\ImageManager(array('driver' => $this->_isUseGD ? 'gd' : 'imagick'));
46  }
47 
51  public function __clone()
52  {
53  if (isset($GLOBALS['chWsbClasses'][get_class($this)]))
54  trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
55  }
56 
60  public static function instance()
61  {
62  if(!isset($GLOBALS['chWsbClasses'][__CLASS__]))
63  $GLOBALS['chWsbClasses'][__CLASS__] = new ChWsbImageResize();
64 
65  return $GLOBALS['chWsbClasses'][__CLASS__];
66  }
67 
68  function getManager ()
69  {
70  return $this->_oManager;
71  }
72 
73  function getError ()
74  {
75  return $this->_sError;
76  }
77 
78  function isAllowedImage ($sSrcImage)
79  {
80  try {
81  $this->_oManager->make($sSrcImage);
82  }
83  catch (Exception $e) {
84  return false;
85  }
86 
87  return true;
88  }
89 
90  function resize ($mixedImage, $sDstImage = '')
91  {
92  if (is_array($mixedImage)) {
93  $aRet = array();
94  foreach ($mixedImage as $s) {
95  $aRet[] = $this->_resize ($s, $s);
96  }
97  return $aRet;
98  } else {
99  return $this->_resize ($mixedImage, $sDstImage);
100  }
101  }
102 
103  function applyWatermark ($mixedImage, $sDstImage, $sWtrImage, $iTransparency, $sPosition = 'bottom-right', $sPositionOffsetX = 0, $sPositionOffsetY = 0, $sScaleFactor = 0.2 )
104  {
105  if (is_array($mixedImage)) {
106  $aRet = array();
107  foreach ($mixedImage as $s)
108  $aRet[] = $this->_applyWatermark ($s, $s, $sWtrImage, $iTransparency, $sPosition, $sPositionOffsetX, $sPositionOffsetY, $sScaleFactor);
109  return $aRet;
110  } else {
111  return $this->_applyWatermark ($mixedImage, $sDstImage, $sWtrImage, $iTransparency, $sPosition, $sPositionOffsetX, $sPositionOffsetY, $sScaleFactor);
112  }
113  }
114 
115  function grayscale ($mixedImage, $sDstImage = '')
116  {
117  if (is_array($mixedImage)) {
118  $aRet = array();
119  foreach ($mixedImage as $s) {
120  $aRet[] = $this->_grayscale ($s, $s);
121  }
122  return $aRet;
123  } else {
124  return $this->_grayscale ($mixedImage, $sDstImage);
125  }
126  }
127 
128  function setSize ($w, $h)
129  {
130  $this->w = $w;
131  $this->h = $h;
132  }
133 
134  function removeCropOptions ()
135  {
136  $this->_isAutoCrop = false;
137  }
138 
142  function setAutoCrop ($b)
143  {
144  $this->_isAutoCrop = $b;
145  }
146 
147  function setJpegOutput ($b)
148  {
149  // not used anymore
150  }
151 
152  function setJpegQuality ($i)
153  {
154  $this->_iJpegQuality = $i;
155  }
156 
157  function setSquareResize ($b)
158  {
159  $this->_isSquareResize = ($b ? true : false);
160  }
161 
162  function isUsedGD ()
163  {
164  return $this->_isUseGD;
165  }
166 
167  static function getImageSize($sPath)
168  {
169  $o = self::instance();
170  return $o->_getImageSize($sPath);
171  }
172 
173  function _getImageSize($sPath)
174  {
175  $this->_sError = '';
176  try {
177  $o = $this->_oManager->make($sPath);
178  }
179  catch (Exception $e) {
180  $this->_sError = $e->getMessage();
181  return false;
182  }
183  return array ('w' => $o->width(), 'h' => $o->height());
184  }
185 
186  function getExifInfo($sSrcImage, $bCreateLocalFileIfUrl = true)
187  {
188  $this->_sError = '';
189  $sTmpFileName = false;
190  $mixedRet = false;
191 
192  if ($bCreateLocalFileIfUrl && preg_match('/^https?:\/\//', $sSrcImage)) {
193  $sTmpFileName = tempnam(CH_DIRECTORY_PATH_ROOT . 'tmp', '');
194  file_put_contents($sTmpFileName, file_get_contents($sSrcImage));
195  }
196 
197  try {
198  $mixedRet = $this->_oManager
199  ->make($sTmpFileName ? $sTmpFileName : $sSrcImage)
200  ->exif();
201  }
202  catch (Exception $e) {
203  $this->_sError = $e->getMessage();
204  }
205 
206  if ($sTmpFileName)
207  @unlink($sTmpFileName);
208 
209  return $mixedRet;
210  }
211 
212  function getAverageColor($sSrcImage)
213  {
214  $this->_sError = '';
215  try {
216  $a = $this->_oManager
217  ->make($sSrcImage)
218  ->resize(1, 1)
219  ->pickColor(0, 0, 'array');
220 
221  return array('r' => $a[0], 'g' => $a[1], 'b' => $a[2]);
222  }
223  catch (Exception $e) {
224  $this->_sError = $e->getMessage();
225  return false;
226  }
227  }
228 
242  function crop($iScaledWidth, $iScaledHeight, $x, $y, $iCroppedWidth, $iCroppedHeight, $iRotation, $sSrcImage, $sDstImage = '')
243  {
244  $iScaledWidth = round($iScaledWidth);
245  $iScaledHeight = round($iScaledHeight);
246  $this->_sError = '';
247  try {
248  $o = $this->_oManager
249  ->make($sSrcImage)
250  ->resize($iScaledWidth, $iScaledHeight)
251  ->rotate($iRotation);
252 
253  $iImgRotWidth = $o->width();
254  $iImgRotHeight = $o->height();
255  $dx = round(($iImgRotWidth - $iScaledWidth) / 2);
256  $dy = round(($iImgRotHeight - $iScaledHeight) / 2);
257 
258  $o->crop($iScaledWidth, $iScaledHeight, $dx, $dy)
259  ->crop(round($iCroppedWidth), round($iCroppedHeight), $x, $y)
260  ->save($sDstImage ? $sDstImage : $sSrcImage, $this->_iJpegQuality);
261  }
262  catch (Exception $e) {
263  $this->_sError = $e->getMessage();
264  return IMAGE_ERROR_WRONG_TYPE;
265  }
266 
267  return IMAGE_ERROR_SUCCESS;
268  }
269 
270  // private functions are below -------------------------------
271 
272  function _grayscale ($sSrcImage, $sDstImage = '')
273  {
274  $this->_sError = '';
275  try {
276  $this->_oManager
277  ->make($sSrcImage)
278  ->greyscale()
279  ->save($sDstImage ? $sDstImage : $sSrcImage, $this->_iJpegQuality);
280 
281  chmod($sDstImage ? $sDstImage : $sSrcImage, CH_WSB_FILE_RIGHTS);
282  }
283  catch (Exception $e) {
284  $this->_sError = $e->getMessage();
285  return IMAGE_ERROR_WRONG_TYPE;
286  }
287 
288  return IMAGE_ERROR_SUCCESS;
289  }
290 
291  function _resize ($sSrcImage, $sDstImage = '')
292  {
293  $this->_sError = '';
294  try {
295  if ($this->_isAutoCrop || $this->_isSquareResize) {
296  $this->_oManager
297  ->make($sSrcImage)
298  ->orientate()
299  ->fit($this->w, $this->_isSquareResize ? $this->w : $this->h, null, 'top')
300  ->save($sDstImage ? $sDstImage : $sSrcImage, $this->_iJpegQuality);
301  }
302  else {
303  $this->_oManager
304  ->make($sSrcImage)
305  ->orientate()
306  ->resize($this->w, $this->h, function ($constraint) {
307  $constraint->aspectRatio();
308  $constraint->upsize();
309  })
310  ->save($sDstImage ? $sDstImage : $sSrcImage, $this->_iJpegQuality);
311  }
312  chmod($sDstImage ? $sDstImage : $sSrcImage, CH_WSB_FILE_RIGHTS);
313  }
314  catch (Exception $e) {
315  $this->_sError = $e->getMessage();
316  return IMAGE_ERROR_WRONG_TYPE;
317  }
318 
319  return IMAGE_ERROR_SUCCESS;
320  }
321 
322  function _applyWatermark( $sSrcImage, $sDstImage, $sWtrImage, $iTransparency, $sPosition = 'bottom-right', $sPositionOffsetX = 0, $sPositionOffsetY = 0, $sScaleFactor = 0.2)
323  {
324  $this->_sError = '';
325  try {
326  $oImageOrig = $this->_oManager->make($sSrcImage);
327 
328  $oImageOrig
329  ->insert($this->_oManager
330  ->make($sWtrImage)
331  ->resize(round($oImageOrig->width() * $sScaleFactor), round($oImageOrig->height() * $sScaleFactor), function ($constraint) {
332  $constraint->aspectRatio();
333  $constraint->upsize();
334  })
335  ->opacity($iTransparency),
336  $sPosition, $sPositionOffsetX, $sPositionOffsetY)
337  ->save($sDstImage ? $sDstImage : $sSrcImage, $this->_iJpegQuality);
338 
339  chmod($sDstImage ? $sDstImage : $sSrcImage, CH_WSB_FILE_RIGHTS);
340  }
341  catch (Exception $e) {
342  $this->_sError = $e->getMessage();
343  return IMAGE_ERROR_WRONG_TYPE;
344  }
345 
346  return IMAGE_ERROR_SUCCESS;
347  }
348 }
349 
ChWsbImageResize\getAverageColor
getAverageColor($sSrcImage)
Definition: ChWsbImageResize.php:212
ChWsbImageResize\setAutoCrop
setAutoCrop($b)
Definition: ChWsbImageResize.php:142
true
if(!defined("TRUE_VAL")) define("TRUE_VAL" true
Definition: constants.inc.php:8
ChWsbImageResize\applyWatermark
applyWatermark($mixedImage, $sDstImage, $sWtrImage, $iTransparency, $sPosition='bottom-right', $sPositionOffsetX=0, $sPositionOffsetY=0, $sScaleFactor=0.2)
Definition: ChWsbImageResize.php:103
IMAGE_ERROR_WRONG_TYPE
const IMAGE_ERROR_WRONG_TYPE
operation failed, most probably because incorrect image format(or not image file) was provided
Definition: ChWsbImageResize.php:23
ChWsbImageResize\setJpegQuality
setJpegQuality($i)
Definition: ChWsbImageResize.php:152
ChWsbImageResize\getError
getError()
Definition: ChWsbImageResize.php:73
ChWsbImageResize\grayscale
grayscale($mixedImage, $sDstImage='')
Definition: ChWsbImageResize.php:115
ChWsbImageResize\$_iJpegQuality
$_iJpegQuality
jpeg quality
Definition: ChWsbImageResize.php:32
ChWsbImageResize\$_isAutoCrop
$_isAutoCrop
Definition: ChWsbImageResize.php:31
ChWsbImageResize\$_isSquareResize
$_isSquareResize
use smart resize, destination image will be exact Width x Height size
Definition: ChWsbImageResize.php:33
php
ChWsbImageResize\$_sError
$_sError
Intervention Image Manager error string.
Definition: ChWsbImageResize.php:36
ChWsbImageResize\isAllowedImage
isAllowedImage($sSrcImage)
Definition: ChWsbImageResize.php:78
ChWsbImageResize\setJpegOutput
setJpegOutput($b)
Definition: ChWsbImageResize.php:147
ChWsbImageResize\$_isUseGD
$_isUseGD
use GD library or command line ImagMagic utilites
Definition: ChWsbImageResize.php:34
save
save($sSavedId, $sFilePath, $sTitle)
Definition: customFunctions.inc.php:18
ChWsbImageResize\_resize
_resize($sSrcImage, $sDstImage='')
Definition: ChWsbImageResize.php:291
ChWsbImageResize\_grayscale
_grayscale($sSrcImage, $sDstImage='')
Definition: ChWsbImageResize.php:272
ChWsbImageResize\$h
$h
size of destination image
Definition: ChWsbImageResize.php:30
$sFile
$sFile
Definition: index.php:20
ChWsbImageResize\resize
resize($mixedImage, $sDstImage='')
Definition: ChWsbImageResize.php:90
ChWsbImageResize\instance
static instance()
Definition: ChWsbImageResize.php:60
ch_intervention_image_autoload
ch_intervention_image_autoload($sClass)
Definition: ChWsbImageResize.php:11
getParam
getParam($sParamName, $bUseCache=true)
Definition: db.inc.php:130
ChWsbImageResize\_getImageSize
_getImageSize($sPath)
Definition: ChWsbImageResize.php:173
ChWsbImageResize\$w
$w
Definition: ChWsbImageResize.php:30
ChWsbImageResize\__clone
__clone()
Definition: ChWsbImageResize.php:51
Intervention\Image\ImageManager
Definition: ImageManager.php:8
ChWsbImageResize\getManager
getManager()
Definition: ChWsbImageResize.php:68
ChWsbImageResize\__construct
__construct()
Definition: ChWsbImageResize.php:38
ChWsbImageResize\_applyWatermark
_applyWatermark( $sSrcImage, $sDstImage, $sWtrImage, $iTransparency, $sPosition='bottom-right', $sPositionOffsetX=0, $sPositionOffsetY=0, $sScaleFactor=0.2)
Definition: ChWsbImageResize.php:322
ChWsbImageResize\$_oManager
$_oManager
Intervention Image Manager.
Definition: ChWsbImageResize.php:35
ChWsbImageResize\crop
crop($iScaledWidth, $iScaledHeight, $x, $y, $iCroppedWidth, $iCroppedHeight, $iRotation, $sSrcImage, $sDstImage='')
Definition: ChWsbImageResize.php:242
$s
$s
Definition: embed.php:13
ChWsbImageResize\getExifInfo
getExifInfo($sSrcImage, $bCreateLocalFileIfUrl=true)
Definition: ChWsbImageResize.php:186
IMAGE_ERROR_SUCCESS
const IMAGE_ERROR_SUCCESS
operation was successfull
Definition: ChWsbImageResize.php:22
ChWsbImageResize\removeCropOptions
removeCropOptions()
Definition: ChWsbImageResize.php:134
ChWsbImageResize\isUsedGD
isUsedGD()
Definition: ChWsbImageResize.php:162
ChWsbImageResize\setSquareResize
setSquareResize($b)
Definition: ChWsbImageResize.php:157
ChWsbImageResize
Definition: ChWsbImageResize.php:29
$o
$o
Definition: cmd.php:193
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChWsbImageResize\getImageSize
static getImageSize($sPath)
Definition: ChWsbImageResize.php:167
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10
ChWsbImageResize\setSize
setSize($w, $h)
Definition: ChWsbImageResize.php:128