3 require_once dirname(__FILE__) .
"/../bootstrap.php";
6 if ( !function_exists(
"getShopBasePath" ) ) {
12 function getShopBasePath()
14 return realpath( dirname( __FILE__ ) .
'/..' ) .
'/';
20 if ( !function_exists(
'isAdmin' )) {
33 if ( !function_exists(
"getConfig" ) ) {
42 include_once getShopBasePath() .
'modules/functions.php';
45 include_once getShopBasePath() .
'core/oxfunctions.php';
53 if ( !function_exists(
"getGeneratorInstanceName" ) ) {
59 function getGeneratorInstanceName()
61 return "oxdynimggenerator";
66 if ( !function_exists(
"getGdVersion" ) ) {
72 function getGdVersion()
74 static $iVersion = null;
76 if ( $iVersion === null ) {
78 if ( function_exists(
"gd_info" ) ) {
81 if ( isset( $aInfo[
"GD Version"] ) ) {
82 $iVersion = version_compare( preg_replace(
"/[^0-9\.]/",
"", $aInfo[
"GD Version"] ), 1,
'>' ) ? 2 : 1;
92 if ( !function_exists(
"includeImageUtils" ) ) {
98 function includeImageUtils()
100 include_once getShopBasePath() .
"core/utils/oxpicgenerator.php";
151 "sIconsize" =>
'/.*\/generated\/product\/(icon|\d+)\/\d+\_\d+\_\d+$/',
152 "sThumbnailsize" =>
'/.*\/generated\/product\/(thumb|\d+)\/\d+\_\d+\_\d+$/',
153 "sZoomImageSize" =>
'/.*\/generated\/product\/\d+\/\d+\_\d+\_\d+$/',
154 "aDetailImageSizes" =>
'/.*\/generated\/product\/\d+\/\d+\_\d+\_\d+$/',
157 "sManufacturerIconsize" =>
'/.*\/generated\/(manufacturer|vendor)\/icon\/\d+\_\d+\_\d+$/',
160 "sCatThumbnailsize" =>
'/.*\/generated\/category\/thumb\/\d+\_\d+\_\d+$/',
161 "sCatIconsize" =>
'/.*\/generated\/category\/icon\/\d+\_\d+\_\d+$/',
162 "sCatPromotionsize" =>
'/.*\/generated\/category\/promo_icon\/\d+\_\d+\_\d+$/'
172 if ( self::$_oInstance === null ) {
173 $sInstanceName = getGeneratorInstanceName();
174 self::$_oInstance =
new $sInstanceName();
190 public function __call( $sMethod, $aArgs )
192 if ( defined(
'OXID_PHP_UNIT' ) ) {
193 if ( substr( $sMethod, 0, 4) ==
"UNIT" ) {
194 $sMethod = str_replace(
"UNIT",
"_", $sMethod );
196 if ( method_exists( $this, $sMethod)) {
197 return call_user_func_array( array( & $this, $sMethod ), $aArgs );
201 throw new oxSystemComponentException(
"Function '$sMethod' does not exist or is not accessible! (" . get_class($this) .
")".PHP_EOL);
211 return getConfig()->getConfigParam(
"sShopDir" );
221 if ( $this->_sImageUri === null ) {
223 $this->_sImageUri =
"";
224 $sReqPath =
"out/pictures/generated";
227 $sReqImg = isset( $_SERVER[
"REQUEST_URI"] ) ? urldecode($_SERVER[
"REQUEST_URI"]) :
"";
228 if ( ( $iPos = strpos( $sReqImg, $sReqPath ) ) !==
false ) {
229 $this->_sImageUri = substr( $sReqImg, $iPos );
232 $this->_sImageUri = trim( $this->_sImageUri,
"/" );
258 if ( $sUri && ( $sPath = dirname( dirname( $sUri ) ) ) ) {
259 $sPath = preg_replace(
"/\/([^\/]*)\/([^\/]*)\/([^\/]*)$/",
"/master/\\2/\\3/", $sPath );
274 $aInfo = explode( $this->_sImageInfoSep, basename( dirname( $sUri ) ) );
298 return str_replace( $this->
_getImageName(),
"nopic.jpg", $sPath );
308 $sType = preg_replace(
"/.*\.(png|jp(e)?g|gif)$/",
"\\1", $this->
_getImageName() );
309 $sType = ( strcmp( $sType,
"jpg") == 0 ) ?
"jpeg" : $sType;
310 return in_array( $sType, $this->_aAllowedImgTypes ) ? $sType :
false;
323 protected function _generatePng( $sSource, $sTarget, $iWidth, $iHeight )
325 return resizePng( $sSource, $sTarget, $iWidth, $iHeight, @getimagesize( $sSource ), getGdVersion(), null );
339 protected function _generateJpg( $sSource, $sTarget, $iWidth, $iHeight, $iQuality )
341 return resizeJpeg( $sSource, $sTarget, $iWidth, $iHeight, @getimagesize( $sSource ), getGdVersion(), null, $iQuality );
354 protected function _generateGif( $sSource, $sTarget, $iWidth, $iHeight )
356 $aImageInfo = @getimagesize( $sSource );
357 return resizeGif( $sSource, $sTarget, $iWidth, $iHeight, $aImageInfo[0], $aImageInfo[1], getGdVersion() );
371 $sDir = dirname( trim( $sPath ) );
374 if ( ! is_dir( $sDir ) && ( $blValid = $this->
_isValidPath( $sDir ) ) ) {
391 $oConfig = getConfig();
392 $sPicFolderPath = dirname( $oConfig->getMasterPictureDir() );
395 if ( $sPicFolderPath && is_dir( $sPicFolderPath ) ) {
398 if ( strcmp( $sPicFolderPath, substr( $sDir, 0, strlen( $sPicFolderPath ) ) ) == 0 ) {
400 if ( ! ( $blDone = file_exists( $sDir ) ) ) {
404 if ( defined(
'OXID_PHP_UNIT' ) ) {
407 $blDone = mkdir( $sDir, $iMode,
true ) || file_exists( $sDir );
426 list( $iWidth, $iHeight, $sQuality ) = $this->
_getImageInfo();
427 if ( $iWidth && $iHeight && $sQuality ) {
429 $oConfig = getConfig();
434 foreach ( $this->_aConfParamToPath as $sParamName => $sPathReg ) {
435 if ( preg_match( $sPathReg, $sPath ) ) {
439 $sNames .= $oDb->quote( $sParamName );
441 if ( $sParamName ==
"sManufacturerIconsize" || $sParamName ==
"sCatIconsize" ) {
442 $sNames .=
", " . $oDb->quote(
"sIconsize" );
450 $sDecodeField = $oConfig->getDecodeValueQuery();
453 $sQ =
"select oxshopid from oxconfig where oxvarname = 'sDefaultImageQuality' and
454 {$sDecodeField} = " . $oDb->quote( $sQuality );
456 $aShopIds = $oDb->getAll( $sQ );
461 foreach ( $aShopIds as $aShopId ) {
469 $sShopIds .= $oDb->quote( $aShopId[
"oxshopid"] );
476 $sCheckSize =
"$iWidth*$iHeight";
479 $sQ =
"select oxvartype, {$sDecodeField} as oxvarvalue from oxconfig
480 where oxvarname in ( {$sNames} ) and oxshopid in ( {$sShopIds} ) order by oxshopid";
482 $aValues = $oDb->getAll( $sQ );
483 foreach ( $aValues as $aValue ) {
484 $aConfValues = (array) $oConfig->decodeValue( $aValue[
"oxvartype"], $aValue[
"oxvarvalue"] );
485 foreach ( $aConfValues as $sValue ) {
486 if ( strcmp( $sCheckSize, $sValue ) == 0 ) {
516 if ( $this->
_lock( $sImageTarget ) ) {
519 list( $iWidth, $iHeight, $iQuality ) = $this->
_getImageInfo();
520 switch ( $sImageType ) {
522 $sPath = $this->
_generatePng( $sImageSource, $sImageTarget, $iWidth, $iHeight );
525 $sPath = $this->
_generateJpg( $sImageSource, $sImageTarget, $iWidth, $iHeight, $iQuality );
528 $sPath = $this->
_generateGif( $sImageSource, $sImageTarget, $iWidth, $iHeight );
534 $this->
_unlock( $sImageTarget );
538 $sPath = file_exists( $sImageTarget ) ? $sImageTarget :
false;
564 protected function _lock( $sSource )
570 $this->_hLockHandle = @fopen( $this->
_getLockName( $sSource ),
"w" );
571 if ( $this->_hLockHandle ) {
572 if ( !( $blLocked = flock( $this->_hLockHandle, LOCK_EX ) ) ) {
581 if (!( file_exists( $this->
_getLockName( $sSource ) ) && abs( time() - filectime( $this->
_getLockName( $sSource ) ) < 40 ) ) ) {
582 if ( $this->_hLockHandle = @fopen( $this->
_getLockName( $sSource ),
"w" ) ) {
600 if ( $this->_hLockHandle ) {
601 flock( $this->_hLockHandle, LOCK_UN );
602 fclose( $this->_hLockHandle );
618 $this->_sImageUri = str_replace( $this->
_getShopBasePath(),
"", $sAbsPath );
627 if ( file_exists( $sMasterImagePath ) ) {
631 $sMasterImagePath = $this->
_getShopBasePath() . dirname( dirname( $sMasterPath ) ) .
"/nopic.jpg";
635 $this->
_setHeader(
"HTTP/1.0 404 Not Found" );
639 if ( file_exists( $sGenImagePath ) ) {
640 $sImagePath = $sGenImagePath;
641 } elseif ( file_exists( $sMasterImagePath ) ) {
643 $sImagePath = $this->
_generateImage( $sMasterImagePath, $sGenImagePath );
651 $this->
_setHeader(
"HTTP/1.0 404 Not Found" );
667 if ( defined(
'OXID_PHP_UNIT' ) ) {
686 foreach ( $aHeaders as $sHeader ) {
698 @readfile( $sImgPath );
711 $this->_aHeaders[] = $sHeader;