Darksat IT Security Forums
April 18, 2024, 10:00:58 pm
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Darksat IT Security Forum
From Firewall Support, AntiVirus Questions, Spyware problems, Linux and Windows Security, Black Hat SEO right down to Website Design and Multimedia
 
  Home Help Search Gallery Links Staff List Login Register  

PHP Thumbnail generation Script

Pages: [1] 2 3 4 ... 23
  Print  
Author Topic: PHP Thumbnail generation Script  (Read 7452 times)
Darksat
Administrator
Master
*******
Posts: 3303



View Profile WWW
« on: January 17, 2008, 05:30:09 pm »

I based this on an old beta open source script i came across years ago, managed to get it working properly with truecolour.
On the fly image generation with fairly good compression.

You call the script like so.
<img src="image.php?img=/imgs/logo.png;sm,200" border="1" alt="[Image]" align="left">


Code:
<?php

if( ! defined'IMG_IP' ) ) { define'IMG_IP'); } // Image interpolation 
if( ! defined'IMG_MAXSIZE' ) ) { define'IMG_MAXSIZE'999999999 ); } // can't be any larger 

class Image
{
    var 
$data;
    var 
$filename;
    var 
$img;
    
    function 
Image$mixed )
    {
        if( empty( 
$mixed ) ) { return $this; }
        if( ! 
$this->ImageFromFile$mixed ) ) { 
            if( ! 
$this->ImageFromData$mixed ) ) {
                return( 
);
            }
        }
        return 
$this;    
    }
    
    function 
ImageFromFile$f )
    {
        if( ( 
$fp fopen$f'rb' ) ) || ($fp fopen($_SERVER['DOCUMENT_ROOT'] . $f'rb')) ) { 
            
$this->data fread$fp, ( filesize$f ) ? filesize$f ) : IMG_MAXSIZE ) );
            
$this->filename $f;
            return 
$this->ImageFromData$this->data );
        } else { 
            return( 
);
        }
        return( 
);
    }
    
    function 
ImageFromData$d )
    {
        if( 
$this->img ImageCreateFromString$d ) ) {
            return( 
);
        }
        return( 
);
    }
    
    function 
save$filename ''$print )
    {
        if( empty( 
$filename ) ) {
            if( empty( 
$this->filename ) ) {
                return 
0
            } else {
                
$filename $this->filename;
            }
        }
        
//print $filename;
        
$filetype substrstrrchr$filename'.' ), );
        switch( 
$filetype ) { 
            case 
'jpg':
            case 
'jpeg':
                if( 
ImageTypes() & IMG_JPG ) {
                    if( 
$print ) {
                        
header"Content-type: image/jpeg\n\n" ); 
                        return 
ImageJPEG$this->imgnull60 );
                    } else return 
ImageJPEG$this->img$filename60 );
                }
                break;
            case 
'png'
                if( 
ImageTypes() & IMG_PNG ) {
                    if( 
$print ) {
                        
header"Content-type: image/png\n\n" );
                        return 
ImagePNG$this->img ); 
                    } else return 
ImagePNG$this->img$filename );
                }
                break;
            case 
'gif':
                if( 
ImageTypes() & IMG_GIF ) {
                    if( 
$print ) { 
                        
header"Content-type: image/gif\n\n" );
                        return 
ImageGIF$this->img );
                    } else return 
ImageGIF$this->img$filename );
                } 
                break;
            case 
'bmp':
                if( 
ImageTypes() & IMG_WBMP ) {
                    if( 
$print ) {
                        
header"Content-type: image/wbmp\n\n" ); 
                        return 
ImageWBMP$this->img );
                    } else return 
ImageWBMP$this->img$filename );
                }
                break;
        }
    }
    
    function 
p() 
    {
        return 
$this->save0);
    }
    
    
    function 
scale_image$newWidth 0$newHeight 0$ratio 0$copy '' )
    {
        if( ! empty( 
$copy ) ) {
            global $
$copy
        }

        
$width imageSX$this->img );
        
$height imageSY$this->img );
        if( empty( 
$newWidth ) &&  empty( $newHeight ) ) {
            
// no dimensions, check ratio 
            
if( empty( $ratio ) ) {
                
// nothing to scale to, return
                
return( );
            } else {
                
// get dimensions from ratio
                
$dimensions[] = round$width $ratio ); 
                
$dimensions[] = round$height $ratio );
            }
        } else if( empty( 
$newWidth ) || empty( $newHeight ) ) {
            
// we have one dimension, use this as the max and run through ratio() 
            
$dimensions $this->ratio( ( ! empty( $newWidth ) ? $newWidth $newHeight ) );
        } else {
            
$dimensions = array( $newWidth$newHeight );
        }
        
$newImg imagecreatetruecolor$dimensions[0], $dimensions[1] ); 
imageCopyResampled($newImg$this->img0000$dimensions[0], $dimensions[1], $width$height);
        if( empty( 
$copyScaled ) ) {
            
imageDestroy$this->img );
            
$this->img imagecreatetruecolor$dimensions[0], $dimensions[1] ); 
            
imageCopy$this->img$newImg0000$dimensions[0], $dimensions[1] );
        } else {
            $
$copy = new ImageimageJpeg$newImg ) );
        }
        
imageDestroy$newImg ); 
        return( 
);
    }

    function 
ratio$max )
    {
        
$width imageSX$this->img  );
        
$height imageSY$this->img );
        if( empty( 
$max ) ) { return( ); }
        
$ratio round( ( $max max$width$height ) ), );
        
// work out interpolation (scale larger than original)
        
if( ! IMG_IP ) {
            if( 
$ratio ) {
                
$width round$width $ratio ); 
                
$height round$height $ratio );
            }
        } else {
            
$width round$width $ratio );
            
$height round$height $ratio );
        }
        return( Array( 
$width$height$ratio ) ); 

    }
    
    function 
overlay$img$ovr$copy '' )
    {
        
    }
    
    function 
parseImgTag$tag )
    {
        
/*format '$url;$args'
            args: '$attr,$param[[,$param],$paramX];$attr,$param[[,$param],$paramX]' 
        */
        
$url substr$tag0, (strpos$tag';' )?strpos($tag,';'):strlen($tag)) );
        
//print $url;
        // load image or die
        
        
if( !$this->ImageFromFile$url ) ) { 
            die( 
"Could not load image" );
        }
        
        
$args substrstrchr$tag';' ), );
        
// parse args
        
$commands explode';'$args ); 
        foreach( 
$commands as $command ) {
            
$attr substr$command0strpos$command',' ) );
            
$params explode','substrstrchr$command',' ), ) );
            switch( 
$attr ) {
                
/* s.. Scale functions as follows
                    s,width,height -- scale to desired dimensions
                    sm,max -- scale to maximum
                    sr,ratio -- scale to ratio (relative to 1.0)
                */
                
case 's':
                    
$this->scale_image$params[0], $params[1] );
                    break;
                case 
'sm':
                    
$this->scale_image$params[0] ); 
                    break;
                case 
'sr':
                    
$this->scale_image00$params[0] );
                    break;
            }
            
        }
        
$this->p(); 
        
    }

}


/* main logic if called directly */

if($_SERVER['PATH_TRANSLATED'] == __FILE__) {
    if(isset(
$_GET['img']) && !empty($_GET['img'])) {
        
$imgO = new Image();

        
$imgO->parseImgTag(urldecode($_GET['img']));
    }
}

?>
Report Spam   Logged

Share on Facebook Share on Twitter

Darksat
Administrator
Master
*******
Posts: 3303



View Profile WWW
« Reply #1 on: January 24, 2008, 01:16:38 pm »

New version of code, now delivers a blank image if its called with incorrect image paramiters.
Also errors messages are now suppressed.

Code:
<?php
  error_reporting
(0);
if( ! 
defined'IMG_IP' ) ) { define'IMG_IP'); } // Image interpolation 
if( ! defined'IMG_MAXSIZE' ) ) { define'IMG_MAXSIZE'9999 ); } // can't be any larger

class Image
{
    var 
$data;
    var 
$filename;
    var 
$img;
    
    function 
Image$mixed )
    {
        if( empty( 
$mixed ) ) { return $this; }
        if( ! 
$this->ImageFromFile$mixed ) ) {
            if( ! 
$this->ImageFromData$mixed ) ) {
                return( 
);
            }
        }
        return 
$this;    
    }
    
    function 
ImageFromFile$f )
    {
        if( ( 
$fp fopen$f'rb' ) ) || ($fp fopen($_SERVER['DOCUMENT_ROOT'] . $f'rb')) ) {
            
$this->data fread$fp, ( filesize$f ) ? filesize$f ) : IMG_MAXSIZE ) );
            
$this->filename $f;
            return 
$this->ImageFromData$this->data );
        } else {
            return( 
);
        }
        return( 
);
    }
    
    function 
ImageFromData$d )
    {
        if( 
$this->img ImageCreateFromString$d ) ) {
            return( 
);
        }
        return( 
);
    }
    
    function 
save$filename ''$print )
    {
        if( empty( 
$filename ) ) {
            if( empty( 
$this->filename ) ) {
                return 
0;
            } else {
                
$filename $this->filename;
            }
        }
        
//print $filename;
        
$filetype substrstrrchr$filename'.' ), );
        switch( 
$filetype ) {
            case 
'jpg':
            case 
'jpeg':
                if( 
ImageTypes() & IMG_JPG ) {
                    if( 
$print ) {
                        
header"Content-type: image/jpeg\n\n" );
                        return 
ImageJPEG$this->imgnull60 );
                    } else return 
ImageJPEG$this->img$filename60 );
                }
                break;
            case 
'png':
                if( 
ImageTypes() & IMG_PNG ) {
                    if( 
$print ) {
                        
header"Content-type: image/png\n\n" );
                        return 
ImagePNG$this->img );
                    } else return 
ImagePNG$this->img$filename );
                }
                break;
            case 
'gif':
                if( 
ImageTypes() & IMG_GIF ) {
                    if( 
$print ) {
                        
header"Content-type: image/gif\n\n" );
                        return 
ImageGIF$this->img );
                    } else return 
ImageGIF$this->img$filename );
                }
                break;
            case 
'bmp':
                if( 
ImageTypes() & IMG_WBMP ) {
                    if( 
$print ) {
                        
header"Content-type: image/wbmp\n\n" );
                        return 
ImageWBMP$this->img );
                    } else return 
ImageWBMP$this->img$filename );
                }
                break;
        }
    }
    
    function 
p()
    {
        return 
$this->save0);
    }
    
    
    function 
scale_image$newWidth 0$newHeight 0$ratio 0$copy '' )
    {
        if( ! empty( 
$copy ) ) {
            global $
$copy;
        }

        
$width imageSX$this->img );
        
$height imageSY$this->img );
        if( empty( 
$newWidth ) &&  empty( $newHeight ) ) {
            
// no dimensions, check ratio
            
if( empty( $ratio ) ) {
                
// nothing to scale to, return
                
return( );
            } else {
                
// get dimensions from ratio
                
$dimensions[] = round$width $ratio );
                
$dimensions[] = round$height $ratio );
            }
        } else if( empty( 
$newWidth ) || empty( $newHeight ) ) {
            
// we have one dimension, use this as the max and run through ratio()
            
$dimensions $this->ratio( ( ! empty( $newWidth ) ? $newWidth $newHeight ) );
        } else {
            
$dimensions = array( $newWidth$newHeight );
        }

$newImg imagecreatetruecolor$dimensions[0], $dimensions[1] );
imageCopyResampled($newImg$this->img0000$dimensions[0], $dimensions[1], $width$height);
        if( empty( 
$copyScaled ) ) {
            
imageDestroy$this->img );
            
$this->img imagecreatetruecolor$dimensions[0], $dimensions[1] );
            
imageCopy$this->img$newImg0000$dimensions[0], $dimensions[1] );
        } else {
            $
$copy = new ImageimageJpeg$newImg ) );
        }
        
imageDestroy$newImg );
        return( 
);
    }

    function 
ratio$max )
    {
        
$width imageSX$this->img  );
        
$height imageSY$this->img );
        if( empty( 
$max ) ) { return( ); }
        
$ratio round( ( $max max$width$height ) ), );
        
// work out interpolation (scale larger than original)
        
if( ! IMG_IP ) {
            if( 
$ratio ) {
                
$width round$width $ratio );
                
$height round$height $ratio );
            }
        } else {
            
$width round$width $ratio );
            
$height round$height $ratio );
        }
        return( Array( 
$width$height$ratio ) );

    }
    
    function 
overlay$img$ovr$copy '' )
    {
        
    }
    
    function 
parseImgTag$tag )
    {
        
/*format '$url;$args'
            args: '$attr,$param[[,$param],$paramX];$attr,$param[[,$param],$paramX]'
        */
        
$url substr$tag0, (strpos$tag';' )?strpos($tag,';'):strlen($tag)) );
        
//print $url;
        // load image or die
        
        
if( !$this->ImageFromFile$url ) ) {
            







header'Content-Type: image/jpeg' );

$imgPath 'blank.jpg';


function 
imageCreateFromJpegEx($file)
{
    
$data file_get_contents($file);
    
$im = @imagecreatefromstring($data);
    
$i 0;
    while (!
$im)
    {
        
$data substr_replace($data"", -3, -2);
        
$im = @imagecreatefromstring($data);
    }
    return 
$im;
}

$im imageCreateFromJpegEx($imgPath);
imagejpeg$im );






        }
        
        
$args substrstrchr$tag';' ), );
        
// parse args
        
$commands explode';'$args );
        foreach( 
$commands as $command ) {
            
$attr substr$command0strpos$command',' ) );
            
$params explode','substrstrchr$command',' ), ) );
            switch( 
$attr ) {
                
/* s.. Scale functions as follows
                    s,width,height -- scale to desired dimensions
                    sm,max -- scale to maximum
                    sr,ratio -- scale to ratio (relative to 1.0)
                */
                
case 's':
                    
$this->scale_image$params[0], $params[1] );
                    break;
                case 
'sm':
                    
$this->scale_image$params[0], $params[1] );
                    break;
                case 
'sr':
                    
$this->scale_image00$params[0] );
                    break;
            }
            
        }
        
$this->p();
        
    }

}


/* main logic if called directly */

if($_SERVER['PATH_TRANSLATED'] == __FILE__) {
    if(isset(
$_GET['img']) && !empty($_GET['img'])) {
        
$imgO = new Image();

        
$imgO->parseImgTag(urldecode($_GET['img']));
    }
}

?>


Report Spam   Logged
Defcon 5
Master
*****
Posts: 2410



View Profile WWW
« Reply #2 on: January 26, 2008, 05:28:07 am »

Awsome, I did something similar a while back but I had the size fixed in a variable.
Just takes the width from a variable and then works out the height, I should get stuck into php again it was fun.

Your script is way more advanced though.
Report Spam   Logged
warscar
Master
*****
Posts: 157123


View Profile
« Reply #3 on: May 11, 2021, 01:44:43 pm »

Huh?212.95Huh?mirrHuh?ResiWillHuh?Huh?AndeMichDISCFranFlowErneGeraStepHeinHuh?TramHuh?Huh?Huh?
Huh?UnitPROFProvCreoFreeAhavSensHuh?Huh?SympWoodHuh?OreaMexxHuh?KamiSkinPureDiadHuh?SchaTimo
RobeCotoHuh?ThomAndrHuh?SockXVIINichHuh?RobeHuh?Huh?Huh?Huh?Huh?RaymHuh?MatiCircAdioSonaJame
SainBeliPathDamiDougHuh?LewiHuh?ThisXVIIHuh?Huh?LloyZoneHuh?ZoneZoneHuh?MiyodiamHuh-BernHuh?
XIIIHuh?50-2XIIIMichHuh?WorlLyriWorlHuh?RogeHenrClivHuh?RodgHuh?JuliHuh?Huh?HenrHuh?Ligh«???
Huh?Huh?Huh?CampHuh?Huh?TekaHuh?WindHuh?Huh?JardHuh?EricPoweMistRenzVALGHuh?PerfHuh?Huh?Afri
LeifHuh?Huh?Huh?Huh?Huh?Huh?WINDWindBoscHuh?BoscBorkDolcHuh?Huh?AlleHuh?picaHeinClauHuh?Huh?
Huh?Huh?Huh?Huh?Huh?Huh?Huh?GodaHuh?Papu(192Huh?CompHuh?FranAndrHuh?Huh?Huh?JameDailHuh?Aaro
XVIIHuh?Huh?Huh?Huh?Huh?PISAHuh?Huh?Huh?Huh?Huh?Huh?JohnKessHuh?Huh?Huh?Huh?IntrHuh?CampCamp
CampHuh?PROMContHuh?Huh?RobbHuh?Huh?Huh?AlfrHuh?KlautuchkasHuh?Huh?
Report Spam   Logged
warscar
Master
*****
Posts: 157123


View Profile
« Reply #4 on: October 08, 2021, 04:12:11 pm »

Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?
Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?
Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?
Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?
Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?
Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?
Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?
Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?
Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?
Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?Huh?tuchkasHuh?Huh?
Report Spam   Logged
warscar
Master
*****
Posts: 157123


View Profile
« Reply #5 on: November 06, 2021, 04:42:25 pm »

STAL
Report Spam   Logged
warscar
Master
*****
Posts: 157123


View Profile
« Reply #6 on: November 06, 2021, 04:43:36 pm »

215.1
Report Spam   Logged
warscar
Master
*****
Posts: 157123


View Profile
« Reply #7 on: November 06, 2021, 04:44:47 pm »

TREA
Report Spam   Logged
warscar
Master
*****
Posts: 157123


View Profile
« Reply #8 on: November 06, 2021, 04:45:57 pm »

When
Report Spam   Logged
warscar
Master
*****
Posts: 157123


View Profile
« Reply #9 on: November 06, 2021, 04:47:08 pm »

Prop
Report Spam   Logged
warscar
Master
*****
Posts: 157123


View Profile
« Reply #10 on: November 06, 2021, 04:48:18 pm »

Mich
Report Spam   Logged
warscar
Master
*****
Posts: 157123


View Profile
« Reply #11 on: November 06, 2021, 04:49:29 pm »

Fred
Report Spam   Logged
warscar
Master
*****
Posts: 157123


View Profile
« Reply #12 on: November 06, 2021, 04:50:39 pm »

Jule
Report Spam   Logged
warscar
Master
*****
Posts: 157123


View Profile
« Reply #13 on: November 06, 2021, 04:51:50 pm »

Demi
Report Spam   Logged
warscar
Master
*****
Posts: 157123


View Profile
« Reply #14 on: November 06, 2021, 04:53:00 pm »

Norm
Report Spam   Logged

Pages: [1] 2 3 4 ... 23
  Print  
 
Jump to:  

Powered by EzPortal
eXTReMe Tracker
Security Forum
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum


Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy
Page created in 0.079 seconds with 17 queries.