BLOB images cut off

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
msurabbott
Forum Commoner
Posts: 33
Joined: Thu Jan 01, 2009 10:18 pm
Location: Chicago, IL, USA

BLOB images cut off

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: BLOB images cut off

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
msurabbott
Forum Commoner
Posts: 33
Joined: Thu Jan 01, 2009 10:18 pm
Location: Chicago, IL, USA

Re: BLOB images cut off

Post 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";
}
Attachments
Flower, half cut off..
Flower, half cut off..
displayimage.jpeg (46.77 KiB) Viewed 908 times
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: BLOB images cut off

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
msurabbott
Forum Commoner
Posts: 33
Joined: Thu Jan 01, 2009 10:18 pm
Location: Chicago, IL, USA

Re: BLOB images cut off

Post 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:
Post Reply