Page 1 of 1

create thumbs

Posted: Fri Mar 06, 2009 8:36 am
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.

Re: create thumbs

Posted: Fri Mar 06, 2009 10:18 am
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.