Mein Thumbnail Script

E

ExtiQ

Gast
Hi CBler,
ich habe mir vor einiger Zeit mal ein Thumbnail Script gebastelt und wollte fragen was ich daran verbessern ändern oder einfach nur einfacher machen könnte :)
Hier der code:
PHP:
<?php

###############################
## Thumbcreation script V 0.9b
##    by eXtiQ     05/31/2006
## last edit     10/15/2006
###############################



/*
*    Usage:
*    Example: thumb.php?bild=%Filename or URL to an image%
*    This will output an 100px width and propotional height sized image.
*    You can use wether .gif , .jpg or .png
*    This script returns an Image! not an textbased file [Content-Type: image/jpg]
*/

if(isset($_GET['picture']))                                                                                 ## Check, if $_GET Attribute is set
{
    $img = htmlspecialchars($_GET['picture']);
    if (!get_magic_quotes_gpc())
    {
        addslashes($img);
    }

    if (trim(substr($img,0,7)) == "http://")
    {
        DIE("Denied");
    }

    $type = getimagesize($img);
    switch($type[2])                                                                                             ## Switch the Imagetype, written in index 2
    {
        case 1 : $picture = imagecreatefromgif($img); $typ = 1;                 break;            ## gif
        case 2 :    $picture = imagecreatefromjpeg($img); $typ = 2;             break;            ## jpg
        case 3 :    $picture = imagecreatefrompng($img); $typ = 3;                 break;            ## png

        default : echo "Not a JPG/GIF/PNG file";
    }

    $width = imagesx($picture);                                                                             ## get width of the image
    $height = imagesy($picture);                                                                            ## get height of the image
    $nwidth = 100;                                                                                                ## define new width
    $nheight = round(($height / $width) * 100);                                                        ## define new height
    $thumb = imagecreate($nwidth, $nheight);                                                            ##    create new empty image
    imagecopyresized($thumb,$picture,0,0,0,0,$nwidth,$nheight,$width,$height);                ## copy thumbnail into new image

    ## Send header information to the browser ##
    header ("HTTP/1.1 200 OK", true);
    header ("Date: ". gmdate("D, d M Y H:i:s GTM"), true);
    header ("Server: Apache/2.0.55", true);
    header ("Accept-Ranges: bytes", true);

    switch($typ)
    {
        case 1 :
        {
            header ("Content-Type: image/gif", true);
            imagegif($thumb,"",100);
            break;
        }

        case 2 :
        {
            header ("Content-Type: image/jpg", true);
            imagejpeg($thumb,"",100);
            break;
        }

        case 3 :
        {
            header ("Content-Type: image/png", true);
            imagejpeg($thumb,"",100);
            break;
        }

        default : echo "Error: No Filetype";
    }


    imagedestroy($thumb);
}
?>
 
hm, naja. man kann an solchen Scripten natürlich sehr viel verbessern und erweitern, ob wirklich alles Sinn macht ist dann halt die Frage...
Eine Kleinigkeit ist mir gerade so spontan aufgefallen. Du kannst dir ein Switch sparen:

PHP:
<?php

###############################
## Thumbcreation script V 0.9b
##    by eXtiQ     05/31/2006
## last edit     10/15/2006
###############################



/*
*    Usage:
*    Example: thumb.php?bild=%Filename or URL to an image%
*    This will output an 100px width and propotional height sized image.
*    You can use wether .gif , .jpg or .png
*    This script returns an Image! not an textbased file [Content-Type: image/jpg]
*/

if(isset($_GET['picture']))                                                                                 ## Check, if $_GET Attribute is set
{
    $img = htmlspecialchars($_GET['picture']);
    if (!get_magic_quotes_gpc())
    {
        addslashes($img);
    }

    if (trim(substr($img,0,7)) == "http://")
    {
        DIE("Denied");
    }

    $width = imagesx($picture);                                                                             ## get width of the image
    $height = imagesy($picture);                                                                            ## get height of the image
    $nwidth = 100;                                                                                                ## define new width
    $nheight = round(($height / $width) * 100);                                                        ## define new height
    $thumb = imagecreate($nwidth, $nheight);                                                            ##    create new empty image
    imagecopyresized($thumb,$picture,0,0,0,0,$nwidth,$nheight,$width,$height);                ## copy thumbnail into new image

    ## Send header information to the browser ##
    header ("HTTP/1.1 200 OK", true);
    header ("Date: ". gmdate("D, d M Y H:i:s GTM"), true);
    header ("Server: Apache/2.0.55", true);
    header ("Accept-Ranges: bytes", true);

    $type = getimagesize($img);
    switch($type[2]) {
        case 1 : $picture = imagecreatefromgif($img); 
				 header ("Content-Type: image/gif", true);
				 imagegif($thumb,"",100);                 
				 break;            ## gif
        case 2 : $picture = imagecreatefromjpeg($img);
                 header ("Content-Type: image/jpg", true);
				 imagejpeg($thumb,"",100);
				 break;
        case 3 : $picture = imagecreatefrompng($img); 
				 header ("Content-Type: image/png", true);
				 imagejpeg($thumb,"",100);
				 break;
        default : echo "Not a JPG/GIF/PNG file";
    }    
    imagedestroy($thumb);
}
?>
 
nur was extrem kleines, aber is mir sofort aufgefallen... alles andere würde ich aber so lassen..

im kommentar hast du thumb.php?bild=... stehen, aber es müsste thumb.php?picture=... heißen..
 
Hmm.. so wie du es gemacht hast Jul-ian gehts nicht :) allein schon weil ich ja den imagesy/x die variable $picture übergebe, welche ich noch nicht habe :/ etc. etc.
 
Zurück
Oben