views:

39

answers:

1

I have this source code where I got it from net tutsplus. I have configured it and made it work in one PHP file. It does work by transferring the original image, but it does not generate to the thumbnails folder.

<?php

$final_width_of_image = 100;
$path_to_image_directory = "../../img/events/" . urldecode($_GET['name']) . "/";
$path_to_thumbs_directory = "../../img/events/" . urldecode($_GET['name']) . "/thumbnails/";

function createThumbnail($filename)
{
    if(preg_match('/[.](jpg)$/', $filename)) 
    {
        $im = imagecreatefromjpeg($path_to_image_directory . $filename);
    } 
    elseif(preg_match('/[.](gif)$/', $filename)) 
    {
        $im = imagecreatefromgif($path_to_image_directory . $filename);
    } 
    elseif(preg_match('/[.](png)$/', $filename)) 
    {
        $im = imagecreatefrompng($path_to_image_directory . $filename);
    }

    $ox = imagesx($im);
    $oy = imagesy($im);

    $nx = $final_width_of_image;
    $ny = floor($oy * ($final_width_of_image / $ox));

    $nm = imagecreatetruecolor($nx, $ny);

    imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);

    imagejpeg($nm, $path_to_thumbs_directory . $filename);
    $tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';
    echo $tn;
}

if(isset($_FILES['fupload'])) {

    if(preg_match('/[.](jpg)|(gif)|(png)$/', $_FILES['fupload']['name'])) {

        $filename = $_FILES['fupload']['name'];
        $source = $_FILES['fupload']['tmp_name'];
        $target = $path_to_image_directory . $filename;

        move_uploaded_file($source, $target);

        createThumbnail($filename);
    }
}

?>

Basically it is supposed to generate a thumbnail of the uploaded image and store the original image into a different folder.

The paths are correct, it works by getting the folder name in the URL, it does work, but nothing works for the thumbnails folder.

BEFORE you ask this related question, yes, thumbnails generation does work on my server by the PHP GD, I have tested it separately. So this is not the problem. :)

How do I get this to work? :(

+1  A: 

Well, first off, use imagecopyresampled(), as it will generate a better thumbnail.

Secondly, you shouldn't use the same variable for filesystem directory and for url directory. You should have $filesystem_path_to_thumbs and $url_path_to_thumbs. So you can set them differently.

Third, you may want to do a size check for both width and height. What happens if someone uploads a tall image? Your thumbnail will be outside the target box (but this may not be a big issue).

Fourth, you should prob do a check to see if the thumbnail file exists in the thumbs directory before generating the thumbnail (for performance reasons)...

Finally, the reason it's actually failing, is $final_width_of_image, $path_to_image_directory and $path_to_thumbs_directory are not defined within the function. Either:

  1. Make them global at the start of the function, so you can access them inside of the function global $final_width_of_image, $path_to_image_directory, $path_to_thumbs_directory;

  2. Make them arguments to the function: function createTumbnail($image, $width, $path_to_images, $path_to_thumbs) {

  3. Hard code them inside of the function (Move their declaration from outside the function to inside the function).

Personally, I'd do #2, but it's up to what your requirements and needs are...

ircmaxell
Could you edit my code the way it should supposed to be with your suggestions, I just need this done with.
YouBook
I think what I gave you should be enough to figure out what needs to be done. If you're confused on something, ask about what you are confused about.
ircmaxell