oxutilspic.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxUtilsPic extends oxSuperCfg
00007 {
00013     protected $_aImageTypes = array("GIF" => IMAGETYPE_GIF, "JPG" => IMAGETYPE_JPEG, "PNG" => IMAGETYPE_PNG);
00014 
00020     private static $_instance = null;
00021 
00027     public static function getInstance()
00028     {
00029         // disable caching for test modules
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         // use this GD Version
00063         if ( ( $iUseGDVersion = $myConfig->getConfigParam( 'iUseGDVersion' ) ) &&
00064                function_exists( 'imagecreate' ) && file_exists( $sSrc ) &&
00065               ( $aImageInfo = @getimagesize( $sSrc ) ) ) {
00066 
00067             // #1837/1177M - do not resize smaller pictures
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             // additionally deleting icon ..
00135             $sIconFile = 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] ) {    //Image type
00273             case ( $this->_aImageTypes["GIF"] ):
00274                 //php does not process gifs until 7th July 2004 (see lzh licensing)
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["JPG"] ):
00281                 $hSourceImage = imagecreatefromjpeg( $sSrc );
00282                 if ( $this->_copyAlteredImage( $hDestinationImage, $hSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch ) ) {
00283                     imagejpeg( $hDestinationImage, $sTarget, $iDefQuality );
00284                     imagedestroy( $hDestinationImage );
00285                     imagedestroy( $hSourceImage );
00286                     $blSuccess = true;
00287                 }
00288                 break;
00289             case ( $this->_aImageTypes["PNG"] ):
00290                 $hSourceImage = imagecreatefrompng( $sSrc );
00291 
00292                 if ( !imageistruecolor( $hSourceImage ) ) {
00293                     $hDestinationImage = imagecreate( $iNewWidth, $iNewHeight );
00294                 }
00295 
00296                 // fix for transparent images sets image to transparent
00297                 $imgWhite = imagecolorallocate( $hDestinationImage, 255, 255, 255 );
00298                 imagefill( $hDestinationImage, 0, 0, $imgWhite );
00299                 imagecolortransparent( $hDestinationImage, $imgWhite );
00300                 //end of fix
00301 
00302                 if ( $this->_copyAlteredImage( $hDestinationImage, $hSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch ) ) {
00303                     imagepng( $hDestinationImage, $sTarget );
00304                     imagedestroy( $hDestinationImage );
00305                     imagedestroy( $hSourceImage );
00306                     $blSuccess = true;
00307                 }
00308                 break;
00309         }
00310 
00311         stopProfile("PICTURE_RESIZE");
00312 
00313         return $blSuccess;
00314     }
00315 
00330     protected function _copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch )
00331     {
00332         if ( $iGdVer == 1 ) {
00333             $blSuccess = imagecopyresized( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00334         } else {
00335             $blSuccess = imagecopyresampled( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00336         }
00337 
00338         if ( !$blDisableTouch && $blSuccess ) {
00339             @touch( $sTarget );
00340         }
00341 
00342         return $blSuccess;
00343     }
00344 
00345 }

Generated by  doxygen 1.6.2