在将图像存储为MySQL中的BLOB之前,我正在尝试调整图像的大小。FireBug告诉我"图像已损坏或被截断",尽管我可以在运行后打开"tmp.jpg"。这是我的代码:
$fileName = $image['name'];
$tmpName = $image['tmp_name'];
$img_orig = imagecreatefromstring(file_get_contents($tmpName));
$orig_width = imagesx($img_orig);
$orig_height = imagesy($img_orig);
$max_width = $max_height = 500;
if ($orig_width > $orig_height && $orig_width > $max_width) {
$new_height = $orig_height/$orig_width*$max_width;
$new_width = $max_width;
}
else if ($orig_height > $max_height) {
$new_width = $orig_width/$orig_height*$max_height;
$new_height = $max_height;
}
$resized = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($resized,$img_orig,0,0,0,0,$new_width,$new_height,$orig_width,$orig_height);
ImageJPEG($resized,'tmp.jpg', 80);
$instr = fopen('tmp.jpg', "r+");
$img = addslashes(fread($instr, filesize('tmp.jpg')));
$imginfo = getimagesize('tmp.jpg');
在调整大小代码之后,我用以下查询插入MySQL:
$query = sprintf(
"insert into images (filename, mime_type, file_size, file_data, category, priority)
values ('%s', '%s', %d, '%s', '%s', 1)",
mysql_real_escape_string($fileName),
mysql_real_escape_string($imginfo['mime']),
filesize('tmp.jpg'),
mysql_real_escape_string($img),
mysql_real_escape_string($_POST['category'])
);
如有任何想法或建议,我们将不胜感激。
首先使用addslashes()
,然后使用mysql_real_escape_string()
,我认为这会损坏文件数据