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 = 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] ) {    //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                     // fix for transparent images sets image to transparent
00295                     $imgWhite = imagecolorallocate( $hDestinationImage, 255, 255, 255 );
00296                     imagefill( $hDestinationImage, 0, 0, $imgWhite );
00297                     imagecolortransparent( $hDestinationImage, $imgWhite );
00298                     //end of fix
00299                 } else {
00300                     imagealphablending( $hDestinationImage, false );
00301                     imagesavealpha( $hDestinationImage, true );
00302                 }
00303 
00304                 if ( $this->_copyAlteredImage( $hDestinationImage, $hSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch ) ) {
00305                     imagepng( $hDestinationImage, $sTarget );
00306                     imagedestroy( $hDestinationImage );
00307                     imagedestroy( $hSourceImage );
00308                     $blSuccess = true;
00309                 }
00310                 break;
00311         }
00312 
00313         stopProfile("PICTURE_RESIZE");
00314 
00315         return $blSuccess;
00316     }
00317 
00332     protected function _copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch )
00333     {
00334         if ( $iGdVer == 1 ) {
00335             $blSuccess = imagecopyresized( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00336         } else {
00337             $blSuccess = imagecopyresampled( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00338         }
00339 
00340         if ( !$blDisableTouch && $blSuccess ) {
00341             @touch( $sTarget );
00342         }
00343 
00344         return $blSuccess;
00345     }
00346 
00347 }