| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
- * CC-BY License - http://creativecommons.org/licenses/by/3.0/
- *
- * @defgroup DolphinCore Dolphin Core
- * @{
- */
- // Function error codes
- define( 'IMAGE_ERROR_SUCCESS', 0 );
- define( 'IMAGE_ERROR_SOURCE_NOT_EXISTS', 1 );
- define( 'IMAGE_ERROR_WRONG_TYPE', 2 );
- define( 'IMAGE_ERROR_FILE_OPEN_FAILED', 3 );
- define( 'IMAGE_ERROR_IMAGEMAGICK_ERROR', 4 );
- define( 'IMAGE_ERROR_GD_NOT_INSTALLED', 5 );
- define( 'IMAGE_ERROR_GD_TYPE_NOT_SUPPORTED', 6 );
- define( 'IMAGE_ERROR_GD_OPEN_FAILED', 7 );
- define( 'IMAGE_ERROR_GD_RESIZE_ERROR', 8 );
- define( 'IMAGE_ERROR_GD_MERGE_ERROR', 9 );
- define( 'IMAGE_ERROR_GD_WRITE_FAILED', 10 );
- define( 'IMAGE_ERROR_GD_TTF_NOT_SUPPORTED', 11 );
- define( 'IMAGE_ERROR_GD_FILTER_ERROR', 12 );
- // Image types for GD
- // NOTE: actually these constants exist in PHP >= 4.3.0, but they are included for
- // back compatibility
- define( 'IMAGE_TYPE_GIF', 1 );
- define( 'IMAGE_TYPE_JPG', 2 );
- define( 'IMAGE_TYPE_PNG', 3 );
- class BxDolImageResize extends BxDol implements iBxDolSingleton {
- var $w = 64, $h = 64; ///< size of destination image
- var $_isCrop = false;
- var $_isAutoCrop = false;
- var $_cropX, $_cropY, $_cropW, $_cropH;
- 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
- var $_iJpegQuality = 90; ///< jpeg quality
- var $_isSquareResize = false; ///< use smart resize, destination image will be exact Width x Height size
- var $_isUseGD; ///< use GD library or command line ImagMagic utilites
- function BxDolImageResize() {
- if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
- trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
- parent::BxDol();
- $this->_isUseGD = getParam('enable_gd') == 'on' && extension_loaded('gd') ? true : false;
- }
- /**
- * Prevent cloning the instance
- */
- public function __clone() {
- if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
- trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
- }
- /**
- * Get singleton instance of the class
- */
- public static function getInstance() {
- if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
- $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolImageResize();
- return $GLOBALS['bxDolClasses'][__CLASS__];
- }
- function isAllowedImage ($sPath) {
- if ($this->_isUseGD) {
- $aImage = getimagesize($sPath);
- if (!empty($aImage) && in_array($aImage[2], array(1, 2, 3)))
- return true;
- } else {
- // TODO: image checking using image magick
- }
- return false;
- }
- function resize ($mixedImage, $sDstImage = '') {
- if (is_array($mixedImage)) {
- $aRet = array();
- foreach ($mixedImage as $s) {
- $aRet[] = $this->_resize ($s, $s);
- }
- return $aRet;
- } else {
- return $this->_resize ($mixedImage, $sDstImage);
- }
- }
- function applyWatermark ($mixedImage, $sDstImage, $wtrFilename, $wtrTransparency) {
- if (is_array($mixedImage)) {
- $aRet = array();
- foreach ($mixedImage as $s) {
- $aRet[] = $this->_applyWatermark ($s, $s, $wtrFilename, $wtrTransparency);
- }
- return $aRet;
- } else {
- return $this->_applyWatermark ($mixedImage, $sDstImage, $wtrFilename, $wtrTransparency);
- }
- }
- function grayscale ($mixedImage, $sDstImage = '') {
- if (is_array($mixedImage)) {
- $aRet = array();
- foreach ($mixedImage as $s) {
- $aRet[] = $this->_grayscale ($s, $s);
- }
- return $aRet;
- } else {
- return $this->_grayscale ($mixedImage, $sDstImage);
- }
- }
- function setSize ($w, $h) {
- $this->w = $w;
- $this->h = $h;
- }
- function removeCropOptions () {
- $this->_isCrop = false;
- $this->_isAutoCrop = false;
- }
- function setCropOptions ($x, $y, $w, $h) {
- $this->_isCrop = true;
- $this->_cropX = $x;
- $this->_cropY = $y;
- $this->_cropW = $w;
- $this->_cropH = $h;
- }
- /**
- * Crop image to destination size with filling whole area of destination size
- */
- function setAutoCrop ($b) {
- $this->_isAutoCrop = $b;
- }
- function setJpegOutput ($b) {
- $this->setOutputType($b ? IMAGE_TYPE_JPG : false);
- }
- function setOutputType ($iType) {
- $this->_iForceOutputType = $iType;;
- }
- function setJpegQuality ($i) {
- $this->_iJpegQuality = $i;
- }
- function setSquareResize ($b) {
- $this->_isSquareResize = ($b ? true : false);
- }
- function isUsedGD () {
- return $this->_isUseGD;
- }
- function getImageSize($sPath) {
- $aSize = getimagesize($sPath);
- return array ('w' => $aSize[0], 'h' => $aSize[1]);
- }
- // private functions are below -------------------------------
- function _grayscale ($sSrcImage, $sDstImage = '') {
- if (!file_exists($sSrcImage))
- return IMAGE_ERROR_SOURCE_NOT_EXISTS;
- if (!$sDstImage)
- $sDstImage = $sSrcImage;
- if ($sDstImage == $sSrcImage) {
- chmod($sDstImage, 0666);
- }
- return $this->_isUseGD ? $this->_grayscaleGD ($sSrcImage, $sDstImage) : $this->_grayscaleImageMagick ($sSrcImage, $sDstImage);
- }
- function _grayscaleGD ($sSrcImage, $sDstImage) {
- $iErr = 0;
- $src_im =& $this->_createGDImage($sSrcImage, $size, $iErr);
- $sizeOrig = $size;
- if ($iErr)
- return $iErr;
- if (!$src_im)
- return IMAGE_ERROR_GD_OPEN_FAILED;
- if (!imagefilter($src_im, IMG_FILTER_GRAYSCALE))
- return IMAGE_ERROR_GD_FILTER_ERROR;
- $writeResult = $this->_writeImageGD ($src_im, $sDstImage, $size[2]);
- // free memory
- imagedestroy( $src_im );
- if ( $writeResult && file_exists($sDstImage) )
- return IMAGE_ERROR_SUCCESS;
- return IMAGE_ERROR_GD_WRITE_FAILED;
- }
- function _grayscaleImageMagick ($sSrcImage, $sDstImage) {
- $cmd = "{$GLOBALS['CONVERT']} \"$sSrcImage\" -type Grayscale -quantize Gray \"$sDstImage\"";
- @exec( $cmd );
- if ( file_exists($sDstImage) )
- return IMAGE_ERROR_SUCCESS;
- return IMAGE_ERROR_IMAGEMAGICK_ERROR;
- }
- function _resize ($sSrcImage, $sDstImage = '') {
- if (!file_exists($sSrcImage))
- return IMAGE_ERROR_SOURCE_NOT_EXISTS;
- if (!$sDstImage)
- $sDstImage = $sSrcImage;
- if ($sDstImage == $sSrcImage) {
- chmod($sDstImage, 0666);
- }
- return $this->_isUseGD ? $this->_resizeGD ($sSrcImage, $sDstImage) : $this->_resizeImageMagick ($sSrcImage, $sDstImage);
- }
- function _resizeGD ($sSrcImage, $sDstImage) {
- $iErr = 0;
- $src_im =& $this->_createGDImage($sSrcImage, $size, $iErr);
- $sizeOrig = $size;
- if ($iErr)
- return $iErr;
- if (!$src_im)
- return IMAGE_ERROR_GD_OPEN_FAILED;
- $xd = $yd = 0;
- $xs = $ys = 0;
- if ($this->_isAutoCrop) {
- $sourceRatio = (float) ($size[0] / $size[1]);
- $destRatio = (float) ($this->w / $this->h);
- if ( $sourceRatio > $destRatio )
- $resizeRatio = (float) ($this->h / $size[1]);
- else
- $resizeRatio = (float) ($this->w / $size[0]);
- $destW = (int) ($resizeRatio * $size[0]);
- $destH = (int) ($resizeRatio * $size[1]);
- if ($destW > $this->w)
- $this->setCropOptions (floor(($size[0] - $this->w/$resizeRatio)/2.0), 0, floor($this->w/$resizeRatio), $size[1]);
- elseif ($destH > $this->h)
- $this->setCropOptions (0, floor(($size[1] - $this->h/$resizeRatio)/2.0), $size[0], floor($this->h/$resizeRatio));
- }
- if ($this->_isCrop) {
- $size[0] = $this->_cropW;
- $size[1] = $this->_cropH;
- $xs = $this->_cropX;
- $ys = $this->_cropY;
- $destW = $this->w;
- $destH = $this->h;
- } elseif ($this->_isSquareResize) {
- $destW = $this->w;
- $destH = $this->h;
- if ($size[0] < $size[1]) {
- $d = ($size[1] - $size[0])/2;
- $size[1] = $size[0];
- $xs = 0;
- $ys = (int)$d;
- } else {
- $d = ($size[0] - $size[1])/2;
- $size[0] = $size[1];
- $xs = (int)$d;
- $ys = 0;
- }
- } else {
- // determ destination size
- $sourceRatio = (float) ($size[0] / $size[1]);
- $destRatio = (float) ($this->w / $this->h);
- if ( $sourceRatio > $destRatio )
- $resizeRatio = (float) ($this->w / $size[0]);
- else
- $resizeRatio = (float) ($this->h / $size[1]);
- $destW = (int) ($resizeRatio * $size[0]);
- $destH = (int) ($resizeRatio * $size[1]);
- }
- // this is more qualitative function, but it doesn't exist in old GD and doesn't support GIF format
- if ( function_exists( 'imagecreatetruecolor' ) && $size[2] != IMAGE_TYPE_GIF )
- {
- // resize only if size is larger than needed
- if ( $this->_isCrop || $sizeOrig[0] > $this->w || $sizeOrig[1] > $this->h )
- {
- $dst_im = imagecreatetruecolor( $destW, $destH );
- imagecolortransparent($dst_im, imagecolorallocate($dst_im, 0, 0, 0));
- imagealphablending($dst_im, false);
- imagesavealpha($dst_im, true);
- $convertResult = imagecopyresampled( $dst_im, $src_im, $xd, $yd, $xs, $ys,
- $destW, $destH, $size[0], $size[1] );
- }
- else
- {
- $dst_im = $src_im;
- $convertResult = true;
- }
- }
- else // this is for old GD versions and for GIF images
- {
- // resize only if size is larger than needed
- if ( $size[0] > $this->w || $size[1] > $this->h )
- {
- $dst_im = imagecreate( $destW, $destH );
- if ($size[2] == IMAGE_TYPE_GIF) {
- ImageColorTransparent( $dst_im, imagecolorallocate($dst_im, 0, 0, 0) );
- imagealphablending( $dst_im, false );
- }
- $convertResult = imagecopyresized( $dst_im, $src_im, $xd, $yd, $xs, $ys,
- $destW, $destH, $size[0], $size[1] );
- }
- else
- {
- $dst_im = $src_im;
- $convertResult = true;
- }
- }
- if ( !$convertResult )
- return IMAGE_ERROR_GD_RESIZE_ERROR;
- $writeResult = $this->_writeImageGD ($dst_im, $sDstImage, $size[2]);
- // free memory
- if ( $dst_im != $src_im )
- {
- imagedestroy( $src_im );
- imagedestroy( $dst_im );
- }
- else
- {
- imagedestroy( $src_im );
- }
- if ( $writeResult && file_exists($sDstImage) )
- return IMAGE_ERROR_SUCCESS;
- return IMAGE_ERROR_GD_WRITE_FAILED;
- }
- function _resizeImageMagick ($sSrcImage, $sDstImage) {
- // TODO: $this->_isCrop and $this->_isSquareResize
- if ( $sSrcImage == $sDstImage )
- {
- $cmd = "{$GLOBALS['MOGRIFY']} -geometry {$this->w}\">\"x{$this->h}\">\" \"$sSrcImage\"";
- @exec( $cmd );
- $ext = strrchr($sSrcImage, '.');
- $nameWithoutExt = substr( $sSrcImage, 0, strrpos($sSrcImage, '.') );
- if ( file_exists( "{$nameWithoutExt}.mgk" ) )
- rename( "{$nameWithoutExt}.mgk", $sSrcImage );
- }
- else
- {
- $cmd = "{$GLOBALS['CONVERT']} \"$sSrcImage\" -geometry {$this->w}\">\"x{$this->h}\">\" \"$sDstImage\"";
- @exec( $cmd );
- }
- if ( file_exists($sDstImage) )
- return IMAGE_ERROR_SUCCESS;
- return IMAGE_ERROR_IMAGEMAGICK_ERROR;
- }
- function _applyWatermark( $srcFilename, $dstFilename, $wtrFilename, $wtrTransparency )
- {
- // TODO: watermard are not applied correctly, becuase images of different suzes are used, FIX IT
- // input validation
- $wtrTransparency = (int) $wtrTransparency;
- if ( $wtrTransparency > 100 )
- $wtrTransparency = 100;
- if ( !file_exists( $srcFilename ) )
- return IMAGE_ERROR_SOURCE_NOT_EXISTS;
- if ( !file_exists( $wtrFilename ) )
- return IMAGE_ERROR_SOURCE_NOT_EXISTS;
- // if destination and source filenames are equivalent then change mode for destination
- if ( $srcFilename == $dstFilename )
- chmod( $dstFilename, 0666 );
- if ( $this->_isUseGD )
- {
- return $this->_applyWatermarkGD($srcFilename, $dstFilename, $wtrFilename, $wtrTransparency);
- }
- else
- {
- return $this->_applyWatermarkImageMagick($srcFilename, $dstFilename, $wtrFilename, $wtrTransparency);
- }
- }
- function _applyWatermarkGD($srcFilename, $dstFilename, $wtrFilename, $wtrTransparency) {
- $iErr = 0;
- $src_im =& $this->_createGDImage($srcFilename, $size, $iErr);
- if ($iErr)
- return $iErr;
- if (!$src_im)
- return IMAGE_ERROR_GD_OPEN_FAILED;
- $wtr_im =& $this->_createGDImage($wtrFilename, $wtrSize, $iErr);
- if ($iErr)
- return $iErr;
- if (!$wtr_im )
- return IMAGE_ERROR_GD_OPEN_FAILED;
- if (function_exists('imagecreatetruecolor'))
- $dst_im = imagecreatetruecolor( $size[0], $size[1] );
- else
- $dst_im = imagecreate( $size[0], $size[1] );
- $watermarkX = ($size[0] - $wtrSize[0]) / 2;
- $watermarkY = $size[1] - $wtrSize[1];
- $copyResult = imagecopy( $dst_im, $src_im, 0, 0, 0, 0, $size[0], $size[1]);
- if (!$copyResult)
- return IMAGE_ERROR_GD_MERGE_ERROR;
- imagealphablending($wtr_im, false);
- imagesavealpha($wtr_im, true);
- if (IMAGE_TYPE_PNG == $wtrSize[2] && imageistruecolor($wtr_im))
- $mergeResult = imagecopy ( $dst_im, $wtr_im, $watermarkX, $watermarkY, 0, 0, $wtrSize[0], $wtrSize[1]);
- else
- $mergeResult = imagecopymerge ( $dst_im, $wtr_im, $watermarkX, $watermarkY, 0, 0, $wtrSize[0], $wtrSize[1], $wtrTransparency );
- if ( !$mergeResult )
- return IMAGE_ERROR_GD_MERGE_ERROR;
- switch ( $size[2] )
- {
- case IMAGE_TYPE_GIF:
- $writeResult = imagegif( $dst_im, $dstFilename );
- break;
- case IMAGE_TYPE_JPG:
- $writeResult = imagejpeg( $dst_im, $dstFilename, $this->_iJpegQuality);
- break;
- case IMAGE_TYPE_PNG:
- $writeResult = imagepng( $dst_im, $dstFilename );
- break;
- }
- // free memory
- imagedestroy( $dst_im );
- imagedestroy( $src_im );
- imagedestroy( $wtr_im );
- if ( $writeResult )
- return IMAGE_ERROR_SUCCESS;
- else
- return IMAGE_ERROR_GD_WRITE_FAILED;
- }
- function _applyWatermarkImageMagick($srcFilename, $dstFilename, $wtrFilename, $wtrTransparency) {
- $imgTransparency = 100 - $wtrTransparency;
- $cmd = "{$GLOBALS['COMPOSITE']} -gravity \"South\" -dissolve \"$imgTransparency\" \"$wtrFilename\" -dissolve $wtrTransparency \"$srcFilename\" \"$dstFilename\"";
- @exec( $cmd );
- if ( file_exists($dstFilename) )
- return IMAGE_ERROR_SUCCESS;
- return IMAGE_ERROR_IMAGEMAGICK_ERROR;
- }
- function & _createGDImage($s, &$aSize, &$iErr) {
- if (!isset($this->gdInfoArray))
- $this->gdInfoArray = gd_info();
- $aSize = getimagesize($s);
- // only GIF, JPG and PNG allowed
- switch ( $aSize[2] )
- {
- case IMAGE_TYPE_GIF:
- if (!isset($this->gdInfoArray['GIF Read Support']) || !$this->gdInfoArray['GIF Read Support'] || !isset($this->gdInfoArray['GIF Create Support']) || !$this->gdInfoArray['GIF Create Support'])
- return ($iErr = IMAGE_ERROR_GD_TYPE_NOT_SUPPORTED);
- $r = imagecreatefromgif( $s );
- return $r;
- case IMAGE_TYPE_JPG:
- if ((!isset($this->gdInfoArray['JPG Support']) || !$this->gdInfoArray['JPG Support']) && (!isset($this->gdInfoArray['JPEG Support']) || !$this->gdInfoArray['JPEG Support']))
- return ($iErr = IMAGE_ERROR_GD_TYPE_NOT_SUPPORTED);
- $r = imagecreatefromjpeg( $s );
- return $r;
- case IMAGE_TYPE_PNG:
- if (!isset($this->gdInfoArray['PNG Support']) || !$this->gdInfoArray['PNG Support'])
- return ($iErr = IMAGE_ERROR_GD_TYPE_NOT_SUPPORTED);
- $r = imagecreatefrompng( $s );
- return $r;
- default:
- return ($iErr = IMAGE_ERROR_WRONG_TYPE);
- }
- }
- function _writeImageGD ($src_im, $sDstImage, $iSrcImageType) {
- $writeResult = false;
- switch ($this->_iForceOutputType){
- case IMAGE_TYPE_JPG:
- $writeResult = imagejpeg( $src_im, $sDstImage, $this->_iJpegQuality);
- break;
- case IMAGE_TYPE_PNG:
- $writeResult = imagepng( $src_im, $sDstImage);
- break;
- case IMAGE_TYPE_GIF:
- $writeResult = imagegif( $src_im, $sDstImage );
- break;
- default:
- switch ($iSrcImageType) {
- case IMAGE_TYPE_GIF:
- $writeResult = imagegif( $src_im, $sDstImage );
- break;
- case IMAGE_TYPE_PNG:
- $writeResult = imagepng( $src_im, $sDstImage );
- break;
- case IMAGE_TYPE_JPG:
- default:
- $writeResult = imagejpeg( $src_im, $sDstImage, $this->_iJpegQuality);
- break;
- }
- }
- return $writeResult;
- }
- }
- /** @} */
|