OXID eShop CE  4.9.5
 All Classes Files Functions Variables Pages
oxutilspic.php
Go to the documentation of this file.
1 <?php
2 
6 require_once getShopBasePath() . "core/utils/oxpicgenerator.php";
7 
11 class oxUtilsPic extends oxSuperCfg
12 {
13 
19  protected $_aImageTypes = array("GIF" => IMAGETYPE_GIF, "JPG" => IMAGETYPE_JPEG, "PNG" => IMAGETYPE_PNG, "JPEG" => IMAGETYPE_JPEG);
20 
31  public function resizeImage($sSrc, $sTarget, $iDesiredWidth, $iDesiredHeight)
32  {
33  $blResize = false;
34 
35  // use this GD Version
36  if (($iUseGDVersion = getGdVersion()) && function_exists('imagecreate') &&
37  file_exists($sSrc) && ($aImageInfo = @getimagesize($sSrc))
38  ) {
39 
40  $myConfig = $this->getConfig();
41  list($iWidth, $iHeight) = calcImageSize($iDesiredWidth, $iDesiredHeight, $aImageInfo[0], $aImageInfo[1]);
42  $blResize = $this->_resize($aImageInfo, $sSrc, null, $sTarget, $iWidth, $iHeight, $iUseGDVersion, $myConfig->getConfigParam('blDisableTouch'), $myConfig->getConfigParam('sDefaultImageQuality'));
43  }
44 
45  return $blResize;
46  }
47 
48 
59  public function safePictureDelete($sPicName, $sAbsDynImageDir, $sTable, $sField)
60  {
61  $blDelete = false;
62  if ($this->_isPicDeletable($sPicName, $sTable, $sField)) {
63  $blDelete = $this->_deletePicture($sPicName, $sAbsDynImageDir);
64  }
65 
66  return $blDelete;
67  }
68 
77  protected function _deletePicture($sPicName, $sAbsDynImageDir)
78  {
79  $blDeleted = false;
80  $myConfig = $this->getConfig();
81 
82  if (!$myConfig->isDemoShop() && (strpos($sPicName, 'nopic.jpg') === false ||
83  strpos($sPicName, 'nopic_ico.jpg') === false)
84  ) {
85 
86  $sFile = "$sAbsDynImageDir/$sPicName";
87 
88  if (file_exists($sFile) && is_file($sFile)) {
89  $blDeleted = unlink($sFile);
90  }
91 
92  if (!$myConfig->getConfigParam('sAltImageUrl')) {
93  // deleting various size generated images
94  $sGenPath = str_replace('/master/', '/generated/', $sAbsDynImageDir);
95  $aFiles = glob("{$sGenPath}*/{$sPicName}");
96  if (is_array($aFiles)) {
97  foreach ($aFiles as $sFile) {
98  $blDeleted = unlink($sFile);
99  }
100  }
101  }
102  }
103 
104  return $blDeleted;
105  }
106 
107 
118  protected function _isPicDeletable($sPicName, $sTable, $sField)
119  {
120  if (!$sPicName || strpos($sPicName, 'nopic.jpg') !== false || strpos($sPicName, 'nopic_ico.jpg') !== false) {
121  return false;
122  }
123 
124  $oDb = oxDb::getDb();
125  $iCountUsed = $oDb->getOne("select count(*) from $sTable where $sField = " . $oDb->quote($sPicName) . " group by $sField ", false, false);
126 
127  return $iCountUsed > 1 ? false : true;
128  }
129 
143  public function overwritePic($oObject, $sPicTable, $sPicField, $sPicType, $sPicDir, $aParams, $sAbsDynImageDir)
144  {
145  $blDelete = false;
146  $sPic = $sPicTable . '__' . $sPicField;
147  if (isset($oObject->{$sPic}) &&
148  ($_FILES['myfile']['size'][$sPicType . '@' . $sPic] > 0 || $aParams[$sPic] != $oObject->{$sPic}->value)
149  ) {
150 
151  $sImgDir = $sAbsDynImageDir . oxRegistry::get("oxUtilsFile")->getImageDirByType($sPicType);
152  $blDelete = $this->safePictureDelete($oObject->{$sPic}->value, $sImgDir, $sPicTable, $sPicField);
153  }
154 
155  return $blDelete;
156  }
157 
172  protected function _resizeGif($sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch)
173  {
174  return resizeGif($sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch);
175  }
176 
192  protected function _resize($aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iGdVer, $blDisableTouch, $iDefQuality)
193  {
194  startProfile("PICTURE_RESIZE");
195 
196  $blSuccess = false;
197  switch ($aImageInfo[2]) { //Image type
198  case ($this->_aImageTypes["GIF"]):
199  //php does not process gifs until 7th July 2004 (see lzh licensing)
200  if (function_exists("imagegif")) {
201  $blSuccess = resizeGif($sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1], $iGdVer);
202  }
203  break;
204  case ($this->_aImageTypes["JPEG"]):
205  case ($this->_aImageTypes["JPG"]):
206  $blSuccess = resizeJpeg($sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage, $iDefQuality);
207  break;
208  case ($this->_aImageTypes["PNG"]):
209  $blSuccess = resizePng($sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage);
210  break;
211  }
212 
213  if ($blSuccess && !$blDisableTouch) {
214  @touch($sTarget);
215  }
216 
217  stopProfile("PICTURE_RESIZE");
218 
219  return $blSuccess;
220  }
221 
236  protected function _copyAlteredImage($sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch)
237  {
238  $blSuccess = copyAlteredImage($sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer);
239  if (!$blDisableTouch && $blSuccess) {
240  @touch($sTarget);
241  }
242 
243  return $blSuccess;
244  }
245 }