使用mySql和php上传和查看图片

时间:2011-05-15 05:25:47

标签: php mysql upload image

好的,我无法上传和查看我试图用作“个人资料图片”的图片 当我上传图片时,我没有得到任何错误或任何东西,但是当我到达详细信息页面以查看它是否存在时,没有任何内容,phpmyadmin将BLOB的图像数据 - NULL更改为BLOB - 0B,所以发生了一些事情但是所有其他属性都是空的,即。 imagename,imagetype,imagesize。没有发布我的所有代码,因为它有很多,有没有人对它可能有什么想法?

使用代码编辑:

用于添加的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 the image:

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

1 个答案:

答案 0 :(得分:1)

$imagedata = chunk_split(base64_encode(file_get_contents($image['tmp_name'])));
相关问题