OXID eShop CE  4.8.12
 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 {
18  protected $_aImageTypes = array("GIF" => IMAGETYPE_GIF, "JPG" => IMAGETYPE_JPEG, "PNG" => IMAGETYPE_PNG, "JPEG" => IMAGETYPE_JPEG);
19 
25  private static $_instance = null;
26 
34  public static function getInstance()
35  {
36  return oxRegistry::get("oxUtilsPic");
37  }
38 
39 
50  public function resizeImage( $sSrc, $sTarget, $iDesiredWidth, $iDesiredHeight )
51  {
52  $blResize = false;
53 
54  // use this GD Version
55  if ( ( $iUseGDVersion = getGdVersion() ) && function_exists( 'imagecreate' ) &&
56  file_exists( $sSrc ) && ( $aImageInfo = @getimagesize( $sSrc ) ) ) {
57 
58  $myConfig = $this->getConfig();
59  list( $iWidth, $iHeight ) = calcImageSize( $iDesiredWidth, $iDesiredHeight, $aImageInfo[0], $aImageInfo[1] );
60  $blResize = $this->_resize( $aImageInfo, $sSrc, null, $sTarget, $iWidth, $iHeight, $iUseGDVersion, $myConfig->getConfigParam( 'blDisableTouch' ), $myConfig->getConfigParam( 'sDefaultImageQuality' ) );
61  }
62  return $blResize;
63  }
64 
65 
76  public function safePictureDelete( $sPicName, $sAbsDynImageDir, $sTable, $sField )
77  {
78  $blDelete = false;
79  if ( $this->_isPicDeletable( $sPicName, $sTable, $sField ) ) {
80  $blDelete = $this->_deletePicture( $sPicName, $sAbsDynImageDir );
81  }
82  return $blDelete;
83  }
84 
93  protected function _deletePicture( $sPicName, $sAbsDynImageDir )
94  {
95  $blDeleted = false;
96  $myConfig = $this->getConfig();
97 
98  if ( !$myConfig->isDemoShop() && ( strpos( $sPicName, 'nopic.jpg' ) === false ||
99  strpos( $sPicName, 'nopic_ico.jpg' ) === false ) ) {
100 
101  $sFile = "$sAbsDynImageDir/$sPicName";
102 
103  if ( file_exists( $sFile ) && is_file( $sFile ) ) {
104  $blDeleted = unlink( $sFile );
105  }
106 
107  if ( !$myConfig->getConfigParam( 'sAltImageUrl' ) ) {
108  // deleting various size generated images
109  $sGenPath = str_replace( '/master/', '/generated/', $sAbsDynImageDir );
110  $aFiles = glob( "{$sGenPath}*/{$sPicName}" );
111  if ( is_array($aFiles) ) {
112  foreach ( $aFiles as $sFile ) {
113  $blDeleted = unlink( $sFile );
114  }
115  }
116  }
117  }
118  return $blDeleted;
119  }
120 
121 
132  protected function _isPicDeletable( $sPicName, $sTable, $sField )
133  {
134  if ( !$sPicName || strpos( $sPicName, 'nopic.jpg' ) !== false || strpos( $sPicName, 'nopic_ico.jpg' ) !== false ) {
135  return false;
136  }
137 
138  $oDb = oxDb::getDb();
139  $iCountUsed = $oDb->getOne( "select count(*) from $sTable where $sField = ".$oDb->quote( $sPicName ). " group by $sField ", false, false);
140  return $iCountUsed > 1 ? false : true;
141  }
142 
156  public function overwritePic( $oObject, $sPicTable, $sPicField, $sPicType, $sPicDir, $aParams, $sAbsDynImageDir )
157  {
158  $blDelete = false;
159  $sPic = $sPicTable.'__'.$sPicField;
160  if ( isset( $oObject->{$sPic} ) &&
161  ( $_FILES['myfile']['size'][$sPicType.'@'.$sPic] > 0 || $aParams[$sPic] != $oObject->{$sPic}->value ) ) {
162 
163  $sImgDir = $sAbsDynImageDir . oxRegistry::get("oxUtilsFile")->getImageDirByType($sPicType);
164  $blDelete = $this->safePictureDelete($oObject->{$sPic}->value, $sImgDir, $sPicTable, $sPicField );
165  }
166 
167  return $blDelete;
168  }
169 
184  protected function _resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch )
185  {
186  return resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch );
187  }
188 
204  protected function _resize( $aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iGdVer, $blDisableTouch, $iDefQuality )
205  {
206  startProfile("PICTURE_RESIZE");
207 
208  $blSuccess = false;
209  switch ( $aImageInfo[2] ) { //Image type
210  case ( $this->_aImageTypes["GIF"] ):
211  //php does not process gifs until 7th July 2004 (see lzh licensing)
212  if ( function_exists( "imagegif" ) ) {
213  $blSuccess = resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1], $iGdVer );
214  }
215  break;
216  case ( $this->_aImageTypes["JPEG"] ):
217  case ( $this->_aImageTypes["JPG"] ):
218  $blSuccess = resizeJpeg( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage, $iDefQuality );
219  break;
220  case ( $this->_aImageTypes["PNG"] ):
221  $blSuccess = resizePng( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage );
222  break;
223  }
224 
225  if ( $blSuccess && !$blDisableTouch ) {
226  @touch( $sTarget );
227  }
228 
229  stopProfile("PICTURE_RESIZE");
230 
231  return $blSuccess;
232  }
233 
248  protected function _copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch )
249  {
250  $blSuccess = copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer );
251  if ( !$blDisableTouch && $blSuccess ) {
252  @touch( $sTarget );
253  }
254  return $blSuccess;
255  }
256 
257 }