Jump to content


This topic is now archived and is closed to further replies.


GD upload and thumbnail script

Recommended Posts

hey, ive dabbled in php before but im still a learner

I recently made a script that would upload a file and if that file was an image you could choose to have a thumbnail made using the GD library.
problem im having is the if else which decides weather to make the thumb from a jpeg, gif or png isnt working.
the script that reads the extension seems to be working fine.
you can probably tell more from the code, so here it is.

$directory = "../../$address/";
$file = $_FILES['userfile']['name'];
$upload = "$directory". $_FILES['userfile']['name'];
$link = "http://localhost/gsm/$address/$file";
$thumbWidth = 200;
$thumbDirectory = "$directory/thumbs";

$namelength = strlen($file);
$namelength = $namelength - 3;
if(strpos($file, jpg) == $namelength){
$filetype = "jpg";
} else if(strpos($file, png) == $namelength){
$filetype = "png";
} else if(strpos($file, gif) == $namelength){
$filetype = "gif";

//upload script
if(!(copy($_FILES['userfile']['tmp_name'], "$upload"))) die("Cannot upload files.");
echo "Upload Complete!<br> <a href=\"$link\" target=\"_blank\">$link</a><br>";
echo "$filetype <br>";

//thumb script
function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth)
    //detecting type of source file
        if ( $filetype == "png") {
        $srcImg = imagecreatefrompng("$imageDirectory/$imageName");
        } else if ( $filetype == "jpg") {
        $srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
        } else if ( $filetype == "gif") {
        $srcImg = imagecreatefromgif("$imageDirectory/$imageName");
        } else {
        echo "file type not supported for thumbnailing";
        $origWidth = imagesx($srcImg);
        $origHeight = imagesy($srcImg);

        $ratio = $origWidth / $thumbWidth;
        $thumbHeight = $origHeight / $ratio;

        $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);

        imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);

        imagejpeg($thumbImg, "$thumbDirectory/$imageName");    

if($makethumb == "1" && $address == "media1")
        createThumbnail("$directory", "$file", "$thumbDirectory", "$thumbWidth");

?>    [/code]

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.