2011-06-08 46 views
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']) 
     ); 

任何想法或建議表示讚賞。

+0

什麼是你的表的妝容?它可能是trunicated,因爲mysql不能適合列中的圖像,請確保使用bigblob或類似 – Ben 2011-06-08 21:30:59

+0

我正在使用bigblob,我也應該提到,在添加調整大小代碼之前,圖像已成功存儲。 – bdunn 2011-06-08 21:34:04

+0

感謝您的澄清。 – Ben 2011-06-08 23:35:21

回答

0

首先使用的是addslashes(),然後你使用mysql_real_escape_string(),我認爲是破壞文件數據

+0

謝謝!這是問題所在。 – bdunn 2011-06-08 21:53:57