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                 foreach ( glob( "{$sGenPath}*/{$sPicName}" ) as $sFile ) {
00123                     $blDeleted = unlink( $sFile );
00124                 }
00125             }
00126         }
00127         return $blDeleted;
00128     }
00129 
00130 
00141     protected function _isPicDeletable( $sPicName, $sTable, $sField )
00142     {
00143         if ( !$sPicName || strpos( $sPicName, 'nopic.jpg' ) !== false || strpos( $sPicName, 'nopic_ico.jpg' ) !== false ) {
00144             return false;
00145         }
00146 
00147         $oDb = oxDb::getDb();
00148         $iCountUsed = $oDb->getOne( "select count(*) from $sTable where $sField = ".$oDb->quote( $sPicName ). " group by $sField " );
00149         return $iCountUsed > 1 ? false : true;
00150     }
00151 
00165     public function overwritePic( $oObject, $sPicTable, $sPicField, $sPicType, $sPicDir, $aParams, $sAbsDynImageDir )
00166     {
00167         $blDelete = false;
00168         $sPic = $sPicTable.'__'.$sPicField;
00169         if ( isset( $oObject->{$sPic} ) &&
00170              ( $_FILES['myfile']['size'][$sPicType.'@'.$sPic] > 0 || $aParams[$sPic] != $oObject->{$sPic}->value ) ) {
00171 
00172             $sImgDir = $sAbsDynImageDir . $sPicDir;
00173             $blDelete = $this->safePictureDelete($oObject->{$sPic}->value, $sImgDir, $sPicTable, $sPicField );
00174         }
00175 
00176         return $blDelete;
00177     }
00178 
00193     protected function _resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch )
00194     {
00195         return resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch );
00196     }
00197 
00213     protected function _resize( $aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iGdVer, $blDisableTouch, $iDefQuality )
00214     {
00215         startProfile("PICTURE_RESIZE");
00216 
00217         $blSuccess = false;
00218         switch ( $aImageInfo[2] ) { //Image type
00219             case ( $this->_aImageTypes["GIF"] ):
00220                 //php does not process gifs until 7th July 2004 (see lzh licensing)
00221                 if ( function_exists( "imagegif" ) ) {
00222                     $blSuccess = resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1], $iGdVer );
00223                 }
00224                 break;
00225             case ( $this->_aImageTypes["JPEG"] ):
00226             case ( $this->_aImageTypes["JPG"] ):
00227                 $blSuccess = resizeJpeg( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage, $iDefQuality );
00228                 break;
00229             case ( $this->_aImageTypes["PNG"] ):
00230                 $blSuccess = resizePng( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage );
00231                 break;
00232         }
00233 
00234         if ( $blSuccess && !$blDisableTouch ) {
00235             @touch( $sTarget );
00236         }
00237 
00238         stopProfile("PICTURE_RESIZE");
00239 
00240         return $blSuccess;
00241     }
00242 
00257     protected function _copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch )
00258     {
00259         $blSuccess = copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer );
00260         if ( !$blDisableTouch && $blSuccess ) {
00261             @touch( $sTarget );
00262         }
00263         return $blSuccess;
00264     }
00265 
00266 }