oxutilspic.php

Go to the documentation of this file.
00001 <?php
00002 
00006 require_once getShopBasePath() . "core/utils/oxpicgenerator.php";
00007 
00011 class oxUtilsPic extends oxSuperCfg
00012 {
00018     protected $_aImageTypes = array("GIF" => IMAGETYPE_GIF, "JPG" => IMAGETYPE_JPEG, "PNG" => IMAGETYPE_PNG, "JPEG" => IMAGETYPE_JPEG);
00019 
00025     private static $_instance = null;
00026 
00032     public static function getInstance()
00033     {
00034         // disable caching for test modules
00035         if ( defined( 'OXID_PHP_UNIT' ) ) {
00036             self::$_instance = modInstances::getMod( __CLASS__ );
00037         }
00038 
00039         if ( !self::$_instance instanceof oxUtilsPic ) {
00040 
00041 
00042             self::$_instance = oxNew( 'oxUtilsPic' );
00043 
00044             if ( defined( 'OXID_PHP_UNIT' ) ) {
00045                 modInstances::addMod( __CLASS__, self::$_instance);
00046             }
00047         }
00048         return self::$_instance;
00049     }
00050 
00051 
00062     public function resizeImage( $sSrc, $sTarget, $iDesiredWidth, $iDesiredHeight )
00063     {
00064         $blResize = false;
00065 
00066         // use this GD Version
00067         if ( ( $iUseGDVersion = getGdVersion() ) && function_exists( 'imagecreate' ) &&
00068              file_exists( $sSrc ) && ( $aImageInfo = @getimagesize( $sSrc ) ) ) {
00069 
00070             $myConfig = $this->getConfig();
00071             list( $iWidth, $iHeight ) = calcImageSize( $iDesiredWidth, $iDesiredHeight, $aImageInfo[0], $aImageInfo[1] );
00072             $blResize = $this->_resize( $aImageInfo, $sSrc, null, $sTarget, $iWidth, $iHeight, $iUseGDVersion, $myConfig->getConfigParam( 'blDisableTouch' ), $myConfig->getConfigParam( 'sDefaultImageQuality' ) );
00073         }
00074         return $blResize;
00075     }
00076 
00077 
00088     public function safePictureDelete( $sPicName, $sAbsDynImageDir, $sTable, $sField )
00089     {
00090         $blDelete = false;
00091         if ( $this->_isPicDeletable( $sPicName, $sTable, $sField ) ) {
00092             $blDelete = $this->_deletePicture( $sPicName, $sAbsDynImageDir );
00093         }
00094         return $blDelete;
00095     }
00096 
00105     protected function _deletePicture( $sPicName, $sAbsDynImageDir )
00106     {
00107         $blDeleted = false;
00108         $myConfig  = $this->getConfig();
00109 
00110         if ( !$myConfig->isDemoShop() && ( strpos( $sPicName, 'nopic.jpg' ) === false ||
00111              strpos( $sPicName, 'nopic_ico.jpg' ) === false ) ) {
00112 
00113             $sFile = "$sAbsDynImageDir/$sPicName";
00114 
00115             if ( file_exists( $sFile ) && is_file( $sFile ) ) {
00116                 $blDeleted = unlink( $sFile );
00117             }
00118 
00119             if ( !$myConfig->getConfigParam( 'sAltImageUrl' ) ) {
00120                 // deleting various size generated images
00121                 $sGenPath = str_replace( '/master/', '/generated/', $sAbsDynImageDir );
00122                 $aFiles = glob( "{$sGenPath}*/{$sPicName}" );
00123                 if ( is_array($aFiles) ) {
00124                     foreach ( $aFiles as $sFile ) {
00125                         $blDeleted = unlink( $sFile );
00126                     }
00127                 }
00128             }
00129         }
00130         return $blDeleted;
00131     }
00132 
00133 
00144     protected function _isPicDeletable( $sPicName, $sTable, $sField )
00145     {
00146         if ( !$sPicName || strpos( $sPicName, 'nopic.jpg' ) !== false || strpos( $sPicName, 'nopic_ico.jpg' ) !== false ) {
00147             return false;
00148         }
00149 
00150         $oDb = oxDb::getDb();
00151         $iCountUsed = $oDb->getOne( "select count(*) from $sTable where $sField = ".$oDb->quote( $sPicName ). " group by $sField ", false, false);
00152         return $iCountUsed > 1 ? false : true;
00153     }
00154 
00168     public function overwritePic( $oObject, $sPicTable, $sPicField, $sPicType, $sPicDir, $aParams, $sAbsDynImageDir )
00169     {
00170         $blDelete = false;
00171         $sPic = $sPicTable.'__'.$sPicField;
00172         if ( isset( $oObject->{$sPic} ) &&
00173              ( $_FILES['myfile']['size'][$sPicType.'@'.$sPic] > 0 || $aParams[$sPic] != $oObject->{$sPic}->value ) ) {
00174 
00175             $sImgDir = $sAbsDynImageDir . oxUtilsFile::getInstance()->getImageDirByType($sPicType);
00176             $blDelete = $this->safePictureDelete($oObject->{$sPic}->value, $sImgDir, $sPicTable, $sPicField );
00177         }
00178 
00179         return $blDelete;
00180     }
00181 
00196     protected function _resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch )
00197     {
00198         return resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch );
00199     }
00200 
00216     protected function _resize( $aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iGdVer, $blDisableTouch, $iDefQuality )
00217     {
00218         startProfile("PICTURE_RESIZE");
00219 
00220         $blSuccess = false;
00221         switch ( $aImageInfo[2] ) { //Image type
00222             case ( $this->_aImageTypes["GIF"] ):
00223                 //php does not process gifs until 7th July 2004 (see lzh licensing)
00224                 if ( function_exists( "imagegif" ) ) {
00225                     $blSuccess = resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1], $iGdVer );
00226                 }
00227                 break;
00228             case ( $this->_aImageTypes["JPEG"] ):
00229             case ( $this->_aImageTypes["JPG"] ):
00230                 $blSuccess = resizeJpeg( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage, $iDefQuality );
00231                 break;
00232             case ( $this->_aImageTypes["PNG"] ):
00233                 $blSuccess = resizePng( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage );
00234                 break;
00235         }
00236 
00237         if ( $blSuccess && !$blDisableTouch ) {
00238             @touch( $sTarget );
00239         }
00240 
00241         stopProfile("PICTURE_RESIZE");
00242 
00243         return $blSuccess;
00244     }
00245 
00260     protected function _copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch )
00261     {
00262         $blSuccess = copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer );
00263         if ( !$blDisableTouch && $blSuccess ) {
00264             @touch( $sTarget );
00265         }
00266         return $blSuccess;
00267     }
00268 
00269 }