00001 <?php
00002
00006 class oxUtilsPic extends oxSuperCfg
00007 {
00013 protected $_aImageTypes = array("GIF" => IMAGETYPE_GIF, "JPG" => IMAGETYPE_JPEG, "PNG" => IMAGETYPE_PNG, "JPEG" => IMAGETYPE_JPEG);
00014
00020 private static $_instance = null;
00021
00027 public static function getInstance()
00028 {
00029
00030 if ( defined( 'OXID_PHP_UNIT' ) ) {
00031 self::$_instance = modInstances::getMod( __CLASS__ );
00032 }
00033
00034 if ( !self::$_instance instanceof oxUtilsPic ) {
00035
00036
00037 self::$_instance = oxNew( 'oxUtilsPic' );
00038
00039 if ( defined( 'OXID_PHP_UNIT' ) ) {
00040 modInstances::addMod( __CLASS__, self::$_instance);
00041 }
00042 }
00043 return self::$_instance;
00044 }
00045
00046
00057 public function resizeImage( $sSrc, $sTarget, $iDesiredWidth, $iDesiredHeight )
00058 {
00059 $blResize = false;
00060 $myConfig = $this->getConfig();
00061
00062
00063 if ( ( $iUseGDVersion = $myConfig->getConfigParam( 'iUseGDVersion' ) ) &&
00064 function_exists( 'imagecreate' ) && file_exists( $sSrc ) &&
00065 ( $aImageInfo = @getimagesize( $sSrc ) ) ) {
00066
00067
00068 if ( $iDesiredWidth < $aImageInfo[0] || $iDesiredHeight < $aImageInfo[1] ) {
00069 if ( $aImageInfo[0] >= $aImageInfo[1]*( (float) ( $iDesiredWidth / $iDesiredHeight ) ) ) {
00070 $iNewHeight = round( ( $aImageInfo[1] * (float) ( $iDesiredWidth / $aImageInfo[0] ) ), 0 );
00071 $iNewWidth = $iDesiredWidth;
00072 } else {
00073 $iNewHeight = $iDesiredHeight;
00074 $iNewWidth = round( ( $aImageInfo[0] * (float) ( $iDesiredHeight / $aImageInfo[1] ) ), 0 );
00075 }
00076 } else {
00077 $iNewWidth = $aImageInfo[0];
00078 $iNewHeight = $aImageInfo[1];
00079 }
00080
00081 if ( $iUseGDVersion == 1) {
00082 $hDestinationImage = imagecreate( $iNewWidth, $iNewHeight );
00083 } else {
00084 $hDestinationImage = imagecreatetruecolor( $iNewWidth, $iNewHeight );
00085 }
00086
00087 $blResize = $this->_resize( $aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iUseGDVersion, $myConfig->getConfigParam( 'blDisableTouch' ), $myConfig->getConfigParam( 'sDefaultImageQuality' ) );
00088 }
00089 return $blResize;
00090 }
00091
00092
00103 public function safePictureDelete( $sPicName, $sAbsDynImageDir, $sTable, $sField )
00104 {
00105 $blDelete = false;
00106 if ( $this->_isPicDeletable( $sPicName, $sTable, $sField ) ) {
00107 $blDelete = $this->_deletePicture( $sPicName, $sAbsDynImageDir );
00108 }
00109 return $blDelete;
00110 }
00111
00120 protected function _deletePicture( $sPicName, $sAbsDynImageDir )
00121 {
00122 $blDeleted = false;
00123 $myConfig = $this->getConfig();
00124
00125 if ( !$myConfig->isDemoShop() && ( strpos( $sPicName, 'nopic.jpg' ) === false
00126 || strpos( $sPicName, 'nopic_ico.jpg' ) === false ) ) {
00127
00128 $sFile = "$sAbsDynImageDir/$sPicName";
00129
00130 if ( file_exists( $sFile ) && is_file( $sFile ) ) {
00131 $blDeleted = unlink( $sFile );
00132 }
00133
00134
00135 $sIconFile = getStr()->preg_replace( "/(\.[a-z0-9]*$)/i", "_ico\\1", $sFile );
00136
00137 if ( file_exists( $sIconFile ) && is_file( $sIconFile ) ) {
00138 unlink( $sIconFile );
00139 }
00140 }
00141 return $blDeleted;
00142 }
00143
00144
00155 protected function _isPicDeletable( $sPicName, $sTable, $sField )
00156 {
00157 if ( !$sPicName || strpos( $sPicName, 'nopic.jpg' ) !== false || strpos( $sPicName, 'nopic_ico.jpg' ) !== false ) {
00158 return false;
00159 }
00160
00161 $iCountUsed = oxDb::getDb()->getOne( "select count(*) from $sTable where $sField = '$sPicName' group by $sField " );
00162
00163 if ( $iCountUsed > 1) {
00164 return false;
00165 }
00166 return true;
00167 }
00168
00182 public function overwritePic( $oObject, $sPicTable, $sPicField, $sPicType, $sPicDir, $aParams, $sAbsDynImageDir )
00183 {
00184 $blDelete = false;
00185 $sPic = $sPicTable.'__'.$sPicField;
00186 if ( isset( $oObject->{$sPic} ) &&
00187 ( $_FILES['myfile']['size'][$sPicType.'@'.$sPic] > 0 || $aParams[$sPic] != $oObject->{$sPic}->value ) ) {
00188
00189 $sImgDir = $sAbsDynImageDir . $sPicDir;
00190 $blDelete = $this->safePictureDelete($oObject->{$sPic}->value, $sImgDir, $sPicTable, $sPicField );
00191 }
00192
00193 return $blDelete;
00194 }
00195
00206 public function iconName( $sFilename )
00207 {
00208 $oPictureHandler = oxPictureHandler::getInstance();
00209 return $oPictureHandler->getIconName( $sFilename );
00210 }
00211
00212
00227 protected function _resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch )
00228 {
00229 $hDestinationImage = imagecreate( $iNewWidth, $iNewHeight );
00230 $hSourceImage = imagecreatefromgif( $sSrc );
00231 $iTransparentColor = imagecolorresolve( $hSourceImage, 255, 255, 255 );
00232 $iFillColor = imagecolorresolve( $hDestinationImage, 255, 255, 255 );
00233 imagefill( $hDestinationImage, 0, 0, $iFillColor );
00234 imagecolortransparent( $hSourceImage, $iTransparentColor );
00235
00236 if ( $iGDVer == 1 ) {
00237 imagecopyresized( $hDestinationImage, $hSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth );
00238 } else {
00239 imagecopyresampled( $hDestinationImage, $hSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth );
00240 }
00241
00242 imagecolortransparent( $hDestinationImage, $fillColor );
00243 if ( !$blDisableTouch ) {
00244 touch( $sTarget );
00245 }
00246 imagegif( $hDestinationImage, $sTarget );
00247 imagedestroy( $hDestinationImage );
00248 imagedestroy( $hSourceImage );
00249 return true;
00250 }
00251
00267 protected function _resize( $aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iGdVer, $blDisableTouch, $iDefQuality )
00268 {
00269 startProfile("PICTURE_RESIZE");
00270
00271 $blSuccess = false;
00272 switch ( $aImageInfo[2] ) {
00273 case ( $this->_aImageTypes["GIF"] ):
00274
00275 if ( function_exists( "imagegif" ) ) {
00276 imagedestroy( $hDestinationImage );
00277 $blSuccess = $this->_resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1], $iGdVer, $blDisableTouch );
00278 }
00279 break;
00280 case ( $this->_aImageTypes["JPEG"] ):
00281 case ( $this->_aImageTypes["JPG"] ):
00282 $hSourceImage = imagecreatefromjpeg( $sSrc );
00283 if ( $this->_copyAlteredImage( $hDestinationImage, $hSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch ) ) {
00284 imagejpeg( $hDestinationImage, $sTarget, $iDefQuality );
00285 imagedestroy( $hDestinationImage );
00286 imagedestroy( $hSourceImage );
00287 $blSuccess = true;
00288 }
00289 break;
00290 case ( $this->_aImageTypes["PNG"] ):
00291 $hSourceImage = imagecreatefrompng( $sSrc );
00292
00293 if ( !imageistruecolor( $hSourceImage ) ) {
00294 $hDestinationImage = imagecreate( $iNewWidth, $iNewHeight );
00295
00296 $imgWhite = imagecolorallocate( $hDestinationImage, 255, 255, 255 );
00297 imagefill( $hDestinationImage, 0, 0, $imgWhite );
00298 imagecolortransparent( $hDestinationImage, $imgWhite );
00299
00300 } else {
00301 imagealphablending( $hDestinationImage, false );
00302 imagesavealpha( $hDestinationImage, true );
00303 }
00304
00305 if ( $this->_copyAlteredImage( $hDestinationImage, $hSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch ) ) {
00306 imagepng( $hDestinationImage, $sTarget );
00307 imagedestroy( $hDestinationImage );
00308 imagedestroy( $hSourceImage );
00309 $blSuccess = true;
00310 }
00311 break;
00312 }
00313
00314 stopProfile("PICTURE_RESIZE");
00315
00316 return $blSuccess;
00317 }
00318
00333 protected function _copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch )
00334 {
00335 if ( $iGdVer == 1 ) {
00336 $blSuccess = imagecopyresized( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00337 } else {
00338 $blSuccess = imagecopyresampled( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00339 }
00340
00341 if ( !$blDisableTouch && $blSuccess ) {
00342 @touch( $sTarget );
00343 }
00344
00345 return $blSuccess;
00346 }
00347
00348 }