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
00210 protected function _resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch )
00211 {
00212 $hDestinationImage = imagecreate( $iNewWidth, $iNewHeight );
00213 $hSourceImage = imagecreatefromgif( $sSrc );
00214 $iTransparentColor = imagecolorresolve( $hSourceImage, 255, 255, 255 );
00215 $iFillColor = imagecolorresolve( $hDestinationImage, 255, 255, 255 );
00216 imagefill( $hDestinationImage, 0, 0, $iFillColor );
00217 imagecolortransparent( $hSourceImage, $iTransparentColor );
00218
00219 if ( $iGDVer == 1 ) {
00220 imagecopyresized( $hDestinationImage, $hSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth );
00221 } else {
00222 imagecopyresampled( $hDestinationImage, $hSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth );
00223 }
00224
00225 imagecolortransparent( $hDestinationImage, $fillColor );
00226 if ( !$blDisableTouch ) {
00227 touch( $sTarget );
00228 }
00229 imagegif( $hDestinationImage, $sTarget );
00230 imagedestroy( $hDestinationImage );
00231 imagedestroy( $hSourceImage );
00232 return true;
00233 }
00234
00250 protected function _resize( $aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iGdVer, $blDisableTouch, $iDefQuality )
00251 {
00252 startProfile("PICTURE_RESIZE");
00253
00254 $blSuccess = false;
00255 switch ( $aImageInfo[2] ) {
00256 case ( $this->_aImageTypes["GIF"] ):
00257
00258 if ( function_exists( "imagegif" ) ) {
00259 imagedestroy( $hDestinationImage );
00260 $blSuccess = $this->_resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1], $iGdVer, $blDisableTouch );
00261 }
00262 break;
00263 case ( $this->_aImageTypes["JPEG"] ):
00264 case ( $this->_aImageTypes["JPG"] ):
00265 $hSourceImage = imagecreatefromjpeg( $sSrc );
00266 if ( $this->_copyAlteredImage( $hDestinationImage, $hSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch ) ) {
00267 imagejpeg( $hDestinationImage, $sTarget, $iDefQuality );
00268 imagedestroy( $hDestinationImage );
00269 imagedestroy( $hSourceImage );
00270 $blSuccess = true;
00271 }
00272 break;
00273 case ( $this->_aImageTypes["PNG"] ):
00274 $hSourceImage = imagecreatefrompng( $sSrc );
00275
00276 if ( !imageistruecolor( $hSourceImage ) ) {
00277 $hDestinationImage = imagecreate( $iNewWidth, $iNewHeight );
00278
00279 $imgWhite = imagecolorallocate( $hDestinationImage, 255, 255, 255 );
00280 imagefill( $hDestinationImage, 0, 0, $imgWhite );
00281 imagecolortransparent( $hDestinationImage, $imgWhite );
00282
00283 } else {
00284 imagealphablending( $hDestinationImage, false );
00285 imagesavealpha( $hDestinationImage, true );
00286 }
00287
00288 if ( $this->_copyAlteredImage( $hDestinationImage, $hSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch ) ) {
00289 imagepng( $hDestinationImage, $sTarget );
00290 imagedestroy( $hDestinationImage );
00291 imagedestroy( $hSourceImage );
00292 $blSuccess = true;
00293 }
00294 break;
00295 }
00296
00297 stopProfile("PICTURE_RESIZE");
00298
00299 return $blSuccess;
00300 }
00301
00316 protected function _copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch )
00317 {
00318 if ( $iGdVer == 1 ) {
00319 $blSuccess = imagecopyresized( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00320 } else {
00321 $blSuccess = imagecopyresampled( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00322 }
00323
00324 if ( !$blDisableTouch && $blSuccess ) {
00325 @touch( $sTarget );
00326 }
00327
00328 return $blSuccess;
00329 }
00330
00331 }