amokske
amokske

Reputation: 55

creating thumbnails on image upload - 'SOME' are black, yet others are ok

I have the following image upload script, that uploads an image, renames and resizes a thumbnail too. When uploading image (all JPG) some upload and display fine, yet others upload the main image fine but the thumbnail is just a black square.

Does anyone have any idea as to what the problem is?

[edit] I have added PNG to the filetypes, and it uploads correctly, however 'real' JPG files now upload the black thumbnail. I am thinking that I need to somehow check the file extension and apply imagecreatefromjpeg/imagecreatefrompng based on what is returned? if this is the case, i am thinking an if/else statement where highlighted will do? - not sure what to check against to get the extension (jpg/png) though...

    if(isset($_POST['submit'])){
        // get file info
        $file = $_FILES['file'];

        $fileName = $_FILES['file']['name'];
        $fileTmpName = $_FILES['file']['tmp_name'];
        $fileSize = $_FILES['file']['size'];
        $thumbName = $_FILES['file']['name']; //
        $thumbTmpName = $_FILES['file']['tmp_name']; //
        $thumbSize = $_FILES['file']['size']; //
        $fileError = $_FILES['file']['error'];
        $fileType = $_FILES['file']['type'];

        //allow file types
        $fileExt = explode('.', $fileName);
        $fileActualExt = strtolower(end($fileExt));

        $allowed = array('jpg', 'jpeg', 'png'); // ADDED PNG HERE

        if(in_array($fileActualExt, $allowed)){

            if($fileError === 0){

                if($fileSize < 1000000){
                    $fileNameNew = $row['item_img'].".".$fileActualExt; //code!
                    $fileDestination = 'images/'.$col_id.'/'.$fileNameNew; //number
                    move_uploaded_file($fileTmpName, $fileDestination);

                    // if(???){ // START POSSIBLE IF/ELSE HERE?
                    // create thumbnail
                    $src = imagecreatefromjpeg($fileDestination);
                    list($width, $height) = getimagesize($fileDestination);
                    $thumbWidth = 100;
                    $thumbHeight = 100;

                    // } else { // POSSIBLE ELSE HERE?

                    // ADDED THIS FOR PNG
                    $src = imagecreatefrompng($fileDestination);
                    list($width, $height) = getimagesize($fileDestination);
                    $thumbWidth = 100;
                    $thumbHeight = 100;

                    // } // END POSSIBLE IF/ELSE HERE?

                    $tmp = imagecreatetruecolor($thumbWidth,$thumbHeight);
                    imagecopyresampled($tmp, $src, 0,0,0,0, $thumbWidth, $thumbHeight, $width, $height);
                    imagejpeg($tmp, 'images/'.$col_id.'/thumbs/'.$fileNameNew.'', 100);

                    imagedestroy($src);
                    imagedestroy($tmp);

                    //CHECK FOR IMAGE
                    $checkImgQuery = $db->prepare("SELECT item_image.item_image, item_item.item_id FROM item_image JOIN item_item ON item_item.item_img = item_image.item_image WHERE item_id = :item_id");
                    $checkImgQuery->execute(array(':item_id' => $item_id));
                    $check = $checkImgQuery->rowCount();

                    if($check > 0){
                        // UPDATE
                    } else {
                        // ADD
                    }
                } else {
                    $error[] = 'your file is too big';
                }

            } else {
                $error[] = 'there was an error uploading your file';
            }

        } else {
            $error[] = 'you cannot upload files of this type';
        }

}

Upvotes: 0

Views: 116

Answers (1)

Salketer
Salketer

Reputation: 15711

Since it is not always happening, this is almost certain to come from the files, as they are the only thing that changes... I'm guessing your jpeg files are either corrupted (but not enough to not display the picture, but maybe make the PHP code crash) or they simply aren't real jpegs...

Here's what I'd recommend: There is no need to force the users on only one file type while PHP alone can handle multiple, so we can check the mime type and act accordingly.

switch(mime_content_type($fileDestination){
    case 'image/jpeg':
        $src = imagecreatefromjpeg($fileDestination);
        break;
    case 'image/png':
        $src = imagecreatefrompng($fileDestination);
        break;
    case 'image/gif':
        $src = imagecreatefromgif($fileDestination);
        break;
    default:
        $error[] = 'your file is not of a recognized type';
}

if(isset($src)){
   //do what you were doing.
}

So we test the mime type and use the corresponding function. Not that I did not include everything, but that should be a good start.

If you want an easier implementation, you could use this:

$src = imagecreatefromstring(file_get_content($fileDestination));

When creating an image from a string, the type is automatically detected. This would return false if the file is not recognized.

You will want to be careful with transparency when saving from PNG to JPG... But I'll leave that part to you.

Upvotes: 0

Related Questions