【问题标题】:Uploading and viewing a picture with mySql and php用mySql和php上传和查看图片
【发布时间】:2011-05-15 05:25:46
【问题描述】:

好的,我在上传和查看我试图用作“个人资料图片”的图片时遇到问题 当我上传图片时,我没有收到任何错误或任何东西,但是当我进入详细信息页面查看它是否存在时,phpmyadmin 将 BLOB - NULL 的图像数据更改为 BLOB - 0B,所以发生了一些事情但是所有其他属性都是空的,即。图像名称、图像类型、图像大小。没有发布我所有的代码,因为它有很多,有人知道它可能是什么吗?

用代码编辑:

用于添加的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);

显示图像:

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

【问题讨论】:

  • 贴出处理上传的基本代码,很难猜。
  • 没有代码片段很难指出问题
  • 基于该代码,我将不得不使用 process_uploaded_image_file 函数作为罪魁祸首。
  • 请贴出process_uploaded_image_file的完整代码
  • 好的,我编辑了我的帖子以包含它们

标签: php mysql upload image


【解决方案1】:
$imagedata = chunk_split(base64_encode(file_get_contents($image['tmp_name'])));

【讨论】:

  • 哦,从数据库中提取图像时使用 base64_decode()。 IE。 base64_decode($row['image'])
猜你喜欢
  • 2015-01-17
  • 1970-01-01
  • 2016-07-20
  • 2012-07-19
  • 2019-02-19
  • 2018-10-27
  • 1970-01-01
  • 2021-03-30
  • 2012-06-05
相关资源
最近更新 更多