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             if ( file_exists( $sFile ) ) {
00131                 $blDeleted = unlink( $sFile );
00132             }
00133 
00134             // additionally deleting icon ..
00135             $sIconFile = eregi_replace( "(\.[a-z0-9]*$)", "_ico\\1", $sFile );
00136             if ( file_exists( $sIconFile ) ) {
00137                 unlink( $sIconFile );
00138             }
00139         }
00140         return $blDeleted;
00141     }
00142 
00143 
00154     protected function _isPicDeletable( $sPicName, $sTable, $sField )
00155     {
00156         if ( !$sPicName || strpos( $sPicName, 'nopic.jpg' ) !== false || strpos( $sPicName, 'nopic_ico.jpg' ) !== false ) {
00157             return false;
00158         }
00159 
00160         $iCountUsed = oxDb::getDb()->getOne( "select count(*) from $sTable where $sField = '$sPicName' group by $sField " );
00161 
00162         if ( $iCountUsed > 1) {
00163             return false;
00164         }
00165         return true;
00166     }
00167 
00181     public function overwritePic( $oObject, $sPicTable, $sPicField, $sPicType, $sPicDir, $aParams, $sAbsDynImageDir )
00182     {
00183         $blDelete = false;
00184         $sPic = $sPicTable.'__'.$sPicField;
00185         if ( isset( $oObject->{$sPic} ) &&
00186              ( $_FILES['myfile']['size'][$sPicType.'@'.$sPic] > 0 || $aParams[$sPic] != $oObject->{$sPic}->value ) ) {
00187             $blDelete = $this->safePictureDelete($oObject->{$sPic}->value, $sAbsDynImageDir, $sPicTable, $sPicField );
00188         }
00189 
00190         return $blDelete;
00191     }
00192 
00200     public function iconName( $sFilename )
00201     {
00202         $sIconName = eregi_replace( '(\.jpg|\.gif|\.png)$', '_ico\\1', $sFilename );
00203 
00204         return $sIconName;
00205     }
00206 
00207 
00222     protected function _resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch )
00223     {
00224         $hDestinationImage = imagecreate( $iNewWidth, $iNewHeight );
00225         $hSourceImage = imagecreatefromgif( $sSrc );
00226         $iTransparentColor = imagecolorresolve( $hSourceImage, 255, 255, 255 );
00227         $iFillColor = imagecolorresolve( $hDestinationImage, 255, 255, 255 );
00228         imagefill( $hDestinationImage, 0, 0, $iFillColor );
00229         imagecolortransparent( $hSourceImage, $iTransparentColor );
00230 
00231         if ( $iGDVer == 1 ) {
00232             imagecopyresized( $hDestinationImage, $hSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth );
00233         } else {
00234             imagecopyresampled( $hDestinationImage, $hSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth );
00235         }
00236 
00237         imagecolortransparent( $hDestinationImage, $fillColor );
00238         if ( !$blDisableTouch ) {
00239             touch( $sTarget );
00240         }
00241         imagegif( $hDestinationImage, $sTarget );
00242         imagedestroy( $hDestinationImage );
00243         imagedestroy( $hSourceImage );
00244         return true;
00245     }
00246 
00262     protected function _resize( $aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iGdVer, $blDisableTouch, $iDefQuality )
00263     {
00264         startProfile("PICTURE_RESIZE");
00265 
00266         $blSuccess = false;
00267         switch ( $aImageInfo[2] ) {    //Image type
00268             case ( $this->_aImageTypes["GIF"] ):
00269                 //php does not process gifs until 7th July 2004 (see lzh licensing)
00270                 if ( function_exists( "imagegif" ) ) {
00271                     imagedestroy( $hDestinationImage );
00272                     $blSuccess = $this->_resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1], $iGdVer, $blDisableTouch );
00273                 }
00274                 break;
00275             case ( $this->_aImageTypes["JPG"] ):
00276                 $hSourceImage = imagecreatefromjpeg( $sSrc );
00277                 if ( $this->_copyAlteredImage( $hDestinationImage, $hSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch ) ) {
00278                     imagejpeg( $hDestinationImage, $sTarget, $iDefQuality );
00279                     imagedestroy( $hDestinationImage );
00280                     imagedestroy( $hSourceImage );
00281                     $blSuccess = true;
00282                 }
00283                 break;
00284             case ( $this->_aImageTypes["PNG"] ):
00285                 $hSourceImage = imagecreatefrompng( $sSrc );
00286 
00287                 if ( !imageistruecolor( $hSourceImage ) ) {
00288                     $hDestinationImage = imagecreate( $iNewWidth, $iNewHeight );
00289                 }
00290 
00291                 // fix for transparent images sets image to transparent
00292                 $imgWhite = imagecolorallocate( $hDestinationImage, 255, 255, 255 );
00293                 imagefill( $hDestinationImage, 0, 0, $imgWhite );
00294                 imagecolortransparent( $hDestinationImage, $imgWhite );
00295                 //end of fix
00296 
00297                 if ( $this->_copyAlteredImage( $hDestinationImage, $hSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch ) ) {
00298                     imagepng( $hDestinationImage, $sTarget );
00299                     imagedestroy( $hDestinationImage );
00300                     imagedestroy( $hSourceImage );
00301                     $blSuccess = true;
00302                 }
00303                 break;
00304         }
00305 
00306         stopProfile("PICTURE_RESIZE");
00307 
00308         return $blSuccess;
00309     }
00310 
00325     protected function _copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch )
00326     {
00327         if ( $iGdVer == 1 ) {
00328             $blSuccess = imagecopyresized( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00329         } else {
00330             $blSuccess = imagecopyresampled( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00331         }
00332 
00333         if ( !$blDisableTouch && $blSuccess ) {
00334             @touch( $sTarget );
00335         }
00336 
00337         return $blSuccess;
00338     }
00339 
00340 }

Generated on Wed May 13 13:25:51 2009 for OXID eShop CE by  doxygen 1.5.5