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