图片上传 宽高截取
来源:互联网 发布:手机装机必备软件知乎 编辑:程序博客网 时间:2024/06/10 15:02
<?php
class upphoto{
public $ph_name; //上传图片文件名
public $ph_path="img/upload/"; //上传文件存放路径
public $al_ph_type=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png'); //允许上传图片类型
public $al_ph_size=2097152; //允许上传文件大小
function __construct(){
//文件夹路径
$this->ph_path.=date('Ym').'/';
}
//获取原文件名
function get_ph_name($phname){
//文件名称时间随机
$datetime=time().rand(10,99);
$this->ph_name=$this->ph_path.$datetime.strrchr($phname,"."); //strrchr获取文件的点最后一次出现的位置
return $this->ph_name;
}
//判断上传文件存放目录
function check_path(){
if(!file_exists($this->ph_path)){
mkdir($this->ph_path,0777,true);
}
}
//判断上传文件是否超过允许大小
function check_size($size){
if($size> $this->al_ph_size){
return $this->showerror("上传图片超过2M");
}
}
//判断文件类型
function check_type($type){
if(!in_array($type,$this->al_ph_type)){
return $this->showerror("上传图片类型错误");
}
}
//上传图片
function up_photo($file){
$this->check_path();
$this->get_ph_name($file['name']);
if(!move_uploaded_file($file['tmp_name'],$this->ph_name)){
$this->showerror("上传文件出错");
}
return array('error'=>'','file'=>'/'.$this->ph_name);
}
//错误提示
function showerror($errorstr){
return array('error'=>$errorstr,'file'=>'');
}
//上传 处理图片
function save($file,$width=0,$height=0)
{
if (empty($file) || $file['error']!=0)
{
return $this->showerror("上传错误");
}
$type=$this->check_type($file['type']);
if (!empty($type['error']))
{
return $type;
}
$size=$this->check_size($file['size']);
if (!empty($size['error']))
{
return $size;
}
$res= $this->up_photo($file);
if (!empty($res['file']))
{
if ($width>0)
{
$this->resize_image(substr($res['file'], 1),substr($res['file'], 1),$width,$height);
}
}
return $res;
}
/**
* 改变图片的宽高
*
* @author flynetcn (2009-12-16)
*
* @param string $img_src 原图片的存放地址或url
* @param string $new_img_path 新图片的存放地址
* @param int $new_width 新图片的宽度
* @param int $new_height 新图片的高度 默认等比例压缩
* @return bool 成功true, 失败false
*/
function resize_image($img_src, $new_img_path, $new_width, $new_height=0)
{
$img_info = @getimagesize($img_src);
if (!$img_info)
{
return false;
}
if ($img_info[0]<=$new_width)
{
return false;
}
//高度自动变换
if (empty($new_height))
{
$r=round(($new_width/$img_info[0]),2);
$new_height=round($img_info[1]*$r);
}
if ($new_width < 1 || $new_height < 1 || empty($new_img_path)) {
return false;
}
if (strpos($img_info['mime'], 'jpeg') !== false) {
$pic_obj = imagecreatefromjpeg($img_src);
} else if (strpos($img_info['mime'], 'gif') !== false) {
$pic_obj = imagecreatefromgif($img_src);
} else if (strpos($img_info['mime'], 'png') !== false) {
$pic_obj = imagecreatefrompng($img_src);
} else {
return false;
}
$pic_width = imagesx($pic_obj);
$pic_height = imagesy($pic_obj);
if (function_exists("imagecopyresampled")) {
$new_img = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($new_img, $pic_obj, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);
} else {
$new_img = imagecreate($new_width, $new_height);
imagecopyresized($new_img, $pic_obj, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);
}
if (preg_match('~.([^.]+)$~', $new_img_path, $match)) {
$new_type = strtolower($match[1]);
switch ($new_type) {
case 'jpg':
imagejpeg($new_img, $new_img_path);
break;
case 'gif':
imagegif($new_img, $new_img_path);
break;
case 'png':
imagepng($new_img, $new_img_path);
break;
default:
imagejpeg($new_img, $new_img_path);
}
} else {
imagejpeg($new_img, $new_img_path);
}
imagedestroy($pic_obj);
imagedestroy($new_img);
return true;
}
}
?>
调用
include_once(ROOT_PATH.'lib/upphoto/upphoto.php');
$up=new upphoto();
$res=$up->save($_FILES['file'],600);
echo json_encode($res);
die;
- 图片上传 宽高截取
- java图片截取上传
- JAVA+JQUERY 上传截取图片
- jquery 上传图片自由截取
- view截取图片和view获取宽高
- .net 修改上传图片的宽高
- 图片上传大小,类型,宽高验证
- js获取上传图片宽高
- 获取上传图片的宽高
- tp-上传图片,自带截取图片
- 截取屏幕宽高
- 20130420 js的图片上传和截取
- <<爬坑一>>有关上传头像中的截取图片
- 基于swuplaod图片的上传截取
- 截取页面并上传保存图片
- 从已有到图片中截取规定宽高的区域图片
- jquery获取上传图片的宽高和地址图片的宽高
- asp.net判断上传图片的宽高
- jquery通过name绑定单击事件
- python相关常用操作
- event.preventDefault方法的介绍
- Chapter 1 | Arrays and Strings -- 判断字符串中字符唯一
- java异常处理
- 图片上传 宽高截取
- ko学习记录记载
- 黑马程序员--单例模式中为什么用枚举更好
- linux gpio模拟i2c的使用/用GPIO模拟I2C总线-1
- thg5hy6h6thyjy7j
- struts json插件json属性的认识
- stdio和STDIN_FILENO以及带缓冲IO和不带缓冲IO的详细说明
- Android 通过WebService进行网络编程,使用工具类轻松实现
- 解决cpsid_82829s1: "A restart is pending," bootstrapper error has occurred.