Darksat IT Security Forums
January 12, 2026, 05:32:24 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 ... 38
  Print  
Author Topic: PHP Thumbnail generation Script  (Read 17354 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:
<?phpif( ! defined( 'IMG_IP' ) ) { define( 'IMG_IP', 0 ); } // 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 = 0 )    {        if( empty( $mixed ) ) { return $this; }        if( ! $this->ImageFromFile( $mixed ) ) {             if( ! $this->ImageFromData( $mixed ) ) {                return( 0 );            }        }        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( 0 );        }        return( 1 );    }        function ImageFromData( $d )    {        if( $this->img = ImageCreateFromString( $d ) ) {            return( 1 );        }        return( 0 );    }        function save( $filename = '', $print = 0 )    {        if( empty( $filename ) ) {            if( empty( $this->filename ) ) {                return 0;             } else {                $filename = $this->filename;            }        }        //print $filename;        $filetype = substr( strrchr( $filename, '.' ), 1 );        switch( $filetype ) {             case 'jpg':            case 'jpeg':                if( ImageTypes() & IMG_JPG ) {                    if( $print ) {                        header( "Content-type: image/jpeg\n\n" );                         return ImageJPEG( $this->img, null, 60 );                    } else return ImageJPEG( $this->img, $filename, 60 );                }                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->save( 0, 1 );    }            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( 0 );            } 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->img, 0, 0, 0, 0, $dimensions[0], $dimensions[1], $width, $height);        if( empty( $copyScaled ) ) {            imageDestroy( $this->img );            $this->img = imagecreatetruecolor( $dimensions[0], $dimensions[1] );             imageCopy( $this->img, $newImg, 0, 0, 0, 0, $dimensions[0], $dimensions[1] );        } else {            $$copy = new Image( imageJpeg( $newImg ) );        }        imageDestroy( $newImg );         return( 1 );    }    function ratio( $max )    {        $width = imageSX( $this->img  );        $height = imageSY( $this->img );        if( empty( $max ) ) { return( 0 ); }        $ratio = round( ( $max / max( $width, $height ) ), 2 );        // work out interpolation (scale larger than original)        if( ! IMG_IP ) {            if( $ratio < 1 ) {                $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( $tag, 0, (strpos( $tag, ';' )?strpos($tag,';'):strlen($tag)) );        //print $url;        // load image or die                if( !$this->ImageFromFile( $url ) ) {             die( "Could not load image" );        }                $args = substr( strchr( $tag, ';' ), 1 );        // parse args        $commands = explode( ';', $args );         foreach( $commands as $command ) {            $attr = substr( $command, 0, strpos( $command, ',' ) );            $params = explode( ',', substr( strchr( $command, ',' ), 1 ) );            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_image( 0, 0, $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 Bluesky Share on Facebook

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', 0 ); } // Image interpolation if( ! defined( 'IMG_MAXSIZE' ) ) { define( 'IMG_MAXSIZE', 9999 ); } // can't be any largerclass Image{    var $data;    var $filename;    var $img;        function Image( $mixed = 0 )    {        if( empty( $mixed ) ) { return $this; }        if( ! $this->ImageFromFile( $mixed ) ) {            if( ! $this->ImageFromData( $mixed ) ) {                return( 0 );            }        }        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( 0 );        }        return( 1 );    }        function ImageFromData( $d )    {        if( $this->img = ImageCreateFromString( $d ) ) {            return( 1 );        }        return( 0 );    }        function save( $filename = '', $print = 0 )    {        if( empty( $filename ) ) {            if( empty( $this->filename ) ) {                return 0;            } else {                $filename = $this->filename;            }        }        //print $filename;        $filetype = substr( strrchr( $filename, '.' ), 1 );        switch( $filetype ) {            case 'jpg':            case 'jpeg':                if( ImageTypes() & IMG_JPG ) {                    if( $print ) {                        header( "Content-type: image/jpeg\n\n" );                        return ImageJPEG( $this->img, null, 60 );                    } else return ImageJPEG( $this->img, $filename, 60 );                }                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->save( 0, 1 );    }            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( 0 );            } 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->img, 0, 0, 0, 0, $dimensions[0], $dimensions[1], $width, $height);        if( empty( $copyScaled ) ) {            imageDestroy( $this->img );            $this->img = imagecreatetruecolor( $dimensions[0], $dimensions[1] );            imageCopy( $this->img, $newImg, 0, 0, 0, 0, $dimensions[0], $dimensions[1] );        } else {            $$copy = new Image( imageJpeg( $newImg ) );        }        imageDestroy( $newImg );        return( 1 );    }    function ratio( $max )    {        $width = imageSX( $this->img  );        $height = imageSY( $this->img );        if( empty( $max ) ) { return( 0 ); }        $ratio = round( ( $max / max( $width, $height ) ), 2 );        // work out interpolation (scale larger than original)        if( ! IMG_IP ) {            if( $ratio < 1 ) {                $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( $tag, 0, (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 = substr( strchr( $tag, ';' ), 1 );        // parse args        $commands = explode( ';', $args );        foreach( $commands as $command ) {            $attr = substr( $command, 0, strpos( $command, ',' ) );            $params = explode( ',', substr( strchr( $command, ',' ), 1 ) );            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_image( 0, 0, $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: 256678


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: 256678


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: 256678


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

Norm
Report Spam   Logged

Pages: [1] 2 3 4 ... 38
  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.044 seconds with 10 queries.