OXID eShop CE  4.10.2
 All Classes Namespaces Files Functions Variables Pages
oxpicturehandler.php
Go to the documentation of this file.
1 <?php
2 
7 {
8 
20  public function deleteArticleMasterPicture($oObject, $iIndex, $blDeleteMasterPicture = true)
21  {
22  $myConfig = $this->getConfig();
23  $myUtilsPic = oxRegistry::get("oxUtilsPic");
24  $oUtilsFile = oxRegistry::get("oxUtilsFile");
25  $blGeneratedImagesOnly = !$blDeleteMasterPicture;
26 
27  $sAbsDynImageDir = $myConfig->getPictureDir(false);
28  $sMasterImage = basename($oObject->{"oxarticles__oxpic" . $iIndex}->value);
29  if (!$sMasterImage || $sMasterImage == "nopic.jpg") {
30  return;
31  }
32 
33  $aPic = array("sField" => "oxpic" . $iIndex,
34  "sDir" => $oUtilsFile->getImageDirByType("M" . $iIndex, $blGeneratedImagesOnly),
35  "sFileName" => $sMasterImage);
36 
37  $blDeleted = $myUtilsPic->safePictureDelete($aPic["sFileName"], $sAbsDynImageDir . $aPic["sDir"], "oxarticles", $aPic["sField"]);
38  if ($blDeleted) {
39  $this->deleteZoomPicture($oObject, $iIndex);
40 
41  $aDelPics = array();
42  if ($iIndex == 1) {
43  // deleting generated main icon picture if custom main icon
44  // file name not equal with generated from master picture
45  if ($this->getMainIconName($sMasterImage) != basename($oObject->oxarticles__oxicon->value)) {
46  $aDelPics[] = array("sField" => "oxpic1",
47  "sDir" => $oUtilsFile->getImageDirByType("ICO", $blGeneratedImagesOnly),
48  "sFileName" => $this->getMainIconName($sMasterImage));
49  }
50 
51  // deleting generated thumbnail picture if custom thumbnail
52  // file name not equal with generated from master picture
53  if ($this->getThumbName($sMasterImage) != basename($oObject->oxarticles__oxthumb->value)) {
54  $aDelPics[] = array("sField" => "oxpic1",
55  "sDir" => $oUtilsFile->getImageDirByType("TH", $blGeneratedImagesOnly),
56  "sFileName" => $this->getThumbName($sMasterImage));
57  }
58  }
59 
60  foreach ($aDelPics as $aPic) {
61  $myUtilsPic->safePictureDelete($aPic["sFileName"], $sAbsDynImageDir . $aPic["sDir"], "oxarticles", $aPic["sField"]);
62  }
63  }
64 
65  //deleting custom zoom pic (compatibility mode)
66  if ($oObject->{"oxarticles__oxzoom" . $iIndex}->value) {
67  if (basename($oObject->{"oxarticles__oxzoom" . $iIndex}->value) !== "nopic.jpg") {
68  // deleting old zoom picture
69  $this->deleteZoomPicture($oObject, $iIndex);
70  }
71  }
72 
73  }
74 
80  public function deleteMainIcon($oObject)
81  {
82  if (($sMainIcon = $oObject->oxarticles__oxicon->value)) {
83  $sPath = $this->getConfig()->getPictureDir(false) . oxRegistry::get("oxUtilsFile")->getImageDirByType("ICO");
84  oxRegistry::get("oxUtilsPic")->safePictureDelete($sMainIcon, $sPath, "oxarticles", "oxicon");
85  }
86  }
87 
93  public function deleteThumbnail($oObject)
94  {
95  if (($sThumb = $oObject->oxarticles__oxthumb->value)) {
96  // deleting article main icon and thumb picture
97  $sPath = $this->getConfig()->getPictureDir(false) . oxRegistry::get("oxUtilsFile")->getImageDirByType("TH");
98  oxRegistry::get("oxUtilsPic")->safePictureDelete($sThumb, $sPath, "oxarticles", "oxthumb");
99  }
100  }
101 
110  public function deleteZoomPicture($oObject, $iIndex)
111  {
112  // checking if oxzoom field exists
113  $oDbHandler = oxNew("oxDbMetaDataHandler");
114  $iZoomPicCount = (int) $this->getConfig()->getConfigParam('iZoomPicCount');
115 
116  if ($iIndex > $iZoomPicCount || !$oDbHandler->fieldExists("oxzoom" . $iIndex, "oxarticles")) {
117  if ($sZoomPicName = $this->getZoomName($oObject->{"oxarticles__oxpic" . $iIndex}->value, $iIndex)) {
118  $sFieldToCheck = "oxpic" . $iIndex;
119  } else {
120  return;
121  }
122  } else {
123  $sZoomPicName = basename($oObject->{"oxarticles__oxzoom" . $iIndex}->value);
124  $sFieldToCheck = "oxzoom" . $iIndex;
125  }
126 
127  if ($sZoomPicName && $sZoomPicName != "nopic.jpg") {
128  // deleting zoom picture
129  $sPath = $this->getConfig()->getPictureDir(false) . oxRegistry::get("oxUtilsFile")->getImageDirByType("Z" . $iIndex);
130  oxRegistry::get("oxUtilsPic")->safePictureDelete($sZoomPicName, $sPath, "oxarticles", $sFieldToCheck);
131  }
132  }
133 
141  public function getIconName($sFilename)
142  {
143  return $sFilename;
144  }
145 
153  public function getMainIconName($sMasterImageFile)
154  {
155  return $this->_getBaseMasterImageFileName($sMasterImageFile);
156  }
157 
165  public function getThumbName($sMasterImageFile)
166  {
167  return basename($sMasterImageFile);
168  }
169 
178  public function getZoomName($sMasterImageFile, $iIndex)
179  {
180  return basename($sMasterImageFile);
181  }
182 
190  protected function _getBaseMasterImageFileName($sMasterImageFile)
191  {
192  return basename($sMasterImageFile);
193  }
194 
203  public function getImageSize($aImgSizes, $sIndex = null)
204  {
205  if (isset($sIndex) && is_array($aImgSizes) && isset($aImgSizes[$sIndex])) {
206  $aSize = explode('*', $aImgSizes[$sIndex]);
207  } elseif (is_string($aImgSizes)) {
208  $aSize = explode('*', $aImgSizes);
209  }
210  if (2 == count($aSize)) {
211  $x = (int) $aSize[0];
212  $y = (int) $aSize[1];
213  if ($x && $y) {
214  return $aSize;
215  }
216  }
217 
218  return null;
219  }
220 
233  protected function _getPictureInfo($sFilePath, $sFile, $blAdmin = false, $blSSL = null, $iLang = null, $iShopId = null)
234  {
235  // custom server as image storage?
236  if ($sAltUrl = $this->getAltImageUrl($sFilePath, $sFile, $blSSL)) {
237  return array('path' => false, 'url' => $sAltUrl);
238  }
239 
240  $oConfig = $this->getConfig();
241  $sPath = $oConfig->getPicturePath($sFilePath . $sFile, $blAdmin, $iLang, $iShopId);
242  if (!$sPath) {
243  return array('path' => false, 'url' => false);
244  }
245 
246  $sDirPrefix = $oConfig->getOutDir();
247  $sUrlPrefix = $oConfig->getOutUrl($blSSL, $blAdmin, $oConfig->getConfigParam('blNativeImages'));
248 
249  return array('path' => $sPath, 'url' => str_replace($sDirPrefix, $sUrlPrefix, $sPath));
250  }
251 
261  public function getAltImageUrl($sFilePath, $sFile, $blSSL = null)
262  {
263  $oConfig = $this->getConfig();
264 
265  $sAltUrl = $oConfig->getConfigParam('sAltImageUrl');
266  if (!$sAltUrl) {
267  $sAltUrl = $oConfig->getConfigParam('sAltImageDir');
268  }
269 
270  if ($sAltUrl) {
271  if ((is_null($blSSL) && $oConfig->isSsl()) || $blSSL) {
272 
273  $sSslAltUrl = $oConfig->getConfigParam('sSSLAltImageUrl');
274  if (!$sSslAltUrl) {
275  $sSslAltUrl = $oConfig->getConfigParam('sSSLAltImageDir');
276  }
277 
278  if ($sSslAltUrl) {
279  $sAltUrl = $sSslAltUrl;
280  }
281  }
282 
283  if (!is_null($sFile)) {
284  $sAltUrl = oxRegistry::getUtils()->checkUrlEndingSlash($sAltUrl) . $sFilePath . $sFile;
285  }
286  }
287 
288  return $sAltUrl;
289  }
290 
303  public function getPicUrl($sPath, $sFile, $sSize, $sIndex = null, $sAltPath = false, $bSsl = null)
304  {
305  $sUrl = null;
306  if ($sPath && $sFile && ($aSize = $this->getImageSize($sSize, $sIndex))) {
307 
308  $aPicInfo = $this->_getPictureInfo("master/" . ($sAltPath ? $sAltPath : $sPath), $sFile, $this->isAdmin(), $bSsl);
309  if ($aPicInfo['url'] && $aSize[0] && $aSize[1]) {
310  $sDirName = "{$aSize[0]}_{$aSize[1]}_" . $this->getConfig()->getConfigParam('sDefaultImageQuality');
311  $sUrl = str_replace("/master/" . ($sAltPath ? $sAltPath : $sPath), "/generated/{$sPath}{$sDirName}/", $aPicInfo['url']);
312  }
313  }
314 
315  return $sUrl;
316  }
317 
329  public function getProductPicUrl($sPath, $sFile, $sSize, $sIndex = null, $bSsl = null)
330  {
331  $sUrl = null;
332  if (!$sFile || !($sUrl = $this->getPicUrl($sPath, $sFile, $sSize, $sIndex, false, $bSsl))) {
333  $sUrl = $this->getPicUrl($sPath, "nopic.jpg", $sSize, $sIndex, "/", $bSsl);
334  }
335 
336  return $sUrl;
337  }
338 }