2011-05-15 18 views
0

好吧,即時通訊上傳和查看圖片,試圖用作「個人資料圖片」的圖片 當我上傳圖片,我沒有得到任何錯誤或任何東西,但是當我得到到詳細信息頁面查看它是否存在,沒有任何內容,phpmyadmin將BLOB的圖像數據 - NULL更改爲BLOB - 0B,因此發生了一些情況,但所有其他屬性都是空的,即。圖像名稱,圖像類型,圖像大小。沒有發佈我所有的代碼,因爲它有相當多的代碼,有沒有人有任何想法,它可能是什麼?用mySql和php上傳和查看圖片

的代碼編輯:

HTML代碼中添加:

<tr><td>Profile Picture:</td> <td><input type="hidden" value="1000000" name="MAX_FILE_SIZE"> 
<input type="file" size="30" value="Image file" name="imagefile"></td></tr> 

PHP添加:

$profilepic = $_FILES['imagefile']; 
$personid = add_person($username, $firstname, $lastname, $profilepic...etc); 

diplaying圖像:

<img src="get_image.php?id={$person.id}" {$person.imagesize} alt="{$person.imagename}"> 

在我add_person中我有:

$image_details = process_uploaded_image_file($profilepic); 
list($imagedata, $imagename, $imagetype, $imagewidthheight) = $image_details; 
then i insert those into my database 

最後我get_image.php

$id = $_GET['id']; 

$image = getImage($id); 

$data = $image['imagedata']; 
$name = $image['imagename']; 
$type = $image['imagetype']; 
$size = strlen($data); 

header("Content-length: $size"); 
header("Content-type: $type"); 
header("Content-Disposition: attachment; filename=$name"); 
echo $data; 

這就是所有我可以張貼未經處理圖像功能和調整等

編輯與process_ipload_image_file:

function process_uploaded_image_file($image) { 
// Check upload succeeded 
if (!is_uploaded_file($image['tmp_name']) || $image['size'] == 0) { 
    return NULL; 
} 

// Extract details 
$imagedata = addslashes(file_get_contents($image['tmp_name'])); 
$imagename = addslashes(basename($image['name'])); 
$imagesize = getimagesize($image['tmp_name']); // an array 
$imagewidthheight = addslashes($imagesize[3]); 
$imagetype = $imagesize['mime']; 

// Check file is a JPEG 
if ($imagetype != "image/jpeg") { 
    return NULL; 
} 

/* 
echo "Before resizing: name = $imagename, type = $imagetype, ", 
    "size = '$imagewidthheight'<br>\n"; 
*/ 

// Resize uploaded JPEG file, if necessary 
// (shouldn't reuse name tmp.jpg repeatedly) 
if ($imagesize[0] > MAX_WIDTH || $imagesize[1] > MAX_HEIGHT) { 

    resize_image_file($image['tmp_name'], "images/tmp.jpg", 
         MAX_WIDTH, MAX_HEIGHT); 
    list($width,$height) = getimagesize("images/tmp.jpg"); 
    $imagedata = addslashes(file_get_contents("images/tmp.jpg")); 
    $imagewidthheight = "width=\"$width\" height=\"$height\""; 
} 

/* 
echo "After resizing: name = $imagename, type = $imagetype, ", 
    "size = '$imagewidthheight'<br>\n"; 
*/ 

return array($imagedata, $imagename, $imagetype, $imagewidthheight); 
} 

這是調整大小功能

/* Resize image into a width-height rectangle using GD library. */ 
function resize_image_file($src_file, $dst_file, $width, $height) { 
// Compute new dimensions 
list($width_orig, $height_orig) = getimagesize($src_file); 

$ratio_orig = $width_orig/$height_orig; 

if ($width/$height > $ratio_orig) { 
    $width = $height*$ratio_orig; 
} else { 
    $height = $width/$ratio_orig; 
} 

// Resample $srcfile 
$image_p = imagecreatetruecolor($width, $height); 
$image = imagecreatefromjpeg($src_file); 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); 

// Output resized image to $dst_file 
imagejpeg($image_p, "$dst_file", 100); 
} 
+0

發佈處理上傳的基本代碼,很難猜測。 – tradyblix 2011-05-15 05:31:06

+0

現在很難指出沒有代碼snipset – AjayR 2011-05-15 05:35:31

+0

添加一些問題。 – John 2011-05-15 05:54:25

回答

1
$imagedata = chunk_split(base64_encode(file_get_contents($image['tmp_name']))); 
+0

哦,並使用base64_decode(),當你從數據庫中拉圖像。即。 BASE64_DECODE($行[ '圖像']) – radios4rabbits 2011-05-15 09:55:29