1
我试图在将图像存储为MySQL中的BLOB之前调整图像大小。 FireBug告诉我“图像已损坏或被截断”,尽管我可以在运行后打开“tmp.jpg”。这里是我的代码:PHP调整大小后“图像损坏或截断”
$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'])
);
任何想法或建议表示赞赏。
什么是你的表的妆容?它可能是trunicated,因为mysql不能适合列中的图像,请确保使用bigblob或类似 – Ben 2011-06-08 21:30:59
我正在使用bigblob,我也应该提到,在添加调整大小代码之前,图像已成功存储。 – bdunn 2011-06-08 21:34:04
感谢您的澄清。 – Ben 2011-06-08 23:35:21