BxDolImageResize.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. // Function error codes
  10. define( 'IMAGE_ERROR_SUCCESS', 0 );
  11. define( 'IMAGE_ERROR_SOURCE_NOT_EXISTS', 1 );
  12. define( 'IMAGE_ERROR_WRONG_TYPE', 2 );
  13. define( 'IMAGE_ERROR_FILE_OPEN_FAILED', 3 );
  14. define( 'IMAGE_ERROR_IMAGEMAGICK_ERROR', 4 );
  15. define( 'IMAGE_ERROR_GD_NOT_INSTALLED', 5 );
  16. define( 'IMAGE_ERROR_GD_TYPE_NOT_SUPPORTED', 6 );
  17. define( 'IMAGE_ERROR_GD_OPEN_FAILED', 7 );
  18. define( 'IMAGE_ERROR_GD_RESIZE_ERROR', 8 );
  19. define( 'IMAGE_ERROR_GD_MERGE_ERROR', 9 );
  20. define( 'IMAGE_ERROR_GD_WRITE_FAILED', 10 );
  21. define( 'IMAGE_ERROR_GD_TTF_NOT_SUPPORTED', 11 );
  22. define( 'IMAGE_ERROR_GD_FILTER_ERROR', 12 );
  23. // Image types for GD
  24. // NOTE: actually these constants exist in PHP >= 4.3.0, but they are included for
  25. // back compatibility
  26. define( 'IMAGE_TYPE_GIF', 1 );
  27. define( 'IMAGE_TYPE_JPG', 2 );
  28. define( 'IMAGE_TYPE_PNG', 3 );
  29. class BxDolImageResize extends BxDol implements iBxDolSingleton
  30. {
  31. protected $w = 64, $h = 64; ///< size of destination image
  32. protected $_isCrop = false;
  33. protected $_isAutoCrop = false;
  34. protected $_cropX, $_cropY, $_cropW, $_cropH;
  35. protected $_iForceOutputType = false; ///< force IMAGE_TYPE_PNG, IMAGE_TYPE_PNG, IMAGE_TYPE_GIF or false to keep the original format // TODO: check if this setting works when ImageMagick is used
  36. protected $_iJpegQuality = 90; ///< jpeg quality
  37. protected $_isSquareResize = false; ///< use smart resize, destination image will be exact Width x Height size
  38. protected $_isUseGD; ///< use GD library or command line ImagMagic utilites
  39. function __construct()
  40. {
  41. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  42. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  43. parent::__construct();
  44. $this->_isUseGD = getParam('enable_gd') == 'on' && extension_loaded('gd') ? true : false;
  45. }
  46. /**
  47. * Prevent cloning the instance
  48. */
  49. public function __clone()
  50. {
  51. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  52. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  53. }
  54. /**
  55. * Get singleton instance of the class
  56. */
  57. public static function getInstance()
  58. {
  59. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  60. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolImageResize();
  61. return $GLOBALS['bxDolClasses'][__CLASS__];
  62. }
  63. function isAllowedImage ($sPath)
  64. {
  65. if ($this->_isUseGD) {
  66. $aImage = getimagesize($sPath);
  67. if (!empty($aImage) && in_array($aImage[2], array(1, 2, 3)))
  68. return true;
  69. } else {
  70. // TODO: image checking using image magick
  71. }
  72. return false;
  73. }
  74. function resize ($mixedImage, $sDstImage = '')
  75. {
  76. if (is_array($mixedImage)) {
  77. $aRet = array();
  78. foreach ($mixedImage as $s) {
  79. $aRet[] = $this->_resize ($s, $s);
  80. }
  81. return $aRet;
  82. } else {
  83. return $this->_resize ($mixedImage, $sDstImage);
  84. }
  85. }
  86. function applyWatermark ($mixedImage, $sDstImage, $wtrFilename, $wtrTransparency)
  87. {
  88. if (is_array($mixedImage)) {
  89. $aRet = array();
  90. foreach ($mixedImage as $s) {
  91. $aRet[] = $this->_applyWatermark ($s, $s, $wtrFilename, $wtrTransparency);
  92. }
  93. return $aRet;
  94. } else {
  95. return $this->_applyWatermark ($mixedImage, $sDstImage, $wtrFilename, $wtrTransparency);
  96. }
  97. }
  98. function grayscale ($mixedImage, $sDstImage = '')
  99. {
  100. if (is_array($mixedImage)) {
  101. $aRet = array();
  102. foreach ($mixedImage as $s) {
  103. $aRet[] = $this->_grayscale ($s, $s);
  104. }
  105. return $aRet;
  106. } else {
  107. return $this->_grayscale ($mixedImage, $sDstImage);
  108. }
  109. }
  110. function setSize ($w, $h)
  111. {
  112. $this->w = $w;
  113. $this->h = $h;
  114. }
  115. function removeCropOptions ()
  116. {
  117. $this->_isCrop = false;
  118. $this->_isAutoCrop = false;
  119. }
  120. function setCropOptions ($x, $y, $w, $h)
  121. {
  122. $this->_isCrop = true;
  123. $this->_cropX = $x;
  124. $this->_cropY = $y;
  125. $this->_cropW = $w;
  126. $this->_cropH = $h;
  127. }
  128. /**
  129. * Crop image to destination size with filling whole area of destination size
  130. */
  131. function setAutoCrop ($b)
  132. {
  133. $this->_isAutoCrop = $b;
  134. }
  135. function setJpegOutput ($b)
  136. {
  137. $this->setOutputType($b ? IMAGE_TYPE_JPG : false);
  138. }
  139. function setOutputType ($iType)
  140. {
  141. $this->_iForceOutputType = $iType;;
  142. }
  143. function setJpegQuality ($i)
  144. {
  145. $this->_iJpegQuality = $i;
  146. }
  147. function setSquareResize ($b)
  148. {
  149. $this->_isSquareResize = ($b ? true : false);
  150. }
  151. function isUsedGD ()
  152. {
  153. return $this->_isUseGD;
  154. }
  155. static function getImageSize($sPath)
  156. {
  157. $aSize = getimagesize($sPath);
  158. return array ('w' => $aSize[0], 'h' => $aSize[1]);
  159. }
  160. function getAverageColor($sPath)
  161. {
  162. return $this->isUsedGD() ? $this->_getAverageColorGD($sPath) : $this->_getAverageColorImageMagic($sPath);
  163. }
  164. // private functions are below -------------------------------
  165. function _grayscale ($sSrcImage, $sDstImage = '')
  166. {
  167. if (!file_exists($sSrcImage))
  168. return IMAGE_ERROR_SOURCE_NOT_EXISTS;
  169. if (!$sDstImage)
  170. $sDstImage = $sSrcImage;
  171. if ($sDstImage == $sSrcImage) {
  172. chmod($sDstImage, 0666);
  173. }
  174. return $this->_isUseGD ? $this->_grayscaleGD ($sSrcImage, $sDstImage) : $this->_grayscaleImageMagick ($sSrcImage, $sDstImage);
  175. }
  176. function _grayscaleGD ($sSrcImage, $sDstImage)
  177. {
  178. $iErr = 0;
  179. $src_im =& $this->_createGDImage($sSrcImage, $size, $iErr);
  180. if ($iErr)
  181. return $iErr;
  182. if (!$src_im)
  183. return IMAGE_ERROR_GD_OPEN_FAILED;
  184. if (!imagefilter($src_im, IMG_FILTER_GRAYSCALE))
  185. return IMAGE_ERROR_GD_FILTER_ERROR;
  186. $writeResult = $this->_writeImageGD ($src_im, $sDstImage, $size[2]);
  187. // free memory
  188. imagedestroy( $src_im );
  189. if ( $writeResult && file_exists($sDstImage) )
  190. return IMAGE_ERROR_SUCCESS;
  191. return IMAGE_ERROR_GD_WRITE_FAILED;
  192. }
  193. function _grayscaleImageMagick ($sSrcImage, $sDstImage)
  194. {
  195. $cmd = "{$GLOBALS['CONVERT']} \"$sSrcImage\" -type Grayscale -quantize Gray \"$sDstImage\"";
  196. @exec( $cmd );
  197. if ( file_exists($sDstImage) )
  198. return IMAGE_ERROR_SUCCESS;
  199. return IMAGE_ERROR_IMAGEMAGICK_ERROR;
  200. }
  201. function _resize ($sSrcImage, $sDstImage = '')
  202. {
  203. if (!file_exists($sSrcImage))
  204. return IMAGE_ERROR_SOURCE_NOT_EXISTS;
  205. if (!$sDstImage)
  206. $sDstImage = $sSrcImage;
  207. if ($sDstImage == $sSrcImage) {
  208. chmod($sDstImage, 0666);
  209. }
  210. return $this->_isUseGD ? $this->_resizeGD ($sSrcImage, $sDstImage) : $this->_resizeImageMagick ($sSrcImage, $sDstImage);
  211. }
  212. function _resizeGD ($sSrcImage, $sDstImage)
  213. {
  214. $iErr = 0;
  215. $src_im =& $this->_createGDImage($sSrcImage, $size, $iErr);
  216. $sizeOrig = $size;
  217. if ($iErr)
  218. return $iErr;
  219. if (!$src_im)
  220. return IMAGE_ERROR_GD_OPEN_FAILED;
  221. $xd = $yd = 0;
  222. $xs = $ys = 0;
  223. if ($this->_isAutoCrop) {
  224. $sourceRatio = (float) ($size[0] / $size[1]);
  225. $destRatio = (float) ($this->w / $this->h);
  226. if ( $sourceRatio > $destRatio )
  227. $resizeRatio = (float) ($this->h / $size[1]);
  228. else
  229. $resizeRatio = (float) ($this->w / $size[0]);
  230. $destW = (int) ($resizeRatio * $size[0]);
  231. $destH = (int) ($resizeRatio * $size[1]);
  232. if ($destW > $this->w)
  233. $this->setCropOptions (floor(($size[0] - $this->w/$resizeRatio)/2.0), 0, floor($this->w/$resizeRatio), $size[1]);
  234. elseif ($destH > $this->h)
  235. $this->setCropOptions (0, floor(($size[1] - $this->h/$resizeRatio)/2.0), $size[0], floor($this->h/$resizeRatio));
  236. }
  237. if ($this->_isCrop) {
  238. $size[0] = $this->_cropW;
  239. $size[1] = $this->_cropH;
  240. $xs = $this->_cropX;
  241. $ys = $this->_cropY;
  242. $destW = $this->w;
  243. $destH = $this->h;
  244. } elseif ($this->_isSquareResize) {
  245. $destW = $this->w;
  246. $destH = $this->h;
  247. if ($size[0] < $size[1]) {
  248. $d = ($size[1] - $size[0])/2;
  249. $size[1] = $size[0];
  250. $xs = 0;
  251. $ys = (int)$d;
  252. } else {
  253. $d = ($size[0] - $size[1])/2;
  254. $size[0] = $size[1];
  255. $xs = (int)$d;
  256. $ys = 0;
  257. }
  258. } else {
  259. // determ destination size
  260. $sourceRatio = (float) ($size[0] / $size[1]);
  261. $destRatio = (float) ($this->w / $this->h);
  262. if ( $sourceRatio > $destRatio )
  263. $resizeRatio = (float) ($this->w / $size[0]);
  264. else
  265. $resizeRatio = (float) ($this->h / $size[1]);
  266. $destW = (int) ($resizeRatio * $size[0]);
  267. $destH = (int) ($resizeRatio * $size[1]);
  268. }
  269. // this is more qualitative function, but it doesn't exist in old GD and doesn't support GIF format
  270. if ( function_exists( 'imagecreatetruecolor' ) && $size[2] != IMAGE_TYPE_GIF ) {
  271. // resize only if size is larger than needed
  272. if ( $this->_isCrop || $sizeOrig[0] > $this->w || $sizeOrig[1] > $this->h ) {
  273. $dst_im = imagecreatetruecolor( $destW, $destH );
  274. imagecolortransparent($dst_im, imagecolorallocate($dst_im, 0, 0, 0));
  275. imagealphablending($dst_im, false);
  276. imagesavealpha($dst_im, true);
  277. $convertResult = imagecopyresampled( $dst_im, $src_im, $xd, $yd, $xs, $ys,
  278. $destW, $destH, $size[0], $size[1] );
  279. } else {
  280. $dst_im = $src_im;
  281. $convertResult = true;
  282. }
  283. } else { // this is for old GD versions and for GIF images
  284. // resize only if size is larger than needed
  285. if ( $size[0] > $this->w || $size[1] > $this->h ) {
  286. $dst_im = imagecreate( $destW, $destH );
  287. if ($size[2] == IMAGE_TYPE_GIF) {
  288. ImageColorTransparent( $dst_im, imagecolorallocate($dst_im, 0, 0, 0) );
  289. imagealphablending( $dst_im, false );
  290. }
  291. $convertResult = imagecopyresized( $dst_im, $src_im, $xd, $yd, $xs, $ys,
  292. $destW, $destH, $size[0], $size[1] );
  293. } else {
  294. $dst_im = $src_im;
  295. $convertResult = true;
  296. }
  297. }
  298. if ( !$convertResult )
  299. return IMAGE_ERROR_GD_RESIZE_ERROR;
  300. $writeResult = $this->_writeImageGD ($dst_im, $sDstImage, $size[2]);
  301. // free memory
  302. if ( $dst_im != $src_im ) {
  303. imagedestroy( $src_im );
  304. imagedestroy( $dst_im );
  305. } else {
  306. imagedestroy( $src_im );
  307. }
  308. if ( $writeResult && file_exists($sDstImage) )
  309. return IMAGE_ERROR_SUCCESS;
  310. return IMAGE_ERROR_GD_WRITE_FAILED;
  311. }
  312. function _resizeImageMagick ($sSrcImage, $sDstImage)
  313. {
  314. // TODO: $this->_isCrop and $this->_isSquareResize
  315. if ( $sSrcImage == $sDstImage ) {
  316. $cmd = "{$GLOBALS['MOGRIFY']} -geometry {$this->w}\">\"x{$this->h}\">\" \"$sSrcImage\"";
  317. @exec( $cmd );
  318. $nameWithoutExt = substr( $sSrcImage, 0, strrpos($sSrcImage, '.') );
  319. if ( file_exists( "{$nameWithoutExt}.mgk" ) )
  320. rename( "{$nameWithoutExt}.mgk", $sSrcImage );
  321. } else {
  322. $cmd = "{$GLOBALS['CONVERT']} \"$sSrcImage\" -geometry {$this->w}\">\"x{$this->h}\">\" \"$sDstImage\"";
  323. @exec( $cmd );
  324. }
  325. if ( file_exists($sDstImage) )
  326. return IMAGE_ERROR_SUCCESS;
  327. return IMAGE_ERROR_IMAGEMAGICK_ERROR;
  328. }
  329. function _applyWatermark( $srcFilename, $dstFilename, $wtrFilename, $wtrTransparency )
  330. {
  331. // TODO: watermard are not applied correctly, becuase images of different suzes are used, FIX IT
  332. // input validation
  333. $wtrTransparency = (int) $wtrTransparency;
  334. if ( $wtrTransparency > 100 )
  335. $wtrTransparency = 100;
  336. if ( !file_exists( $srcFilename ) )
  337. return IMAGE_ERROR_SOURCE_NOT_EXISTS;
  338. if ( !file_exists( $wtrFilename ) )
  339. return IMAGE_ERROR_SOURCE_NOT_EXISTS;
  340. // if destination and source filenames are equivalent then change mode for destination
  341. if ( $srcFilename == $dstFilename )
  342. chmod( $dstFilename, 0666 );
  343. if ( $this->_isUseGD ) {
  344. return $this->_applyWatermarkGD($srcFilename, $dstFilename, $wtrFilename, $wtrTransparency);
  345. } else {
  346. return $this->_applyWatermarkImageMagick($srcFilename, $dstFilename, $wtrFilename, $wtrTransparency);
  347. }
  348. }
  349. function _applyWatermarkGD($srcFilename, $dstFilename, $wtrFilename, $wtrTransparency)
  350. {
  351. $iErr = 0;
  352. $src_im =& $this->_createGDImage($srcFilename, $size, $iErr);
  353. if ($iErr)
  354. return $iErr;
  355. if (!$src_im)
  356. return IMAGE_ERROR_GD_OPEN_FAILED;
  357. $wtr_im =& $this->_createGDImage($wtrFilename, $wtrSize, $iErr);
  358. if ($iErr)
  359. return $iErr;
  360. if (!$wtr_im )
  361. return IMAGE_ERROR_GD_OPEN_FAILED;
  362. if (function_exists('imagecreatetruecolor'))
  363. $dst_im = imagecreatetruecolor( $size[0], $size[1] );
  364. else
  365. $dst_im = imagecreate( $size[0], $size[1] );
  366. $watermarkX = ($size[0] - $wtrSize[0]) / 2;
  367. $watermarkY = $size[1] - $wtrSize[1];
  368. $copyResult = imagecopy( $dst_im, $src_im, 0, 0, 0, 0, $size[0], $size[1]);
  369. if (!$copyResult)
  370. return IMAGE_ERROR_GD_MERGE_ERROR;
  371. imagealphablending($wtr_im, false);
  372. imagesavealpha($wtr_im, true);
  373. if (IMAGE_TYPE_PNG == $wtrSize[2] && imageistruecolor($wtr_im))
  374. $mergeResult = imagecopy ( $dst_im, $wtr_im, $watermarkX, $watermarkY, 0, 0, $wtrSize[0], $wtrSize[1]);
  375. else
  376. $mergeResult = imagecopymerge ( $dst_im, $wtr_im, $watermarkX, $watermarkY, 0, 0, $wtrSize[0], $wtrSize[1], $wtrTransparency );
  377. if ( !$mergeResult )
  378. return IMAGE_ERROR_GD_MERGE_ERROR;
  379. switch ( $size[2] ) {
  380. case IMAGE_TYPE_GIF:
  381. $writeResult = imagegif( $dst_im, $dstFilename );
  382. break;
  383. case IMAGE_TYPE_JPG:
  384. $writeResult = imagejpeg( $dst_im, $dstFilename, $this->_iJpegQuality);
  385. break;
  386. case IMAGE_TYPE_PNG:
  387. $writeResult = imagepng( $dst_im, $dstFilename );
  388. break;
  389. }
  390. // free memory
  391. imagedestroy( $dst_im );
  392. imagedestroy( $src_im );
  393. imagedestroy( $wtr_im );
  394. if ( $writeResult )
  395. return IMAGE_ERROR_SUCCESS;
  396. else
  397. return IMAGE_ERROR_GD_WRITE_FAILED;
  398. }
  399. function _applyWatermarkImageMagick($srcFilename, $dstFilename, $wtrFilename, $wtrTransparency)
  400. {
  401. $imgTransparency = 100 - $wtrTransparency;
  402. $cmd = "{$GLOBALS['COMPOSITE']} -gravity \"South\" -dissolve \"$imgTransparency\" \"$wtrFilename\" -dissolve $wtrTransparency \"$srcFilename\" \"$dstFilename\"";
  403. @exec( $cmd );
  404. if ( file_exists($dstFilename) )
  405. return IMAGE_ERROR_SUCCESS;
  406. return IMAGE_ERROR_IMAGEMAGICK_ERROR;
  407. }
  408. function & _createGDImage($s, &$aSize, &$iErr) {
  409. if (!isset($this->gdInfoArray))
  410. $this->gdInfoArray = gd_info();
  411. $aSize = getimagesize($s);
  412. // only GIF, JPG and PNG allowed
  413. switch ( $aSize[2] ) {
  414. case IMAGE_TYPE_GIF:
  415. if (!isset($this->gdInfoArray['GIF Read Support']) || !$this->gdInfoArray['GIF Read Support'] || !isset($this->gdInfoArray['GIF Create Support']) || !$this->gdInfoArray['GIF Create Support'])
  416. return ($iErr = IMAGE_ERROR_GD_TYPE_NOT_SUPPORTED);
  417. $r = imagecreatefromgif( $s );
  418. return $r;
  419. case IMAGE_TYPE_JPG:
  420. if ((!isset($this->gdInfoArray['JPG Support']) || !$this->gdInfoArray['JPG Support']) && (!isset($this->gdInfoArray['JPEG Support']) || !$this->gdInfoArray['JPEG Support']))
  421. return ($iErr = IMAGE_ERROR_GD_TYPE_NOT_SUPPORTED);
  422. $r = imagecreatefromjpeg( $s );
  423. return $r;
  424. case IMAGE_TYPE_PNG:
  425. if (!isset($this->gdInfoArray['PNG Support']) || !$this->gdInfoArray['PNG Support'])
  426. return ($iErr = IMAGE_ERROR_GD_TYPE_NOT_SUPPORTED);
  427. $r = imagecreatefrompng( $s );
  428. imagesavealpha($r, true);
  429. return $r;
  430. default:
  431. return ($iErr = IMAGE_ERROR_WRONG_TYPE);
  432. }
  433. }
  434. function _writeImageGD ($src_im, $sDstImage, $iSrcImageType)
  435. {
  436. $writeResult = false;
  437. switch ($this->_iForceOutputType){
  438. case IMAGE_TYPE_JPG:
  439. $writeResult = imagejpeg( $src_im, $sDstImage, $this->_iJpegQuality);
  440. break;
  441. case IMAGE_TYPE_PNG:
  442. $writeResult = imagepng( $src_im, $sDstImage);
  443. break;
  444. case IMAGE_TYPE_GIF:
  445. $writeResult = imagegif( $src_im, $sDstImage );
  446. break;
  447. default:
  448. switch ($iSrcImageType) {
  449. case IMAGE_TYPE_GIF:
  450. $writeResult = imagegif( $src_im, $sDstImage );
  451. break;
  452. case IMAGE_TYPE_PNG:
  453. $writeResult = imagepng( $src_im, $sDstImage );
  454. break;
  455. case IMAGE_TYPE_JPG:
  456. default:
  457. $writeResult = imagejpeg( $src_im, $sDstImage, $this->_iJpegQuality);
  458. break;
  459. }
  460. }
  461. return $writeResult;
  462. }
  463. function _getAverageColorGD($sPath)
  464. {
  465. $iError = 0;
  466. $aSize = array();
  467. $oImgOrig = $this->_createGDImage($sPath, $aSize, $iError);
  468. if($iError != IMAGE_ERROR_SUCCESS)
  469. return false;
  470. $oImgTmp = ImageCreateTrueColor(1,1);
  471. ImageCopyResampled($oImgTmp, $oImgOrig, 0, 0, 0, 0, 1, 1, $aSize[0],$aSize[1]);
  472. $iRgb = ImageColorAt($oImgTmp, 0, 0);
  473. $iRed = ($iRgb >> 16) & 0xFF;
  474. $iGreen = ($iRgb >> 8) & 0xFF;
  475. $iBlue = $iRgb & 0xFF;
  476. return array('r' => $iRed, 'g' => $iGreen, 'b' => $iBlue);
  477. }
  478. function _getAverageColorImageMagic($sPath)
  479. {
  480. $sCmd = "{$GLOBALS['CONVERT']} $sPath -filter box -resize 1x1! -format \"%[fx:round(255*u.r)],%[fx:round(255*u.g)],%[fx:round(255*u.b)]\" info:";
  481. $sResult = @exec($sCmd);
  482. if(empty($sResult))
  483. return false;
  484. $aResult = explode(',', $sResult);
  485. if(count($aResult) != 3)
  486. return false;
  487. return array('r' => $aResult[0], 'g' => $aResult[1], 'b' => $aResult[2]);
  488. }
  489. }
  490. /** @} */