php把图片上传到数据库并显示

告诉我储存数据库的字段类型是是么给出代码... 告诉我储存数据库的字段类型是是么 给出代码 展开
 我来答
cstabor
2009-08-05 · 超过12用户采纳过TA的回答
知道答主
回答量:33
采纳率:0%
帮助的人:35.4万
展开全部
blob类型
<?php
/*******************************************************
** 文件名:upload_file.php
** Copyright @ 2009
** 创建人:tabor
** 日期:2009年7月24日 8:00
** 修改人:
** 日期:
** 描述:文件上传操作以及对图片文件的处理
** 版本:
********************************************************/
class upload_file {
//保存的文件名
public $file_name;
//系统中上传文件的临时存放路径
public $file_tmp_name;
//文件大小
public $file_size;
//完整的文件类型
public $full_file_type;
//文件类型
public $file_type;
//文件是否覆盖
public $override = 1;
//文件的保存路径
public $file_save_path = '';
//上传文件大小的最大值 单位是字节 2M
public public $file_max_size = 210000000;
//public public $file_max_size = 102400;

//构造函数
function __construct($file_name = '', $file_tmp_name = '', $full_file_type = '', $file_size = '', $file_save_path = '') {
$this->file_name = $file_name;
$this->file_tmp_name = $file_tmp_name;
$this->full_file_type = $full_file_type;
$this->file_size = $file_size;
$this->file_save_path = $file_save_path;
}
//取得文件的后缀名,即文件类型
function get_file_type() {
$type_array = explode('.', $this->file_name);
return $type_array[count($type_array)-1];
}
//判断文件的大小
function check_size() {
if($this->file_size > $this->file_max_size) {
return false;
}
return true;
}
//取得文件的大小
function get_size() {
return intval($this->file_size/1024);
}
//上传图片 格式 jpg,png,gif,pjpeg
function check_upload_pic() {
$type = $this->get_file_type();
$type_array = array('jpg', 'png', 'gif', 'bmp');
foreach($type_array as $value) {
if($value = $type) {
return true;
}
return false;
}
}
//上传文件 格式 zip rar
function check_upload_file() {
$type = $this->get_file_type();
$type_array = array('jpg','gif','bmp','png');
foreach($type_array as $value) {
if($value == $type) {
return true;
}
return false;
}

}
//判断文件是否存在
function check_exist() {
$file = $this->file_save_path.$this->file_name;
return file_exists($file);
}
//上传文件
function move_upfile() {
if(!$this->check_upload_pic()) {
echo "ok1";
return false;
}
else {
if(!$this->check_size()) {
echo "ok2";
return false;
}
else {
// if($this->check_exist()) {
// echo "该文件已存在";
// return false;
// }
// else {
$path = $this->file_save_path.$this->file_name;
if(move_uploaded_file($this->file_tmp_name, $path)) {
return true;
}
else {
return false;
}

// }
}
}
}
//将上传的图片打水印
/**
* $water_pic_name 将要被打水印的目标图片
* $water_word 水印文字
* $path 将来生成水印图片的存放路径
*/
function create_water_pic($water_word) {
$type = $this->get_file_type();
$filename = $this->file_save_path.$this->file_name;
switch($type) {
case 'jpg':
header("content-type:image/jpeg"); //定义输出图像的类型
$im = imagecreatefromjpeg($filename); //载入图片
break;
case 'png':
header("content-type:image/png");
$im = imagecreatefrompng($filename);
break;
case 'gif':
header("content-type:image/gif");
$im = imagecreatefromgif($filename);
break;
case 'bmp':
header("content-type:image/xbm"); //上传bmp格式存在问题
$im = imagecreatefromxbm($filename); //无法打水印
break;
default: {
echo "文件格式不符";
}
}
$textcolor = imagecolorallocate($im, 56, 73,136); //设定字体的颜色
$font = "simhei.ttf"; //定义字体
$word = $water_word; //水印字符
$x = imagesx($im); //获取图片的宽度
$y = imagesy($im); //获取文件的高度
$position_x = $x-80;
$position_y = $y-10;
$str = iconv('gbk', 'utf-8', $word); //将中文文字显示出来的编码过程
imagettftext($im, 20, 0, $position_x, $position_y, $textcolor, $font, $str);
//imagejpeg($im); //显示图片
$new = $this->file_save_path.'water'.$this->file_name; //生成新的文件名
switch($type) {
case 'jpg':
imagejpeg($im, $new); //生成jpg图像
break;
case 'png':
imagepng($im, $new); //生成png图像
break;
case 'gif':
imagegif($im, $new); //生成gif图像
break;
case 'bmp':
imagexbm($im, $new); //生成bmp图像 该格式的文件处理有问题
break;
default: {
echo "文件格式不符";
}
}
imagedestroy($im); //结束图形,释放内存空间*/
}
//生成缩略图
/**
* $pic 图片名 包括其扩展名,但不包括路径
* $width 将来生成缩略图的宽度
* $height 将来生成缩略图的高度
* $path 生成缩略图的存放路径
*/
function create_thumbnail($width, $height) {
$type = $this->get_file_type();
$filename = $this->file_save_path.$this->file_name;
$img = getimagesize($filename);
//print_r($img);
//die();
switch($img[2]) {
case 1:
header("content-type:image/gif"); //定义输出图像的类型
$im = imagecreatefromgif($filename); //载入图片
break;
case 2:
header("content-type:image/jpeg");
$im = imagecreatefromjpeg($filename);
break;
case 3:
header("content-type:image/png");
$im = imagecreatefrompng($filename);
break;
case 6:
header("content-type:image/xbm"); //bmp格式存在问题
$im = imagecreatefromxbm($filename); //无法打水印
break;
default: {
echo "文件格式不符";
}
}
$thumb = imagecreatetruecolor($width, $height); //创建一个新的空白的面板
$color = imagecolorallocate($im, 200, 255, 100); //调色板
/*bool imagecopyresized ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
imagecopyresized() 将一幅图像中的一块正方形区域拷贝到另一个图像中。dst_image 和 src_image 分别是目标图像和源图像的标识符。
*/
imagecopyresized($thumb, $im, 0, 0, 0, 0, $width, $height, $img[0], $img[1]);
//imagejpeg($thumb);
$thumb_path = $this->file_save_path."thumbnail/".$this->file_name;
switch($img[2]) {
case 1:
imagejpeg($thumb, $thumb_path);
break;
case 2:
imagegif($thumb, $thumb_path);
break;
case 3:
imagepng($thumb, $thumb_path);
break;
case 6:
imagexbm($thumb, $thumb_path);
break;
default: {
echo "文件格式不符";
}
}
}
}
?>

前几天做的一个类,可以正常的使用,但还存在问题,仅供参考!忘对您有所帮助
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式