registerHandlers(); // make sure to call it only once! before the first usage, no need to call it every time * $sTranscodedImageUrl = $oTranscoder->getFileUrl('my_dog.jpg'); // the name of file, in the case of 'Folder' storage type this is file name * echo 'My dog : '; // transcoded(resized and/or grayscaled) image will be shown, according to the specified filters * @endcode * */ class BxDolTranscoderImage extends BxDolTranscoder implements iBxDolFactoryObject { protected function __construct($aObject, $oStorage) { parent::__construct($aObject, $oStorage); $this->_oDb = new BxDolTranscoderImageQuery($aObject, false); $this->_sQueueTable = $this->_oDb->getQueueTable(); } public static function getObjectAbstract() { if (isset($GLOBALS['bxDolClasses'][__CLASS__ . '!Abstract'])) return $GLOBALS['bxDolClasses'][__CLASS__ . '!Abstract']; $aObject = array('object' => 'abstract'); $o = new BxDolTranscoderImage ($aObject, null); return ($GLOBALS['bxDolClasses'][__CLASS__ . '!Abstract'] = $o); } /** * check if transcoder suppors given file mime type */ public function isMimeTypeSupported($sMimeType) { $sMimeType = strtolower($sMimeType); switch ($sMimeType) { case 'image/gif': case 'image/jpeg': case 'image/pjpeg': case 'image/png': case 'image/webp': return true; } return false; } public function isMimeTypeSvg($sMimeType) { return $this->_oStorage->isMimeTypeSvg($sMimeType); } /** * Get transcoded file url. * If transcoded file is ready then direct url to the file is returned. * If there is no transcoded data available, then special url is returned, upon opening this url image is transcoded automatically and redirects to the ready transcoed image. * @param $mixedHandler - file handler * @return file url, or false on error. */ public function getFileUrl($mixedHandler) { if(($sFileUrl = $this->getOrigFileUrl($mixedHandler)) !== false) { $sFileMimeType = $this->_oStorage->getMimeTypeByFileName($sFileUrl); if($this->isMimeTypeSvg($sFileMimeType)) return $sFileUrl; } return parent::getFileUrl($mixedHandler); } public function getFileMimeType($mixedHandler) { return $this->_oStorage->getMimeTypeByFileName($this->getFileUrl($mixedHandler)); } /** * Get file url when file isn't transcoded yet */ public function getFileUrlNotReady($mixedHandler) { return BX_DOL_URL_ROOT . 'image_transcoder.php?o=' . $this->_aObject['object'] . '&h=' . $mixedHandler . '&dpx=' . $this->getDevicePixelRatio() . '&t=' . time(); } public function isFileReady ($mixedHandlerOrig, $isCheckOutdated = true) { if (isAdmin() && false !== $this->getFilterParams('ResizeVar')) { // only operators can apply new image size $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandlerOrig); $aTranscodedFileData = $this->_oDb->getTranscodedFileData ($mixedHandler); $x = $this->getCustomResizeDimension ('x'); $y = $this->getCustomResizeDimension ('y'); // if new sizes are provided - delete old image, so new one will be created if (($x && (!isset($aTranscodedFileData['x']) || $x != $aTranscodedFileData['x'])) || ($y && (!isset($aTranscodedFileData['y']) || $y != $aTranscodedFileData['y']))) { if (!($iFileId = $this->_oDb->getFileIdByHandler($mixedHandler))) return false; if (!($aFile = $this->_oStorage->getFile($iFileId))) return false; if (!$this->_oStorage->deleteFile($aFile['id'])) return false; } } return parent::isFileReady ($mixedHandlerOrig, $isCheckOutdated); } public function getSize ($sFile) { $oImage = BxDolImageResize::getInstance(); $aSize = $oImage->getImageSize($sFile); return $aSize; } protected function getCustomResizeDimension ($sName) { $i = (int)bx_get($sName); if ($i > 2048) $i = 2048; if ($i && $i < 16) $i = 16; return $i; } public function transcode ($mixedHandler, $iProfileId = 0) { if (!($bRet = parent::transcode ($mixedHandler, $iProfileId))) return $bRet; $x = $this->getCustomResizeDimension ('x'); $y = $this->getCustomResizeDimension ('y'); if ($x || $y) { $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandler); $this->_oDb->updateTranscodedFileData($mixedHandler, array('x' => $x, 'y' => $y)); } return $bRet; } protected function applyFilter_Grayscale ($sFile, $aParams) { $o = BxDolImageResize::getInstance(); $o->removeCropOptions (); if (IMAGE_ERROR_SUCCESS == $o->grayscale($sFile)) return true; bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: applyFilter_Grayscale failed for file ({$sFile}): " . $o->getError(), BX_LOG_ERR); return false; } protected function applyFilter_ResizeVar ($sFile, $aParams) { $aParams['w'] = $this->getCustomResizeDimension ('x'); $aParams['h'] = $this->getCustomResizeDimension ('y'); if(!$aParams['w']) unset($aParams['w']); if(!$aParams['h']) unset($aParams['h']); if(!isset($aParams['w']) && !isset($aParams['h'])) return true; return $this->applyFilter_Resize ($sFile, $aParams); } } /** @} */