我有一个图像文件存储到数据库作为blob(代码中显示)。一旦调整到80 * 80(缩略图),我需要将它存储到其他数据库。我已调整大小并将其保存为文件,但无法将调整大小的图像存储到数据库中。我怎样才能做到这一点?如何在PHP上调整图像文件的大小?
//resize image and save
include('MyImage.php');
$image = new MyImage();
$image->load($tmpName);
$image->resize(80,80);
$image->save('thumbnail.jpg');
//storing original image onto database
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$query = "INSERT INTO image_tbl (name, size, type, content)".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed');
}
从过去在数据库中存储图像的人,我不认为这是一个好主意。 – ppp 2012-02-25 22:48:03
1.完全缺乏sql注入保护(没有,addslashes不是保护)。 2.完全没有验证上传的成功。 3.明显缺乏对'file_get_contents()'的认识。 4.在定义之前使用变量($ tmpname)。 – 2012-02-25 23:16:16
@anpel你是说图像存储在数据库不是一个好主意吗? – 2012-02-25 23:32:28