2011-05-15 6 views
0

"프로필 사진"으로 사용하려고하는 사진 업로드 및보기에 문제가 있습니다. 사진을 업로드하면 오류가 발생하지 않지만, 세부 정보 페이지에서 아무 것도 없으면 phpmyadmin은 BLOB - NULL에 대한 imagedata를 BLOB - 0B로 변경하므로 어떤 일이 일어나고 있지만 다른 모든 속성은 비어 있습니다. imagename, imagetype, 이미지 크기. 내 코드를 게시하지 않고서는 그 코드가 아주 많아서 누구나 될 수있는 아이디어가 있습니까? 추가mySql 및 php로 사진 업로드 및보기

HTML : 코드

편집

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

이미지 diplaying : 내가 가진 내 add_person에

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

를 추가

<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 :

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

그리고 마지막으로 내 get_image.php

편집 process_ipload_image_file 함께 등 전 프로세스 이미지 기능없이 게시 할 수있는 모든 및 크기 조정에 대한

$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; 

그게 전부 :

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

+0

코드 스 니펫 – AjayR

+0

을 (를) 추가하지 않으면이 문제를 지적하기가 어렵습니다. – John

답변

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

오, 데이터베이스에서 이미지를 가져올 때 base64_decode()를 사용하십시오. 즉. base64_decode ($ row [ 'image']) – radios4rabbits