BxDolImageResize.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 DolphinCore Dolphin 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. var $w = 64, $h = 64; ///< size of destination image
  31. var $_isCrop = false;
  32. var $_cropX, $_cropY, $_cropW, $_cropH;
  33. var $_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
  34. var $_iJpegQuality = 90; ///< jpeg quality
  35. var $_isSquareResize = false; ///< use smart resize, destination image will be exact Width x Height size
  36. var $_isUseGD; ///< use GD library or command line ImagMagic utilites
  37. function BxDolImageResize() {
  38. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  39. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  40. parent::BxDol();
  41. $this->_isUseGD = getParam('enable_gd') == 'on' && extension_loaded('gd') ? true : false;
  42. }
  43. /**
  44. * Prevent cloning the instance
  45. */
  46. public function __clone() {
  47. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  48. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  49. }
  50. /**
  51. * Get singleton instance of the class
  52. */
  53. public static function getInstance() {
  54. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  55. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolImageResize();
  56. return $GLOBALS['bxDolClasses'][__CLASS__];
  57. }
  58. function isAllowedImage ($sPath) {
  59. if ($this->_isUseGD) {
  60. $aImage = getimagesize($sPath);
  61. if (!empty($aImage) && in_array($aImage[2], array(1, 2, 3)))
  62. return true;
  63. } else {
  64. // TODO: image checking using image magick
  65. }
  66. return false;
  67. }
  68. function resize ($mixedImage, $sDstImage = '') {
  69. if (is_array($mixedImage)) {
  70. $aRet = array();
  71. foreach ($mixedImage as $s) {
  72. $aRet[] = $this->_resize ($s, $s);
  73. }
  74. return $aRet;
  75. } else {
  76. return $this->_resize ($mixedImage, $sDstImage);
  77. }
  78. }
  79. function applyWatermark ($mixedImage, $sDstImage, $wtrFilename, $wtrTransparency) {
  80. if (is_array($mixedImage)) {
  81. $aRet = array();
  82. foreach ($mixedImage as $s) {
  83. $aRet[] = $this->_applyWatermark ($s, $s, $wtrFilename, $wtrTransparency);
  84. }
  85. return $aRet;
  86. } else {
  87. return $this->_applyWatermark ($mixedImage, $sDstImage, $wtrFilename, $wtrTransparency);
  88. }
  89. }
  90. function grayscale ($mixedImage, $sDstImage = '') {
  91. if (is_array($mixedImage)) {
  92. $aRet = array();
  93. foreach ($mixedImage as $s) {
  94. $aRet[] = $this->_grayscale ($s, $s);
  95. }
  96. return $aRet;
  97. } else {
  98. return $this->_grayscale ($mixedImage, $sDstImage);
  99. }
  100. }
  101. function setSize ($w, $h) {
  102. $this->w = $w;
  103. $this->h = $h;
  104. }
  105. function removeCropOptions () {
  106. $this->_isCrop = false;
  107. }
  108. function setCropOptions ($x, $y, $w, $h) {
  109. $this->_isCrop = true;
  110. $this->_cropX = $x;
  111. $this->_cropY = $y;
  112. $this->_cropW = $w;
  113. $this->_cropH = $h;
  114. }
  115. function setJpegOutput ($b) {
  116. $this->setOutputType($b ? IMAGE_TYPE_JPG : false);
  117. }
  118. function setOutputType ($iType) {
  119. $this->_iForceOutputType = $iType;;
  120. }
  121. function setJpegQuality ($i) {
  122. $this->_iJpegQuality = $i;
  123. }
  124. function setSquareResize ($b) {
  125. $this->_isSquareResize = ($b ? true : false);
  126. }
  127. function isUsedGD () {
  128. return $this->_isUseGD;
  129. }
  130. function getImageSize($sPath) {
  131. $aSize = getimagesize($sPath);
  132. return array ('w' => $aSize[0], 'h' => $aSize[1]);
  133. }
  134. // private functions are below -------------------------------
  135. function _grayscale ($sSrcImage, $sDstImage = '') {
  136. if (!file_exists($sSrcImage))
  137. return IMAGE_ERROR_SOURCE_NOT_EXISTS;
  138. if (!$sDstImage)
  139. $sDstImage = $sSrcImage;
  140. if ($sDstImage == $sSrcImage) {
  141. chmod($sDstImage, 0666);
  142. }
  143. return $this->_isUseGD ? $this->_grayscaleGD ($sSrcImage, $sDstImage) : $this->_grayscaleImageMagick ($sSrcImage, $sDstImage);
  144. }
  145. function _grayscaleGD ($sSrcImage, $sDstImage) {
  146. $iErr = 0;
  147. $src_im =& $this->_createGDImage($sSrcImage, $size, $iErr);
  148. $sizeOrig = $size;
  149. if ($iErr)
  150. return $iErr;
  151. if (!$src_im)
  152. return IMAGE_ERROR_GD_OPEN_FAILED;
  153. if (!imagefilter($src_im, IMG_FILTER_GRAYSCALE))
  154. return IMAGE_ERROR_GD_FILTER_ERROR;
  155. $writeResult = $this->_writeImageGD ($src_im, $sDstImage);
  156. // free memory
  157. imagedestroy( $src_im );
  158. if ( $writeResult && file_exists($sDstImage) )
  159. return IMAGE_ERROR_SUCCESS;
  160. return IMAGE_ERROR_GD_WRITE_FAILED;
  161. }
  162. function _grayscaleImageMagick ($sSrcImage, $sDstImage) {
  163. $cmd = "{$GLOBALS['CONVERT']} \"$sSrcImage\" -type Grayscale -quantize Gray \"$sDstImage\"";
  164. @exec( $cmd );
  165. if ( file_exists($sDstImage) )
  166. return IMAGE_ERROR_SUCCESS;
  167. return IMAGE_ERROR_IMAGEMAGICK_ERROR;
  168. }
  169. function _resize ($sSrcImage, $sDstImage = '') {
  170. if (!file_exists($sSrcImage))
  171. return IMAGE_ERROR_SOURCE_NOT_EXISTS;
  172. if (!$sDstImage)
  173. $sDstImage = $sSrcImage;
  174. if ($sDstImage == $sSrcImage) {
  175. chmod($sDstImage, 0666);
  176. }
  177. return $this->_isUseGD ? $this->_resizeGD ($sSrcImage, $sDstImage) : $this->_resizeImageMagick ($sSrcImage, $sDstImage);
  178. }
  179. function _resizeGD ($sSrcImage, $sDstImage) {
  180. $iErr = 0;
  181. $src_im =& $this->_createGDImage($sSrcImage, $size, $iErr);
  182. $sizeOrig = $size;
  183. if ($iErr)
  184. return $iErr;
  185. if (!$src_im)
  186. return IMAGE_ERROR_GD_OPEN_FAILED;
  187. $xd = $yd = 0;
  188. $xs = $ys = 0;
  189. if ($this->_isCrop) {
  190. $size[0] = $this->_cropW;
  191. $size[1] = $this->_cropH;
  192. $xs = $this->_cropX;
  193. $ys = $this->_cropY;
  194. $destW = $this->w;
  195. $destH = $this->h;
  196. } elseif ($this->_isSquareResize) {
  197. $destW = $this->w;
  198. $destH = $this->h;
  199. if ($size[0] < $size[1]) {
  200. $d = ($size[1] - $size[0])/2;
  201. $size[1] = $size[0];
  202. $xs = 0;
  203. $ys = (int)$d;
  204. } else {
  205. $d = ($size[0] - $size[1])/2;
  206. $size[0] = $size[1];
  207. $xs = (int)$d;
  208. $ys = 0;
  209. }
  210. } else {
  211. // determ destination size
  212. $sourceRatio = (float) ($size[0] / $size[1]);
  213. $destRatio = (float) ($this->w / $this->h);
  214. if ( $sourceRatio > $destRatio )
  215. {
  216. $resizeRatio = (float) ($this->w / $size[0]);
  217. }
  218. else
  219. {
  220. $resizeRatio = (float) ($this->h / $size[1]);
  221. }
  222. $destW = (int) ($resizeRatio * $size[0]);
  223. $destH = (int) ($resizeRatio * $size[1]);
  224. }
  225. // this is more qualitative function, but it doesn't exist in old GD and doesn't support GIF format
  226. if ( function_exists( 'imagecreatetruecolor' ) && $size[2] != IMAGE_TYPE_GIF )
  227. {
  228. // resize only if size is larger than needed
  229. if ( $this->_isCrop || $sizeOrig[0] > $this->w || $sizeOrig[1] > $this->h )
  230. {
  231. $dst_im = imagecreatetruecolor( $destW, $destH );
  232. imagecolortransparent($dst_im, imagecolorallocate($dst_im, 0, 0, 0));
  233. imagealphablending($dst_im, false);
  234. imagesavealpha($dst_im, true);
  235. $convertResult = imagecopyresampled( $dst_im, $src_im, $xd, $yd, $xs, $ys,
  236. $destW, $destH, $size[0], $size[1] );
  237. }
  238. else
  239. {
  240. $dst_im = $src_im;
  241. $convertResult = true;
  242. }
  243. }
  244. else // this is for old GD versions and for GIF images
  245. {
  246. // resize only if size is larger than needed
  247. if ( $size[0] > $this->w || $size[1] > $this->h )
  248. {
  249. $dst_im = imagecreate( $destW, $destH );
  250. if ($size[2] == IMAGE_TYPE_GIF) {
  251. ImageColorTransparent( $dst_im, imagecolorallocate($dst_im, 0, 0, 0) );
  252. imagealphablending( $dst_im, false );
  253. }
  254. $convertResult = imagecopyresized( $dst_im, $src_im, $xd, $yd, $xs, $ys,
  255. $destW, $destH, $size[0], $size[1] );
  256. }
  257. else
  258. {
  259. $dst_im = $src_im;
  260. $convertResult = true;
  261. }
  262. }
  263. if ( !$convertResult )
  264. return IMAGE_ERROR_GD_RESIZE_ERROR;
  265. $writeResult = $this->_writeImageGD ($dst_im, $sDstImage);
  266. // free memory
  267. if ( $dst_im != $src_im )
  268. {
  269. imagedestroy( $src_im );
  270. imagedestroy( $dst_im );
  271. }
  272. else
  273. {
  274. imagedestroy( $src_im );
  275. }
  276. if ( $writeResult && file_exists($sDstImage) )
  277. return IMAGE_ERROR_SUCCESS;
  278. return IMAGE_ERROR_GD_WRITE_FAILED;
  279. }
  280. function _resizeImageMagick ($sSrcImage, $sDstImage) {
  281. // TODO: $this->_isCrop and $this->_isSquareResize
  282. if ( $sSrcImage == $sDstImage )
  283. {
  284. $cmd = "{$GLOBALS['MOGRIFY']} -geometry {$this->w}\">\"x{$this->h}\">\" \"$sSrcImage\"";
  285. @exec( $cmd );
  286. $ext = strrchr($sSrcImage, '.');
  287. $nameWithoutExt = substr( $sSrcImage, 0, strrpos($sSrcImage, '.') );
  288. if ( file_exists( "{$nameWithoutExt}.mgk" ) )
  289. rename( "{$nameWithoutExt}.mgk", $sSrcImage );
  290. }
  291. else
  292. {
  293. $cmd = "{$GLOBALS['CONVERT']} \"$sSrcImage\" -geometry {$this->w}\">\"x{$this->h}\">\" \"$sDstImage\"";
  294. @exec( $cmd );
  295. }
  296. if ( file_exists($sDstImage) )
  297. return IMAGE_ERROR_SUCCESS;
  298. return IMAGE_ERROR_IMAGEMAGICK_ERROR;
  299. }
  300. function _applyWatermark( $srcFilename, $dstFilename, $wtrFilename, $wtrTransparency )
  301. {
  302. // TODO: watermard are not applied correctly, becuase images of different suzes are used, FIX IT
  303. // input validation
  304. $wtrTransparency = (int) $wtrTransparency;
  305. if ( $wtrTransparency > 100 )
  306. $wtrTransparency = 100;
  307. if ( !file_exists( $srcFilename ) )
  308. return IMAGE_ERROR_SOURCE_NOT_EXISTS;
  309. if ( !file_exists( $wtrFilename ) )
  310. return IMAGE_ERROR_SOURCE_NOT_EXISTS;
  311. // if destination and source filenames are equivalent then change mode for destination
  312. if ( $srcFilename == $dstFilename )
  313. chmod( $dstFilename, 0666 );
  314. if ( $this->_isUseGD )
  315. {
  316. return $this->_applyWatermarkGD($srcFilename, $dstFilename, $wtrFilename, $wtrTransparency);
  317. }
  318. else
  319. {
  320. return $this->_applyWatermarkImageMagick($srcFilename, $dstFilename, $wtrFilename, $wtrTransparency);
  321. }
  322. }
  323. function _applyWatermarkGD($srcFilename, $dstFilename, $wtrFilename, $wtrTransparency) {
  324. $iErr = 0;
  325. $src_im =& $this->_createGDImage($srcFilename, $size, $iErr);
  326. if ($iErr)
  327. return $iErr;
  328. if (!$src_im)
  329. return IMAGE_ERROR_GD_OPEN_FAILED;
  330. $wtr_im =& $this->_createGDImage($wtrFilename, $wtrSize, $iErr);
  331. if ($iErr)
  332. return $iErr;
  333. if (!$wtr_im )
  334. return IMAGE_ERROR_GD_OPEN_FAILED;
  335. if (function_exists('imagecreatetruecolor'))
  336. $dst_im = imagecreatetruecolor( $size[0], $size[1] );
  337. else
  338. $dst_im = imagecreate( $size[0], $size[1] );
  339. $watermarkX = ($size[0] - $wtrSize[0]) / 2;
  340. $watermarkY = $size[1] - $wtrSize[1];
  341. $copyResult = imagecopy( $dst_im, $src_im, 0, 0, 0, 0, $size[0], $size[1]);
  342. if (!$copyResult)
  343. return IMAGE_ERROR_GD_MERGE_ERROR;
  344. imagealphablending($wtr_im, false);
  345. imagesavealpha($wtr_im, true);
  346. if (IMAGE_TYPE_PNG == $wtrSize[2] && imageistruecolor($wtr_im))
  347. $mergeResult = imagecopy ( $dst_im, $wtr_im, $watermarkX, $watermarkY, 0, 0, $wtrSize[0], $wtrSize[1]);
  348. else
  349. $mergeResult = imagecopymerge ( $dst_im, $wtr_im, $watermarkX, $watermarkY, 0, 0, $wtrSize[0], $wtrSize[1], $wtrTransparency );
  350. if ( !$mergeResult )
  351. return IMAGE_ERROR_GD_MERGE_ERROR;
  352. switch ( $size[2] )
  353. {
  354. case IMAGE_TYPE_GIF:
  355. $writeResult = imagegif( $dst_im, $dstFilename );
  356. break;
  357. case IMAGE_TYPE_JPG:
  358. $writeResult = imagejpeg( $dst_im, $dstFilename, $this->_iJpegQuality);
  359. break;
  360. case IMAGE_TYPE_PNG:
  361. $writeResult = imagepng( $dst_im, $dstFilename );
  362. break;
  363. }
  364. // free memory
  365. imagedestroy( $dst_im );
  366. imagedestroy( $src_im );
  367. imagedestroy( $wtr_im );
  368. if ( $writeResult )
  369. return IMAGE_ERROR_SUCCESS;
  370. else
  371. return IMAGE_ERROR_GD_WRITE_FAILED;
  372. }
  373. function _applyWatermarkImageMagick($srcFilename, $dstFilename, $wtrFilename, $wtrTransparency) {
  374. $imgTransparency = 100 - $wtrTransparency;
  375. $cmd = "{$GLOBALS['COMPOSITE']} -gravity \"South\" -dissolve \"$imgTransparency\" \"$wtrFilename\" -dissolve $wtrTransparency \"$srcFilename\" \"$dstFilename\"";
  376. @exec( $cmd );
  377. if ( file_exists($dstFilename) )
  378. return IMAGE_ERROR_SUCCESS;
  379. return IMAGE_ERROR_IMAGEMAGICK_ERROR;
  380. }
  381. function & _createGDImage($s, &$aSize, &$iErr) {
  382. if (!isset($this->gdInfoArray))
  383. $this->gdInfoArray = gd_info();
  384. $aSize = getimagesize($s);
  385. // only GIF, JPG and PNG allowed
  386. switch ( $aSize[2] )
  387. {
  388. case IMAGE_TYPE_GIF:
  389. if (!isset($this->gdInfoArray['GIF Read Support']) || !$this->gdInfoArray['GIF Read Support'] || !isset($this->gdInfoArray['GIF Create Support']) || !$this->gdInfoArray['GIF Create Support'])
  390. return ($iErr = IMAGE_ERROR_GD_TYPE_NOT_SUPPORTED);
  391. $r = imagecreatefromgif( $s );
  392. return $r;
  393. case IMAGE_TYPE_JPG:
  394. if ((!isset($this->gdInfoArray['JPG Support']) || !$this->gdInfoArray['JPG Support']) && (!isset($this->gdInfoArray['JPEG Support']) || !$this->gdInfoArray['JPEG Support']))
  395. return ($iErr = IMAGE_ERROR_GD_TYPE_NOT_SUPPORTED);
  396. $r = imagecreatefromjpeg( $s );
  397. return $r;
  398. case IMAGE_TYPE_PNG:
  399. if (!isset($this->gdInfoArray['PNG Support']) || !$this->gdInfoArray['PNG Support'])
  400. return ($iErr = IMAGE_ERROR_GD_TYPE_NOT_SUPPORTED);
  401. $r = imagecreatefrompng( $s );
  402. return $r;
  403. default:
  404. return ($iErr = IMAGE_ERROR_WRONG_TYPE);
  405. }
  406. }
  407. function _writeImageGD ($src_im, $sDstImage) {
  408. $writeResult = false;
  409. switch ($this->_iForceOutputType){
  410. case IMAGE_TYPE_JPG:
  411. $writeResult = imagejpeg( $src_im, $sDstImage, $this->_iJpegQuality);
  412. break;
  413. case IMAGE_TYPE_PNG:
  414. $writeResult = imagepng( $src_im, $sDstImage);
  415. break;
  416. case IMAGE_TYPE_GIF:
  417. $writeResult = imagegif( $src_im, $sDstImage );
  418. break;
  419. default:
  420. switch ( $size[2] ) {
  421. case IMAGE_TYPE_GIF:
  422. $writeResult = imagegif( $src_im, $sDstImage );
  423. break;
  424. case IMAGE_TYPE_PNG:
  425. $writeResult = imagepng( $src_im, $sDstImage );
  426. break;
  427. case IMAGE_TYPE_JPG:
  428. default:
  429. $writeResult = imagejpeg( $src_im, $sDstImage, $this->_iJpegQuality);
  430. break;
  431. }
  432. }
  433. return $writeResult;
  434. }
  435. }
  436. /** @} */