开发者

Uploading and viewing a picture with mySql and php

Ok, im having trouble uploading and viewing a picture that im trying to use as "profile pictures" When i upload the picture, i get no errors or anything, but when i got to the details page to see if its there, nothing is there, the phpmyadmin changes the imagedata for BLOB - NULL to BLOB - 0B, so something is happening but all the other attributes are empty, ie. the imagename, imagetype, imagesize. Without posting all my code cause there is quite alot of it, does anyone have any ideas on what it could be?

EDIT WITH CODE:

html for adding:

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

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

diplaying the image:

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

in my add_person i have:

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

and finally my 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;

thats about all i can post without the process image function and resizing etc.

EDIT WITH 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);
}

Here is the resize function

/* 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);
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜