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
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
00109 $sGenPath = str_replace( '/master/', '/generated/', $sAbsDynImageDir );
00110 $aFiles = glob( "{$sGenPath}*/{$sPicName}" );
00111 if ( is_array($aFiles) ) {
00112 foreach ( $aFiles as $sFile ) {
00113 $blDeleted = unlink( $sFile );
00114 }
00115 }
00116 }
00117 }
00118 return $blDeleted;
00119 }
00120
00121
00132 protected function _isPicDeletable( $sPicName, $sTable, $sField )
00133 {
00134 if ( !$sPicName || strpos( $sPicName, 'nopic.jpg' ) !== false || strpos( $sPicName, 'nopic_ico.jpg' ) !== false ) {
00135 return false;
00136 }
00137
00138 $oDb = oxDb::getDb();
00139 $iCountUsed = $oDb->getOne( "select count(*) from $sTable where $sField = ".$oDb->quote( $sPicName ). " group by $sField ", false, false);
00140 return $iCountUsed > 1 ? false : true;
00141 }
00142
00156 public function overwritePic( $oObject, $sPicTable, $sPicField, $sPicType, $sPicDir, $aParams, $sAbsDynImageDir )
00157 {
00158 $blDelete = false;
00159 $sPic = $sPicTable.'__'.$sPicField;
00160 if ( isset( $oObject->{$sPic} ) &&
00161 ( $_FILES['myfile']['size'][$sPicType.'@'.$sPic] > 0 || $aParams[$sPic] != $oObject->{$sPic}->value ) ) {
00162
00163 $sImgDir = $sAbsDynImageDir . oxRegistry::get("oxUtilsFile")->getImageDirByType($sPicType);
00164 $blDelete = $this->safePictureDelete($oObject->{$sPic}->value, $sImgDir, $sPicTable, $sPicField );
00165 }
00166
00167 return $blDelete;
00168 }
00169
00184 protected function _resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch )
00185 {
00186 return resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch );
00187 }
00188
00204 protected function _resize( $aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iGdVer, $blDisableTouch, $iDefQuality )
00205 {
00206 startProfile("PICTURE_RESIZE");
00207
00208 $blSuccess = false;
00209 switch ( $aImageInfo[2] ) {
00210 case ( $this->_aImageTypes["GIF"] ):
00211
00212 if ( function_exists( "imagegif" ) ) {
00213 $blSuccess = resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1], $iGdVer );
00214 }
00215 break;
00216 case ( $this->_aImageTypes["JPEG"] ):
00217 case ( $this->_aImageTypes["JPG"] ):
00218 $blSuccess = resizeJpeg( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage, $iDefQuality );
00219 break;
00220 case ( $this->_aImageTypes["PNG"] ):
00221 $blSuccess = resizePng( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage );
00222 break;
00223 }
00224
00225 if ( $blSuccess && !$blDisableTouch ) {
00226 @touch( $sTarget );
00227 }
00228
00229 stopProfile("PICTURE_RESIZE");
00230
00231 return $blSuccess;
00232 }
00233
00248 protected function _copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch )
00249 {
00250 $blSuccess = copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer );
00251 if ( !$blDisableTouch && $blSuccess ) {
00252 @touch( $sTarget );
00253 }
00254 return $blSuccess;
00255 }
00256
00257 }