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 = str_replace(".jpg", "_ico.jpg", $sFilename );
00203         $sIconName = str_replace(".gif", "_ico.gif", $sIconName );
00204         $sIconName = str_replace(".png", "_ico.png", $sIconName );
00205 
00206         return $sIconName;
00207     }
00208 
00209 
00224     protected function _resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch )
00225     {
00226         $hDestinationImage = imagecreate( $iNewWidth, $iNewHeight );
00227         $hSourceImage = imagecreatefromgif( $sSrc );
00228         $iTransparentColor = imagecolorresolve( $hSourceImage, 255, 255, 255 );
00229         $iFillColor = imagecolorresolve( $hDestinationImage, 255, 255, 255 );
00230         imagefill( $hDestinationImage, 0, 0, $iFillColor );
00231         imagecolortransparent( $hSourceImage, $iTransparentColor );
00232 
00233         if ( $iGDVer == 1 ) {
00234             imagecopyresized( $hDestinationImage, $hSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth );
00235         } else {
00236             imagecopyresampled( $hDestinationImage, $hSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth );
00237         }
00238 
00239         imagecolortransparent( $hDestinationImage, $fillColor );
00240         if ( !$blDisableTouch ) {
00241             touch( $sTarget );
00242         }
00243         imagegif( $hDestinationImage, $sTarget );
00244         imagedestroy( $hDestinationImage );
00245         imagedestroy( $hSourceImage );
00246         return true;
00247     }
00248 
00264     protected function _resize( $aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iGdVer, $blDisableTouch, $iDefQuality )
00265     {
00266         startProfile("PICTURE_RESIZE");
00267 
00268         $blSuccess = false;
00269         switch ( $aImageInfo[2] ) {    //Image type
00270             case ( $this->_aImageTypes["GIF"] ):
00271                 //php does not process gifs until 7th July 2004 (see lzh licensing)
00272                 if ( function_exists( "imagegif" ) ) {
00273                     imagedestroy( $hDestinationImage );
00274                     $blSuccess = $this->_resizeGif( $sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1], $iGdVer, $blDisableTouch );
00275                 }
00276                 break;
00277             case ( $this->_aImageTypes["JPG"] ):
00278                 $hSourceImage = imagecreatefromjpeg( $sSrc );
00279                 if ( $this->_copyAlteredImage( $hDestinationImage, $hSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch ) ) {
00280                     imagejpeg( $hDestinationImage, $sTarget, $iDefQuality );
00281                     imagedestroy( $hDestinationImage );
00282                     imagedestroy( $hSourceImage );
00283                     $blSuccess = true;
00284                 }
00285                 break;
00286             case ( $this->_aImageTypes["PNG"] ):
00287                 $hSourceImage = imagecreatefrompng( $sSrc );
00288 
00289                 if ( !imageistruecolor( $hSourceImage ) ) {
00290                     $hDestinationImage = imagecreate( $iNewWidth, $iNewHeight );
00291                 }
00292 
00293                 // fix for transparent images sets image to transparent
00294                 $imgWhite = imagecolorallocate( $hDestinationImage, 255, 255, 255 );
00295                 imagefill( $hDestinationImage, 0, 0, $imgWhite );
00296                 imagecolortransparent( $hDestinationImage, $imgWhite );
00297                 //end of fix
00298 
00299                 if ( $this->_copyAlteredImage( $hDestinationImage, $hSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch ) ) {
00300                     imagepng( $hDestinationImage, $sTarget );
00301                     imagedestroy( $hDestinationImage );
00302                     imagedestroy( $hSourceImage );
00303                     $blSuccess = true;
00304                 }
00305                 break;
00306         }
00307 
00308         stopProfile("PICTURE_RESIZE");
00309 
00310         return $blSuccess;
00311     }
00312 
00327     protected function _copyAlteredImage( $sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch )
00328     {
00329         if ( $iGdVer == 1 ) {
00330             $blSuccess = imagecopyresized( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00331         } else {
00332             $blSuccess = imagecopyresampled( $sDestinationImage, $sSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1] );
00333         }
00334 
00335         if ( !$blDisableTouch && $blSuccess ) {
00336             @touch( $sTarget );
00337         }
00338 
00339         return $blSuccess;
00340     }
00341 
00342 }

Generated on Fri Dec 19 14:20:29 2008 for OXID eShop CE by  doxygen 1.5.5