PHP文件(图片)上传
来源:互联网 发布:java中类和对象的用法 编辑:程序博客网 时间:2024/05/16 04:28
提交表单<input type="file" name="image" />,表单设置属性enctype="multipart/form-data"
提交处理页面:
$imgname = strtolower($_FILES['image']['name']);
$type = substr(strrchr($imgname, '.'), 1);
if (!in_array($type, array('jpg', 'jpeg', 'png', 'gif', 'bmp'))) {
echo('当前图片图片格式不支持,支持jpg、gif、png、bmp格式!');
exit;
}
$upload_tmp_file = ROOT . '/data/tmp/article_' . random(6, 0) . '.' . $type;
$filepath = '/data/attach/article/article' . random(6, 0) . '.' . $type;
mkdir(ROOT . '/data/attach/article');
if (move_uploaded_file($_FILES['image']['tmp_name'], $upload_tmp_file)) {
image_resize($upload_tmp_file, ROOT . $filepath, 260, 180); //生成缩略图
echo('上传成功');
}
//创建目录
function mkdir($path) {
if (!file_exists($path)) {
forcemkdir(dirname($path));
mkdir($path, 0777);
}
}
//生成缩略图
function image_resize($src, $dst, $width, $height, $crop = 0) {
if (!list($w, $h) = getimagesize($src))
return "Unsupported picture type!";
$type = strtolower(substr(strrchr($src, "."), 1));
if ($type == 'jpeg')
$type = 'jpg';
switch ($type) {
case 'bmp': $img = imagecreatefromwbmp($src);
break;
case 'gif': $img = imagecreatefromgif($src);
break;
case 'jpg': $img = imagecreatefromjpeg($src);
break;
case 'png': $img = imagecreatefrompng($src);
break;
default : return false;
}
// resize
if ($crop) {
if ($w < $width or $h < $height) {
rename($src, $dst);
return true;
}
$ratio = max($width / $w, $height / $h);
$h = $height / $ratio;
$x = ($w - $width / $ratio) / 2;
$w = $width / $ratio;
} else {
if ($w < $width and $h < $height) {
rename($src, $dst);
return true;
}
$ratio = min($width / $w, $height / $h);
$width = $w * $ratio;
$height = $h * $ratio;
$x = 0;
}
$new = imagecreatetruecolor($width, $height);
// preserve transparency
if ($type == "gif" or $type == "png") {
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
}
imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);
switch ($type) {
case 'bmp': imagewbmp($new, $dst);
break;
case 'gif': imagegif($new, $dst);
break;
case 'jpg': imagejpeg($new, $dst);
break;
case 'png': imagepng($new, $dst);
break;
}
return true;
}
- PHP文件(图片)上传
- PHP上传图片文件
- php 文件上传图片
- php上传文件图片
- 图片(文件)上传(BROWSER+PHP)
- php多图片(文件)上传
- 【PHP】PHP上传文件,上传图片
- php 关于图片文件上传
- php 图片文件上传类
- PHP图片上传、文件上传函数
- php 远程上传文件 远程上传图片
- php 图片上传 文件上传 大小 限制
- 使用PHP实现七牛云存储图片(文件)上传
- 上传文件(图片)
- 文件上传(图片)
- 图片(文件)上传
- PHP上传多文件,多图片程序代码
- php上传文件,图片前的准备
- iOS学习之UIDatePicker控件使用
- 火狐下无法用window.close关闭窗口解决
- xampp Apache Access forbidden! Error 403 解决方法
- shell中map的使用和遍历
- Git Gui for Windows的建库、克隆(clone)、上传(push)、下载(pull)、合并
- PHP文件(图片)上传
- 递归获取大类中的所有小类
- git push 小结
- YII设置默认控制器与方法
- YII 获取系统级请求参数的常用方法
- Yii的常用URL
- YII 如何在控制器里调用 model 方法
- 在Yii中直接使用sql
- android使用ant打包成SDK