I'm having problem with images becoming corrupted after loading them into a
MySQL database and extracting them again. I'm pretty sure it's todo with
escaping slashes but I'm at a loss as to how to confirm this and find a
solution.
A piece of test code is below, the import script displays the image before
it goes in and then uses a second script to display it straight from the
database as well as extracting it to a file and displaying that. It's
scruffy as it's stripped from a larger system but it works (or more
accurately breaks!) Imagemagick is used for the scaling operation.
The script can be seen at http://geoffsoper.co.uk/test/admin/import_test.php
Some configuration settings that may be relevant are:
Local Master
magic_quotes_gpc Off On
magic_quotes_runtime Off Off
magic_quotes_sybase Off Off
Please ask if you need to know any other PHP configuration settings.
I'd be very grateful for any clues as to what is going on.
Many thanks,
Geoff
<?php
$new_display_size = 640;
include('xxxxxxxx/test.cfg');
if ($config_file_included != true)
{
die('Configuration file not found.');
}
include($admin_inc_dir . 'db.inc');
include($admin_inc_dir . 'misc.inc');
/* Connecting, selecting database */
$link = mysql_connect($db_host, $db_user, $db_password)
or die("Could not connect : " . mysql_error());
mysql_select_db($db_database) or die("Could not select database");
$input_path = "xxxxxxxxx/039_35a.jpg";
$display_path = "xxxxxxxxx/display.jpg";
$output_path = "xxxxxxxxx/output.jpg";
echo "convert -sample 640x427 $input_path $display_path<br>";
exec ("convert -sample 640x427 $input_path $display_path");
echo"Original:<br><img
src=\"http://geoffsoper.co.uk/test/upload/039_35a.jpg\"><br>";
echo"Imagemagick raw:<br><img
src=\"http://geoffsoper.co.uk/test/upload/test/raw.jpg\"><br>";
$display_s = filesize($display_path);
$display_handle = fopen($display_path, "r");
$display_content = fread($display_handle, $display_s);
$display_content = addslashes($display_content);
$sql = "INSERT INTO `t_photos` (`type`, `display_content`) ";
$sql .= "VALUES ('image/jpeg', '$display_content');";
// echo "sql = $sql<br>";
db_query($sql, __LINE__, __FILE__);
$id = mysql_insert_id($link);
echo"Database: <br><img src=\"get_photo_test.php?id=$id\"><br>";
$sql = "SELECT `display_content`";
$sql .= "FROM `t_photos` ";
$sql .= "WHERE `id` = $id";
$photo_result = db_query($sql, __LINE__, __FILE__);
$photo_row = mysql_fetch_assoc($photo_result);
//extract raw from database save as id
$handle = fopen($output_path, 'w');
fwrite($handle, $photo_row['display_content']);
fclose($handle);
echo"Database -> file: <br><img
src=\"http://geoffsoper.co.uk/test/upload/output.jpg\"><br>";
?>
<?php
include('xxxxxxx/test.cfg');
if ($config_file_included != true)
{
die('Configuration file not found.');
}
include($admin_inc_dir . 'db.inc');
// Connecting, selecting database
db_start($db_host, $db_user, $db_password, $db_database);
$sql = "SELECT type, display_content as output ";
$sql .= "FROM t_photos ";
$sql .= "WHERE id = {$_REQUEST['id']}";
$result = db_query($sql, __LINE__, __FILE__);
if(mysql_num_rows($result) == 1)
{
$fileType = @mysql_result($result, 0, "type");
$fileContent = @mysql_result($result, 0, "output");
}
else
{
echo "Record doesn't exist.";
}
$img = imagecreatefromstring($fileContent);
imagejpeg($img);
?>
--
Remove nospam in email address to reply