Jump to content


Photo

GD upload and thumbnail script


  • Please log in to reply
No replies to this topic

#1 kooper

kooper
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 15 May 2006 - 08:46 AM

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.

<?php 
//variables
$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");
    }
;

?>    

if ($2b || !$2b)
{
echo "that is the question";
}




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users