Jump to content

GD upload and thumbnail script


kooper

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.

[code]
<?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");
    }
;

?>    [/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.