"Image corrupt or truncated" after PHP resize
I am attempting to resize an image before storing it as a BLOB in MySQL. FireBug tells me that the "Image is corrupt or truncated", although I am able to open 'tmp.jpg' after this runs. Here is my code:
$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');
After the resize code, I insert into MySQL with this query:
$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($fileNam开发者_如何学编程e),
mysql_real_escape_string($imginfo['mime']),
filesize('tmp.jpg'),
mysql_real_escape_string($img),
mysql_real_escape_string($_POST['category'])
);
Any thoughts or suggestions are appreciated.
First you are using addslashes()
and then you are using mysql_real_escape_string()
, i think thats corrupting the file data
精彩评论