tags:

views:

518

answers:

6

A client has just asked me to fix an issue with their webside. I did not build it but here is what's happening.

The images are stored in the db as longblobs. Before moving to a new server, everything was working a ok. On the new server, some images only display partially. Even though the image is displayed at desired width and height, over a half of the image is either white or gray. EDIT: Only images uploaded after the server change are affected!

This is used for reading the image into a string for db insertion:

move_uploaded_file($_FILES['imagefile1']['tmp_name'],"tmppic.img")) 
$tmpstr = fopen("tmppic.img","rb");
$image = addslashes(fread($tmpstr,filesize("tmppic.img")));

Here are the PHP functions used for displaying the images: imagecreatefromstring, imagecreatetruecolor, imagecopyresampled, imagejpeg

Since this has started happening after the server move, I am suspecting server configuration.

What is going on?

+3  A: 

Dunno if it's what's killing you, but you definitely need to change the addslashes() to mysql_real_escape_string(). SQL injection ahoy...

chaos
A: 

It may be possible the data got corrupted during the server move - especially if the data was dumped and then re-inserted. Can you confirm the entire image data is actually in the database?

Edit: Just saw your edit to the question. Can you confirm that the new images are being completely inserted into the database then? It could be an insertion problem.

Jarod Elliott
A: 

If the images were transferred to the new server via ftp, it's possible that the transfer mode was not set correctly (i.e. ascii vs. binary). If you transfer a binary file in ascii ftp mode, it may interpret some of the bytes in the image as new-lines and try to convert them to windows (CRLF) or UNIX(LF), etc mode. This will usually corrupt the image so it isn't totally readable.

Dana the Sane
A: 

What MySQL library are you using? Certain versions of the MySQLi library have issues correctly retrieving blobs from the database.

I would verify that you are running the same versions of PHP and the relevant MySQL library on both servers.

Noah Goodrich
+2  A: 

Ok, figured it out. Looking at the code below.

if (move_uploaded_file($_FILES['imagefile1']['tmp_name'],"tmppic.img")){
$tmpstr = fopen("tmppic.img","rb");
$image = addslashes(fread($tmpstr,filesize("tmppic.img")));

The uploaded file is moved into the tmppic.img file. That file is constantly used. The problem is that filesize("tmppic.img") is returning the size of the file before move_uploaded_file. So, if the previous tmppic.img had a 120k filesize and the new uploaded file is larger, only 120K worth of info is read, thus the gray area in the pictures.

I fixed the issue by replacing filesize("tmppic.img") with

$file_size = $_FILES['imagefile1']['size'];
$image = mysql_real_escape_string(fread($tmpstr,$file_size));

Is this a bug?

pistolshrimp