您的位置:首页 > 数据库 > Mongodb

PHP mongoDB GridFS 图片存储系统

2015-05-20 00:07 429 查看
本帖最后由 tmkook 于 2011-11-8 23:53 编辑

最近因工作需要研究了下Gridfs并整理了个Demo出来。。

分享一下经验。。

gfs.php文件

<?php

//连接Mongo并初始化GFS

$conn = new Mongo();//如果设置了密码自己配置DSN

$db = $conn->photos;

$grid = $db->getGridFS();

//上传图片

if(isset($_FILES['upfile'])){

//保存新上传的文件

$size = $_FILES['upfile']['size'];

$md5 = md5_file($_FILES['upfile']['tmp_name']);

$exists = $grid->findOne(array('md5'=>$md5,'length'=>$size),array('md5'));

if(empty($exists)){

$grid->storeUpload('upfile');

}else{

unlink($_FILES['upfile']['tmp_name']);

}

echo "<p>图片路径为: <font color=red>http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}?img={$md5}</font></p>";

//生成图片

}elseif($id = $_GET['img']){

//索引图片文件

$grid = $db->getGridFS();

$image = $grid->findOne(array('md5'=>$id));

//设定文档类型,显示图片

$img_bytes = $image->getBytes();

include_once 'thumb.php';

$w = is_numeric($_GET['w'])? intval($_GET['w']) : 100;

Thumb::maxWidth($img_bytes,$w);

}elseif($id = $_GET['del']){

$s = $grid->remove(array('md5'=>$id));

header('Location:'.$_SERVER['HTTP_REFERER']);

//图片列表

}else{

//显示列表

$cursor = $grid->find();

foreach ($cursor as $obj):

echo '<p><a href="?img='.$obj->file['md5'].'&w=800"><img src="?img='.$obj->file['md5'].'" border="0" /></a><a href="?del='.$obj->file['md5'].'">删除</a></p>';

endforeach;

}

复制代码

thumb.php 缩略图文件

<?php

class Thumb

{

/**

* 以最大宽度缩放图像

* @param string $im 图像元数据

* @param float $w 最大宽度

*/

static function maxWidth($im,$w){

if(empty($im) || empty($w) || !is_numeric($w)){

throw new Exception("缺少必须的参数");

}

$im = imagecreatefromstring($im); //创建图像

list($im_w,$im_h) = self::getsize($im); //获取图像宽高

if($im_w > $w){

$new_w = $w;

$new_h = $w / $im_w * $im_h;

}else{

$new_w = $im_w;

$new_h = $im_h;

}

$dst_im = imagecreatetruecolor($new_w,$new_h);

imagecopyresampled($dst_im,$im,0,0,0,0,$new_w,$new_h,$im_w,$im_h);

header('Content-type:image/jpeg');

imagepng($dst_im);

imagedestroy($dst_im);imagedestroy($im);

}

/**

* 以最大高度缩放图像

* @param string $im 图像元数据

* @param float $w 最大高度

*/

static function maxHeight($im,$h){

if(empty($im) || empty($h) || !is_numeric($h)){

throw new Exception("缺少必须的参数");

}

$im = imagecreatefromstring($im); //创建图像

list($im_w,$im_h) = self::getsize($im); //获取图像宽高

if($im_h > $h){

$new_w = $h / $im_h * $im_w;

$new_h = $h;

}else{

$new_w = $im_w;

$new_h = $im_h;

}

$dst_im = imagecreatetruecolor($new_w,$new_h);

imagecopyresampled($dst_im,$im,0,0,0,0,$new_w,$new_h,$im_w,$im_h);

header('Content-type:image/jpeg');

imagepng($dst_im);

imagedestroy($dst_im);imagedestroy($im);

}

/**

* 生成固定大小的图像并按比例缩放

* @param string $im 图像元数据

* @param float $w 最大宽度

* @param float $h 最大高度

*/

static function fixed($im,$w,$h){

if(empty($im) || empty($w) || empty($h) || !is_numeric($w) || !is_numeric($h)){

throw new Exception("缺少必须的参数");

}

$im = imagecreatefromstring($im); //创建图像

list($im_w,$im_h) = self::getsize($im); //获取图像宽高

if($im_w > $im_h || $w < $h){

$new_h = intval(($w / $im_w) * $im_h);

$new_w = $w;

}else{

$new_h = $h;

$new_w = intval(($h / $im_h) * $im_w);

}

//echo "$im_w x $im_h <br/> $new_w x $new_h <br/> $x $y";exit;

//开始创建缩放后的图像

$dst_im = imagecreatetruecolor($new_w,$new_h);

imagecopyresampled($dst_im,$im,0,0,0,0,$new_w,$new_h,$im_w,$im_h);

header('Content-type:image/jpeg');

imagepng($dst_im);

imagedestroy($dst_im);imagedestroy($im);

}

/*

* 获取图像大小

* @param string $im 图像元数据

* @return array

*/

protected static function getsize($im){

return array(imagesx($im),imagesy($im));

}

}

复制代码

index.html HTML表单文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Mongo Gridfs</title>

</head>

<body>

<form action="gfs.php" method="post" enctype="multipart/form-data">

<input type="file" name="upfile" />

<input type="submit" value="upload" /> <a href="gfs.php">查看图片</a>

</form>

</body>

</html>

复制代码

代码下载:

顺便宣传一下自己的博客 http://tmkook.com/blog/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: