I have a PHP script attempting to store images into a MySQL table as blobs. The script runs just fine and does its job as expected. However, when trying to view the stored image from the table, I notice only the top half is getting stored; the remaining is just cut off! Here's what I mean:
Also, I noticed that the image size is getting padded up for no apparent reason while being stored as blob. The image in question (left) is originally 46kB but when downloaded off the table (right), it shows 36kB (I know BLOBs can take up to 64kB so that shouldn't be a problem). This is happening to smaller images as well.
Even funnier is the fact that the file size doesn't look consistent in PHPMyAdmin. Take a look:
This is in table-view:
This is in insert view:
What's going on? Am I doing something wrong? How is a 46kB file turning into 36 in one view and 90 in another?
As for my max_allowed_packet size, it's currently defaulted to 4194304, way above what I'm trying to store.
UPDATE Here's the PHP snippet that's inserting the image to the db:
foreach($files as $fname) {
if($fname != '.' && $fname != '..') {
rename($oldfolder.$fname, $newfolder.$fname);
$imgname = array_shift(explode(".",$fname)); //Individual image file name, without extension
echo "\n".$imgname;
$thumbph = fopen($newfolder.$fname, 'rb');
$sql = 'UPDATE distilled_contacts SET THUMB = ? WHERE PHONE = ?';
$connect = dbconn(PROJHOST,PROJDB,PROJDBUSER,PROJDBPWD);
$query = $connect->prepare($sql);
$query->bindParam(1, $thumbph, PDO::PARAM_LOB);
$query->bindParam(2, $imgname);
$query->execute();
$query = NULL;
$sql = NULL;
$connect = NULL;
}
}
The dbconn() function looks like this:
function dbconn($strHost, $strDB, $strUser, $strPass) {
try {
$conn = new PDO("mysql:host=".$strHost.";dbname=".$strDB.";charset=utf8", $strUser, $strPass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_PERSISTENT, true);
return $conn;
}
catch(PDOException $e) {
die("Could not connect to the database $dbname :" . $e->getMessage());
return false;
}
}
UPDATE: On Ryan's request, here's the table structure:
ID int(10) unsigned, auto-increment
PHONE varchar(20), utf8_unicode_ci
POPULARNAME varchar(60), utf8_unicode_ci
PREFERREDNAME varchar(60), utf8_unicode_ci
LATITUDE float(8,6)
LONGITUDE float(9,6)
LASTSEEN datetime
THUMB blob
The default collation for the database is utf8_unicode_ci
which I cannot change as I need to support non European languages.
PHP version: 5.5.20, MySQL version: 5.6.24