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             static $inst = array();
00032             self::$_instance = $inst[oxClassCacheKey()];
00033         }
00034 
00035         if ( !self::$_instance instanceof oxUtilsPic ) {
00036 
00037 
00038             self::$_instance = oxNew( 'oxUtilsPic' );
00039 
00040             if ( defined( 'OXID_PHP_UNIT' ) ) {
00041                 $inst[oxClassCacheKey()] = self::$_instance;
00042             }
00043         }
00044         return self::$_instance;
00045     }
00046 
00047 
00058     public function resizeImage( $sSrc, $sTarget, $iDesiredWidth, $iDesiredHeight )
00059     {
00060         $blResize = false;
00061         $myConfig = $this->getConfig();
00062 
00063         // use this GD Version
00064         if ( ( $iUseGDVersion = $myConfig->getConfigParam( 'iUseGDVersion' ) ) &&
00065                function_exists( 'imagecreate' ) && file_exists( $sSrc ) &&
00066               ( $aImageInfo = @getimagesize( $sSrc ) ) ) {
00067 
00068             // #1837/1177M - do not resize smaller pictures
00069             if ( $iDesiredWidth < $aImageInfo[0] || $iDesiredHeight < $aImageInfo[1] ) {
00070                 if ( $aImageInfo[0] >= $aImageInfo[1]*( (float) ( $iDesiredWidth / $iDesiredHeight ) ) ) {
00071                     $iNewHeight = round( ( $aImageInfo[1] * (float) ( $iDesiredWidth / $aImageInfo[0] ) ), 0 );
00072                     $iNewWidth = $iDesiredWidth;
00073                 } else {
00074                     $iNewHeight = $iDesiredHeight;
00075                     $iNewWidth = round( ( $aImageInfo[0] * (float) ( $iDesiredHeight / $aImageInfo[1] ) ), 0 );
00076                 }
00077             } else {
00078                 $iNewWidth = $aImageInfo[0];
00079                 $iNewHeight = $aImageInfo[1];
00080             }
00081 
00082             if ( $iUseGDVersion == 1) {
00083                 $hDestinationImage = imagecreate( $iNewWidth, $iNewHeight );
00084             } else {
00085                 $hDestinationImage = imagecreatetruecolor( $iNewWidth, $iNewHeight );
00086             }
00087 
00088             $blResize = $this->_resize( $aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iUseGDVersion, $myConfig->getConfigParam( 'blDisableTouch' ), $myConfig->getConfigParam( 'sDefaultImageQuality' ) );
00089         }
00090         return $blResize;
00091     }
00092 
00093 
00104     public function safePictureDelete( $sPicName, $sAbsDynImageDir, $sTable, $sField )
00105     {
00106         $blDelete = false;
00107         if ( $this->_isPicDeletable( $sPicName, $sTable, $sField ) ) {
00108             $blDelete = $this->_deletePicture( $sPicName, $sAbsDynImageDir );
00109         }
00110         return $blDelete;
00111     }
00112 
00121     protected function _deletePicture( $sPicName, $sAbsDynImageDir )
00122     {
00123         $blDeleted = false;
00124         $myConfig  = $this->getConfig();
00125 
00126         if ( !$myConfig->isDemoShop() && ( strpos( $sPicName, 'nopic.jpg' ) === false
00127                 || strpos( $sPicName, 'nopic_ico.jpg' ) === false ) ) {
00128 
00129             $sFile = "$sAbsDynImageDir/$sPicName";
00130 
00131             if ( file_exists( $sFile ) ) {
00132                 $blDeleted = unlink( $sFile );
00133             }
00134 
00135             // additionally deleting icon ..
00136             $sIconFile = preg_replace( "/(\.[a-z0-9]*$)/i", "_ico\\1", $sFile );
00137             if ( file_exists( $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 
00203     public function iconName( $sFilename )
00204     {
00205         $sIconName = preg_replace( '/(\.jpg|\.gif|\.png)$/i', '_ico\\1', $sFilename );
00206 
00207         return $sIconName;
00208     }
00209 
00210 
00225     protected function _resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch )
00226     {
00227         $hDestinationImage = imagecreate( $iNewWidth, $iNewHeight );
00228         $hSourceImage = imagecreatefromgif( $sSrc );
00229         $iTransparentColor = imagecolorresolve( $hSourceImage, 255, 255, 255 );
00230         $iFillColor = imagecolorresolve( $hDestinationImage, 255, 255, 255 );
00231         imagefill( $hDestinationImage, 0, 0, $iFillColor );
00232         imagecolortransparent( $hSourceImage, $iTransparentColor );
00233 
00234         if ( $iGDVer == 1 ) {
00235             imagecopyresized( $hDestinationImage, $hSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth );
00236         } else {
00237             imagecopyresampled( $hDestinationImage, $hSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth );
00238         }
00239 
00240         imagecolortransparent( $hDestinationImage, $fillColor );
00241         if ( !$blDisableTouch ) {
00242             touch( $sTarget );
00243         }
00244         imagegif( $hDestinationImage, $sTarget );
00245         imagedestroy( $hDestinationImage );
00246         imagedestroy( $hSourceImage );
00247         return true;
00248     }
00249 
00265     protected function _resize( $aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iGdVer, $blDisableTouch, $iDefQuality )
00266     {
00267         startProfile("PICTURE_RESIZE");
00268 
00269         $blSuccess = false;
00270         switch ( $aImageInfo[2] ) {    //Image type
00271             case ( $this->_aImageTypes["GIF"] ):
00272                 //php does not process gifs until 7th July 2004 (see lzh licensing)
00273                 if ( function_exists( "imagegif" ) ) {
00274                     imagedestroy( $hDestinationImage );
00275                     $blSuccess = $this->_resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1], $iGdVer, $blDisableTouch );
00276                 }
00277                 break;
00278             case ( $this->_aImageTypes["JPG"] ):
00279                 $hSourceImage = imagecreatefromjpeg( $sSrc );
00280                 if ( $this->_copyAlteredImage( $hDestinationImage, $hSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch ) ) {
00281                     imagejpeg( $hDestinationImage, $sTarget, $iDefQuality );
00282                     imagedestroy( $hDestinationImage );
00283                     imagedestroy( $hSourceImage );
00284                     $blSuccess = true;
00285                 }
00286                 break;
00287             case ( $this->_aImageTypes["PNG"] ):
00288                 $hSourceImage = imagecreatefrompng( $sSrc );
00289 
00290                 if ( !imageistruecolor( $hSourceImage ) ) {
00291                     $hDestinationImage = imagecreate( $iNewWidth, $iNewHeight );
00292                 }
00293 
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 
00300                 if ( $this->_copyAlteredImage( $hDestinationImage, $hSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch ) ) {
00301                     imagepng( $hDestinationImage, $sTarget );
00302                     imagedestroy( $hDestinationImage );
00303                     imagedestroy( $hSourceImage );
00304                     $blSuccess = true;
00305                 }
00306                 break;
00307         }
00308 
00309         stopProfile("PICTURE_RESIZE");
00310 
00311         return $blSuccess;
00312     }
00313 
00328     protected function _copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch )
00329     {
00330         if ( $iGdVer == 1 ) {
00331             $blSuccess = imagecopyresized( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00332         } else {
00333             $blSuccess = imagecopyresampled( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00334         }
00335 
00336         if ( !$blDisableTouch && $blSuccess ) {
00337             @touch( $sTarget );
00338         }
00339 
00340         return $blSuccess;
00341     }
00342 
00343 }

Generated on Mon Oct 26 20:07:17 2009 for OXID eShop CE by  doxygen 1.5.5