Page 1 of 1

BLOB images cut off

Posted: Sun Feb 01, 2009 11:42 pm
by msurabbott
I have a script that uploads user submitted images for their profile image, the script works great minus the fact that it cuts the image off short.

The entire image is stored but the majority of it is just white space.

Here is the code that is doing the storing for me:

Code: Select all

        if(is_uploaded_file($data['userfile']['tmp_name'])) {
 
            // validate the image size
            if($data['userfile']['size'] < $data['MAX_FILE_SIZE'])
            {
                // pull image contents from uploaded image
                $imgData = chunk_split(base64_encode(file_get_contents($data['userfile']['tmp_name'])));
                
                $sql =  "INSERT INTO $this->table (name, content_type, contents, size) " . 
                        "VALUES(" . 
                        "'" . $data['userfile']['name'] . "', " . 
                        "'" . $data['userfile']['type'] . "', " . 
                        "'" . $imgData . "', " . 
                        "'" . $data['userfile']['size'] . "')";
                        
                // run the query, pass false to avoid logging as the image data will make
                // the log file's size too large over time
                $this->db->query($sql, false);
                
                return $this->db->insertId();
            }
            else {
                throw new Exception("File exceeds the maximum size limit of " . $data['MAX_FILE_SIZE'] . ".");
            }
        }
Anyone have a similar problem, or know what my problem might be?

Re: BLOB images cut off

Posted: Mon Feb 02, 2009 11:00 am
by pickle
Are you sure the whole image is stored? It sounds like the image is too large for the field, so some of the data gets truncated.

Re: BLOB images cut off

Posted: Mon Feb 02, 2009 11:14 am
by msurabbott
The column type is blob.. no restrictions on it..

When i display the image, it knows the dimensions of the image, because itll push content down, and when you save the image from the site as JPG and view it, the majority of it is just white

I have attached an image uploaded using this script.

Here is the code that displays it as well..

Code: Select all

// validate the image id provided
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
    $imageDS = new ImageDataSource();
    
    // retrieve the image
    $image = $imageDS->getImage($_GET['id']);
    
    // set the image header
    header('content-type: ' . $image->contentType);
    
    // display the image contents
    echo base64_decode($image->contents);
}
else {
    // id is invalid
    echo "Invalid ID";
}

Re: BLOB images cut off

Posted: Tue Feb 03, 2009 3:19 pm
by pickle
The dimensions of a jpg file are stored in the meta information (I believe), so all the data about the picture is downloaded first - that's why the size is accurate. BLOB types do have limitations - that's why there are MEDIUMBLOB and LONGBLOB types as well.

BLOB columns can only hold 65,536 bytes (65 KB). Half your image is 47KB, so it looks like the full thing wouldn't have fit. Additionally, there is a maximum on the amount of data that can be transmitted through an SQL query, so that might be restricting the data being retrieved.

http://dev.mysql.com/doc/refman/5.0/en/blob.html: look at the last bullet point before the user comments.

Re: BLOB images cut off

Posted: Sun Feb 08, 2009 10:25 am
by msurabbott
Well I changed to a longblob and it worked. What gets me is that I was using this SAME database table for this application when I started it in RoR and it worked just fine.. then when I started writing to that same exact table using PHP its too small.. odd :crazy: