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_gp c Off On
magic_quotes_ru ntime Off Off
magic_quotes_sy base 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_si ze = 640;
include('xxxxxx xx/test.cfg');
if ($config_file_i ncluded != true)
{
die('Configurat ion 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<b r>";
exec ("convert -sample 640x427 $input_path $display_path") ;
echo"Original:< br><img
src=\"http://geoffsoper.co.u k/test/upload/039_35a.jpg\">< br>";
echo"Imagemagic k raw:<br><img
src=\"http://geoffsoper.co.u k/test/upload/test/raw.jpg\"><br>" ;
$display_s = filesize($displ ay_path);
$display_handle = fopen($display_ path, "r");
$display_conten t = fread($display_ handle, $display_s);
$display_conten t = addslashes($dis play_content);
$sql = "INSERT INTO `t_photos` (`type`, `display_conten t`) ";
$sql .= "VALUES ('image/jpeg', '$display_conte nt');";
// echo "sql = $sql<br>";
db_query($sql, __LINE__, __FILE__);
$id = mysql_insert_id ($link);
echo"Database: <br><img src=\"get_photo _test.php?id=$i d\"><br>";
$sql = "SELECT `display_conten t`";
$sql .= "FROM `t_photos` ";
$sql .= "WHERE `id` = $id";
$photo_result = db_query($sql, __LINE__, __FILE__);
$photo_row = mysql_fetch_ass oc($photo_resul t);
//extract raw from database save as id
$handle = fopen($output_p ath, 'w');
fwrite($handle, $photo_row['display_conten t']);
fclose($handle) ;
echo"Database -> file: <br><img
src=\"http://geoffsoper.co.u k/test/upload/output.jpg\"><b r>";
?>
<?php
include('xxxxxx x/test.cfg');
if ($config_file_i ncluded != true)
{
die('Configurat ion file not found.');
}
include($admin_ inc_dir . 'db.inc');
// Connecting, selecting database
db_start($db_ho st, $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_ro ws($result) == 1)
{
$fileType = @mysql_result($ result, 0, "type");
$fileContent = @mysql_result($ result, 0, "output");
}
else
{
echo "Record doesn't exist.";
}
$img = imagecreatefrom string($fileCon tent);
imagejpeg($img) ;
?>
--
Remove nospam in email address to reply
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_gp c Off On
magic_quotes_ru ntime Off Off
magic_quotes_sy base 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_si ze = 640;
include('xxxxxx xx/test.cfg');
if ($config_file_i ncluded != true)
{
die('Configurat ion 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<b r>";
exec ("convert -sample 640x427 $input_path $display_path") ;
echo"Original:< br><img
src=\"http://geoffsoper.co.u k/test/upload/039_35a.jpg\">< br>";
echo"Imagemagic k raw:<br><img
src=\"http://geoffsoper.co.u k/test/upload/test/raw.jpg\"><br>" ;
$display_s = filesize($displ ay_path);
$display_handle = fopen($display_ path, "r");
$display_conten t = fread($display_ handle, $display_s);
$display_conten t = addslashes($dis play_content);
$sql = "INSERT INTO `t_photos` (`type`, `display_conten t`) ";
$sql .= "VALUES ('image/jpeg', '$display_conte nt');";
// echo "sql = $sql<br>";
db_query($sql, __LINE__, __FILE__);
$id = mysql_insert_id ($link);
echo"Database: <br><img src=\"get_photo _test.php?id=$i d\"><br>";
$sql = "SELECT `display_conten t`";
$sql .= "FROM `t_photos` ";
$sql .= "WHERE `id` = $id";
$photo_result = db_query($sql, __LINE__, __FILE__);
$photo_row = mysql_fetch_ass oc($photo_resul t);
//extract raw from database save as id
$handle = fopen($output_p ath, 'w');
fwrite($handle, $photo_row['display_conten t']);
fclose($handle) ;
echo"Database -> file: <br><img
src=\"http://geoffsoper.co.u k/test/upload/output.jpg\"><b r>";
?>
<?php
include('xxxxxx x/test.cfg');
if ($config_file_i ncluded != true)
{
die('Configurat ion file not found.');
}
include($admin_ inc_dir . 'db.inc');
// Connecting, selecting database
db_start($db_ho st, $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_ro ws($result) == 1)
{
$fileType = @mysql_result($ result, 0, "type");
$fileContent = @mysql_result($ result, 0, "output");
}
else
{
echo "Record doesn't exist.";
}
$img = imagecreatefrom string($fileCon tent);
imagejpeg($img) ;
?>
--
Remove nospam in email address to reply
Comment