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 
00034     public static function getInstance()
00035     {
00036         return oxRegistry::get("oxUtilsPic");
00037     }
00038 
00039 
00050     public function resizeImage( $sSrc, $sTarget, $iDesiredWidth, $iDesiredHeight )
00051     {
00052         $blResize = false;
00053 
00054         // use this GD Version
00055         if ( ( $iUseGDVersion = getGdVersion() ) && function_exists( 'imagecreate' ) &&
00056              file_exists( $sSrc ) && ( $aImageInfo = @getimagesize( $sSrc ) ) ) {
00057 
00058             $myConfig = $this->getConfig();
00059             list( $iWidth, $iHeight ) = calcImageSize( $iDesiredWidth, $iDesiredHeight, $aImageInfo[0], $aImageInfo[1] );
00060             $blResize = $this->_resize( $aImageInfo, $sSrc, null, $sTarget, $iWidth, $iHeight, $iUseGDVersion, $myConfig->getConfigParam( 'blDisableTouch' ), $myConfig->getConfigParam( 'sDefaultImageQuality' ) );
00061         }
00062         return $blResize;
00063     }
00064 
00065 
00076     public function safePictureDelete( $sPicName, $sAbsDynImageDir, $sTable, $sField )
00077     {
00078         $blDelete = false;
00079         if ( $this->_isPicDeletable( $sPicName, $sTable, $sField ) ) {
00080             $blDelete = $this->_deletePicture( $sPicName, $sAbsDynImageDir );
00081         }
00082         return $blDelete;
00083     }
00084 
00093     protected function _deletePicture( $sPicName, $sAbsDynImageDir )
00094     {
00095         $blDeleted = false;
00096         $myConfig  = $this->getConfig();
00097 
00098         if ( !$myConfig->isDemoShop() && ( strpos( $sPicName, 'nopic.jpg' ) === false ||
00099              strpos( $sPicName, 'nopic_ico.jpg' ) === false ) ) {
00100 
00101             $sFile = "$sAbsDynImageDir/$sPicName";
00102 
00103             if ( file_exists( $sFile ) && is_file( $sFile ) ) {
00104                 $blDeleted = unlink( $sFile );
00105             }
00106 
00107             if ( !$myConfig->getConfigParam( 'sAltImageUrl' ) ) {
00108                 // deleting various size generated images
00109                 $sGenPath = str_replace( '/master/', '/generated/', $sAbsDynImageDir );
00110                 foreach ( glob( "{$sGenPath}*/{$sPicName}" ) as $sFile ) {
00111                     $blDeleted = unlink( $sFile );
00112                 }
00113             }
00114         }
00115         return $blDeleted;
00116     }
00117 
00118 
00129     protected function _isPicDeletable( $sPicName, $sTable, $sField )
00130     {
00131         if ( !$sPicName || strpos( $sPicName, 'nopic.jpg' ) !== false || strpos( $sPicName, 'nopic_ico.jpg' ) !== false ) {
00132             return false;
00133         }
00134 
00135         $oDb = oxDb::getDb();
00136         $iCountUsed = $oDb->getOne( "select count(*) from $sTable where $sField = ".$oDb->quote( $sPicName ). " group by $sField ", false, false);
00137         return $iCountUsed > 1 ? false : true;
00138     }
00139 
00153     public function overwritePic( $oObject, $sPicTable, $sPicField, $sPicType, $sPicDir, $aParams, $sAbsDynImageDir )
00154     {
00155         $blDelete = false;
00156         $sPic = $sPicTable.'__'.$sPicField;
00157         if ( isset( $oObject->{$sPic} ) &&
00158              ( $_FILES['myfile']['size'][$sPicType.'@'.$sPic] > 0 || $aParams[$sPic] != $oObject->{$sPic}->value ) ) {
00159 
00160             $sImgDir = $sAbsDynImageDir . oxRegistry::get("oxUtilsFile")->getImageDirByType($sPicType);
00161             $blDelete = $this->safePictureDelete($oObject->{$sPic}->value, $sImgDir, $sPicTable, $sPicField );
00162         }
00163 
00164         return $blDelete;
00165     }
00166 
00181     protected function _resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch )
00182     {
00183         return resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch );
00184     }
00185 
00201     protected function _resize( $aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iGdVer, $blDisableTouch, $iDefQuality )
00202     {
00203         startProfile("PICTURE_RESIZE");
00204 
00205         $blSuccess = false;
00206         switch ( $aImageInfo[2] ) { //Image type
00207             case ( $this->_aImageTypes["GIF"] ):
00208                 //php does not process gifs until 7th July 2004 (see lzh licensing)
00209                 if ( function_exists( "imagegif" ) ) {
00210                     $blSuccess = resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1], $iGdVer );
00211                 }
00212                 break;
00213             case ( $this->_aImageTypes["JPEG"] ):
00214             case ( $this->_aImageTypes["JPG"] ):
00215                 $blSuccess = resizeJpeg( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage, $iDefQuality );
00216                 break;
00217             case ( $this->_aImageTypes["PNG"] ):
00218                 $blSuccess = resizePng( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage );
00219                 break;
00220         }
00221 
00222         if ( $blSuccess && !$blDisableTouch ) {
00223             @touch( $sTarget );
00224         }
00225 
00226         stopProfile("PICTURE_RESIZE");
00227 
00228         return $blSuccess;
00229     }
00230 
00245     protected function _copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch )
00246     {
00247         $blSuccess = copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer );
00248         if ( !$blDisableTouch && $blSuccess ) {
00249             @touch( $sTarget );
00250         }
00251         return $blSuccess;
00252     }
00253 
00254 }