create thumbs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pat78
Forum Newbie
Posts: 1
Joined: Fri Mar 06, 2009 8:29 am

create thumbs

Post by pat78 »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello. I got the following code trying to make a thumbnail. I works if i dont set height and getimagesize for width only but i want to set the height as well. I changed the following script and the thumb is correct in dimension but it goes black. What to do ?

Code: Select all

 
<?php
$final_width_of_image = 100;
$thumbheight = 100;
$path_to_image_directory = 'images/fullsized/';
$path_to_thumbs_directory = 'images/thumbs/';
 
if (isset($_FILES['myphoto'])) {
    
    if (preg_match('/[.](jpg)|(JPG)|(JEPG)|(gif)|(png)$/', $_FILES['myphoto']['name'])) {
        
        $filename = $_FILES['myphoto']['name'];
        $source = $_FILES['myphoto']['tmp_name'];
        $target = $path_to_image_directory . $filename;
 
        move_uploaded_file($source, $target);
 
        if (preg_match('/[.](jpg)$/', $filename)) {
            $myimage = imagecreatefromjpeg($path_to_image_directory . $filename);
        } else
            if (preg_match('/[.](gif)$/', $filename)) {
                $myimage = imagecreatefromgif($path_to_image_directory . $filename);
            } else
                if (preg_match('/[.](png)$/', $filename)) {
                    $myimage = imagecreatefrompng($path_to_image_directory . $filename);
                }
        $dimension = getimagesize($myimage);
        #$width = imagesx($myimage);
        #$height = imagesy($myimage);
 
        #$finalwidth = $final_width_of_image;
        #$newyvalue = floor($height * ($finalwidth / $width));
        $color = imagecreatetruecolor($final_width_of_image, $thumbheight);
 
        imagecopyresampled($color, $myimage, 0, 0, 0, 0, $final_width_of_image, $thumbheight, $dimension[0], $dimension[1]);
 
        if (!file_exists($path_to_thumbs_directory)) {
            if (!mkdir($path_to_thumbs_directory)) {
                die("There was a problem. Please try again!");
                
            }
        }
 
        imagejpeg($color, $path_to_thumbs_directory . $filename);
        $thumb = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';
        #$thumb .= '<br />Congratulations. Your file has been successfully uploaded, and a      thumbnail has been created.';
       # echo $thumb;
    }
} //isset
?>

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: create thumbs

Post by pickle »

Images have a black background by default, until another colour is "painted" overtop of it. If your image is black, I'd suspect your imagecopyresampled() call isn't copying the image properly.

Also, check your regex - you want to check for "JPEG", not "JEPG".

You should also be using getimagesize() to determine the MIME type of the image, and use that to determine which imagecreatefrom*() function to use. Searching for a string in the filename can very easily be spoofed.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply