OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxpicturehandler.php
Go to the documentation of this file.
1 <?php
2 
7 {
13  private static $_instance = null;
14 
22  public static function getInstance()
23  {
24  return oxRegistry::get("oxPictureHandler");
25  }
26 
38  public function deleteArticleMasterPicture( $oObject, $iIndex, $blDeleteMasterPicture = true )
39  {
40  $myConfig = $this->getConfig();
41  $myUtilsPic = oxRegistry::get("oxUtilsPic");
42  $oUtilsFile = oxRegistry::get("oxUtilsFile");
43  $blGeneratedImagesOnly = !$blDeleteMasterPicture;
44 
45  $sAbsDynImageDir = $myConfig->getPictureDir(false);
46  $sMasterImage = basename( $oObject->{"oxarticles__oxpic".$iIndex}->value );
47  if ( !$sMasterImage || $sMasterImage == "nopic.jpg" ) {
48  return;
49  }
50 
51  $aPic = array("sField" => "oxpic".$iIndex,
52  "sDir" => $oUtilsFile->getImageDirByType( "M".$iIndex, $blGeneratedImagesOnly ),
53  "sFileName" => $sMasterImage);
54 
55  $blDeleted = $myUtilsPic->safePictureDelete( $aPic["sFileName"], $sAbsDynImageDir . $aPic["sDir"], "oxarticles", $aPic["sField"] );
56  if ( $blDeleted ) {
57  $this->deleteZoomPicture( $oObject, $iIndex );
58 
59  $aDelPics = array();
60  if ( $iIndex == 1 ) {
61  // deleting generated main icon picture if custom main icon
62  // file name not equal with generated from master picture
63  if ( $this->getMainIconName( $sMasterImage ) != basename($oObject->oxarticles__oxicon->value) ) {
64  $aDelPics[] = array("sField" => "oxpic1",
65  "sDir" => $oUtilsFile->getImageDirByType( "ICO", $blGeneratedImagesOnly ),
66  "sFileName" => $this->getMainIconName( $sMasterImage ));
67  }
68 
69  // deleting generated thumbnail picture if custom thumbnail
70  // file name not equal with generated from master picture
71  if ( $this->getThumbName( $sMasterImage ) != basename($oObject->oxarticles__oxthumb->value) ) {
72  $aDelPics[] = array("sField" => "oxpic1",
73  "sDir" => $oUtilsFile->getImageDirByType( "TH", $blGeneratedImagesOnly ),
74  "sFileName" => $this->getThumbName( $sMasterImage ));
75  }
76  }
77 
78  foreach ( $aDelPics as $aPic ) {
79  $myUtilsPic->safePictureDelete( $aPic["sFileName"], $sAbsDynImageDir . $aPic["sDir"], "oxarticles", $aPic["sField"] );
80  }
81  }
82 
83  //deleting custom zoom pic (compatibility mode)
84  if ( $oObject->{"oxarticles__oxzoom".$iIndex}->value ) {
85  if ( basename($oObject->{"oxarticles__oxzoom".$iIndex}->value) !== "nopic.jpg" ) {
86  // deleting old zoom picture
87  $this->deleteZoomPicture( $oObject, $iIndex );
88  }
89  }
90 
91  }
92 
100  public function deleteMainIcon( $oObject )
101  {
102  if ( ( $sMainIcon = $oObject->oxarticles__oxicon->value ) ) {
103  $sPath = $this->getConfig()->getPictureDir( false ) . oxRegistry::get("oxUtilsFile")->getImageDirByType( "ICO" );
104  oxRegistry::get("oxUtilsPic")->safePictureDelete( $sMainIcon, $sPath, "oxarticles", "oxicon" );
105  }
106  }
107 
115  public function deleteThumbnail( $oObject )
116  {
117  if ( ( $sThumb = $oObject->oxarticles__oxthumb->value ) ) {
118  // deleting article main icon and thumb picture
119  $sPath = $this->getConfig()->getPictureDir( false ) . oxRegistry::get("oxUtilsFile")->getImageDirByType( "TH" );
120  oxRegistry::get("oxUtilsPic")->safePictureDelete( $sThumb, $sPath, "oxarticles", "oxthumb" );
121  }
122  }
123 
132  public function deleteZoomPicture( $oObject, $iIndex )
133  {
134  // checking if oxzoom field exists
135  $oDbHandler = oxNew( "oxDbMetaDataHandler" );
136  $iZoomPicCount = (int) $this->getConfig()->getConfigParam( 'iZoomPicCount' );
137 
138  if ( $iIndex > $iZoomPicCount || !$oDbHandler->fieldExists( "oxzoom".$iIndex, "oxarticles" ) ) {
139  if ( $sZoomPicName = $this->getZoomName( $oObject->{"oxarticles__oxpic".$iIndex}->value, $iIndex ) ) {
140  $sFieldToCheck = "oxpic".$iIndex;
141  } else {
142  return;
143  }
144  } else {
145  $sZoomPicName = basename( $oObject->{"oxarticles__oxzoom".$iIndex}->value );
146  $sFieldToCheck = "oxzoom".$iIndex;
147  }
148 
149  if ( $sZoomPicName && $sZoomPicName != "nopic.jpg" ) {
150  // deleting zoom picture
151  $sPath = $this->getConfig()->getPictureDir( false ) . oxRegistry::get("oxUtilsFile")->getImageDirByType( "Z" . $iIndex );
152  oxRegistry::get("oxUtilsPic")->safePictureDelete( $sZoomPicName, $sPath, "oxarticles", $sFieldToCheck );
153  }
154  }
155 
163  public function getIconName( $sFilename )
164  {
165  return $sFilename;
166  }
167 
175  public function getMainIconName( $sMasterImageFile )
176  {
177  return $this->_getBaseMasterImageFileName( $sMasterImageFile );
178  }
179 
187  public function getThumbName( $sMasterImageFile )
188  {
189  return basename( $sMasterImageFile );
190  }
191 
200  public function getZoomName( $sMasterImageFile, $iIndex )
201  {
202  return basename( $sMasterImageFile );
203  }
204 
212  protected function _getBaseMasterImageFileName( $sMasterImageFile )
213  {
214  return basename( $sMasterImageFile );
215  }
216 
225  public function getImageSize($aImgSizes, $sIndex = null)
226  {
227  if (isset($sIndex) && is_array($aImgSizes) && isset($aImgSizes[$sIndex])) {
228  $aSize = explode('*', $aImgSizes[$sIndex]);
229  } elseif (is_string ($aImgSizes)) {
230  $aSize = explode('*', $aImgSizes);
231  }
232  if (2 == count($aSize)) {
233  $x = (int)$aSize[0];
234  $y = (int)$aSize[1];
235  if ($x && $y) {
236  return $aSize;
237  }
238  }
239  return null;
240  }
241 
254  protected function _getPictureInfo( $sFilePath, $sFile, $blAdmin = false, $blSSL = null, $iLang = null, $iShopId = null )
255  {
256  // custom server as image storage?
257  if ( $sAltUrl = $this->getAltImageUrl($sFilePath, $sFile, $blSSL) ) {
258  return array( 'path' => false, 'url'=> $sAltUrl );
259  }
260 
261  $oConfig = $this->getConfig();
262  $sPath = $oConfig->getPicturePath( $sFilePath . $sFile, $blAdmin, $iLang, $iShopId );
263  if ( !$sPath ) {
264  return array( 'path'=> false, 'url' => false );
265  }
266 
267  $sDirPrefix = $oConfig->getOutDir();
268  $sUrlPrefix = $oConfig->getOutUrl( $blSSL, $blAdmin, $oConfig->getConfigParam( 'blNativeImages' ) );
269 
270  return array( 'path' => $sPath, 'url'=> str_replace( $sDirPrefix, $sUrlPrefix, $sPath ) );
271  }
272 
282  public function getAltImageUrl($sFilePath, $sFile, $blSSL = null)
283  {
284  $oConfig = $this->getConfig();
285 
286  $sAltUrl = $oConfig->getConfigParam('sAltImageUrl');
287  if ( !$sAltUrl ) {
288  $sAltUrl = $oConfig->getConfigParam('sAltImageDir');
289  }
290 
291  if ( $sAltUrl ) {
292  if ( (is_null($blSSL) && $oConfig->isSsl()) || $blSSL) {
293 
294  $sSslAltUrl = $oConfig->getConfigParam('sSSLAltImageUrl');
295  if ( !$sSslAltUrl ) {
296  $sSslAltUrl = $oConfig->getConfigParam('sSSLAltImageDir');
297  }
298 
299  if ( $sSslAltUrl ) {
300  $sAltUrl = $sSslAltUrl;
301  }
302  }
303 
304  if ( !is_null( $sFile ) ) {
305  $sAltUrl .= '/'.$sFilePath.$sFile;
306  }
307  }
308 
309  return $sAltUrl;
310  }
311 
324  public function getPicUrl( $sPath, $sFile, $sSize, $sIndex = null, $sAltPath = false, $bSsl = null )
325  {
326  $sUrl = null;
327  if ( $sPath && $sFile && ( $aSize = $this->getImageSize( $sSize, $sIndex ) ) ) {
328 
329  $aPicInfo = $this->_getPictureInfo( "master/" . ( $sAltPath ? $sAltPath : $sPath ), $sFile, $this->isAdmin(), $bSsl );
330  if ( $aPicInfo['url'] && $aSize[0] && $aSize[1] ) {
331  $sDirName = "{$aSize[0]}_{$aSize[1]}_" . $this->getConfig()->getConfigParam( 'sDefaultImageQuality' );
332  $sUrl = str_replace( "/master/" . ( $sAltPath ? $sAltPath : $sPath ), "/generated/{$sPath}{$sDirName}/", $aPicInfo['url'] );
333  }
334  }
335  return $sUrl;
336  }
337 
349  public function getProductPicUrl( $sPath, $sFile, $sSize, $sIndex = null, $bSsl = null )
350  {
351  $sUrl = null;
352  if ( !$sFile || !( $sUrl = $this->getPicUrl( $sPath, $sFile, $sSize, $sIndex, false, $bSsl ) ) ) {
353  $sUrl = $this->getPicUrl( $sPath, "nopic.jpg", $sSize, $sIndex, "/", $bSsl );
354  }
355  return $sUrl;
356  }
357 }