php 实现图片上传并压缩功能
前段时间由于项目需要,要实现图片上传并且压缩生成缩略图的功能。
接口代码如下:
$allowext = array ( 'png', 'jpg', 'jpeg', 'gif','mp4','doc');
$fileElement = 'file';
$filepath_rel = 'userfiles/upload/chatfile/'.date("Ymd")."/"; // 相对路径
//这里的$FILE_PATH 是网站的根目录
$filepath_abs = $FILE_PATH . $filepath_rel; // 绝对路径
if(!file_exists($filepath_abs))
{
mkdir($filepath_abs,0777,true); }
$fup = new FileUpload ( '100M', $allowext );
$r = $fup->upload ( $fileElement, $filepath_abs, '', true );
$name_abs = $filepath_abs . $r;
$name_rel = $filepath_rel . $r;
//图片等比例压缩
$pic = $fup->getThumb($FILE_PATH,$name_rel,300,300);
//upload 方法实现图片上传
//参数$elename : file域的名称。<input type='file' name='elename'/>
//$newname --上传以后的名字;可以为空。
//$savepath --文件保存路径,一定要以 “/” 结尾。
//$auto_rename 是否自动重命名
//如果$newname 为空 且 auto_rename 为false ,将保留原来的文件名字
function upload($elename, $savepath, $newname = '', $auto_rename = false){
if(empty($_FILES[$elename])) throw new Exception('没有上传文件或文件大小超过系统限制', 981);
$f_name = basename($_FILES[$elename]["name"]); //被上传文件的名称
$f_type = $_FILES[$elename]["type"]; //被上传文件的类型
$f_size = $_FILES[$elename]["size"]; //被上传文件的大小,以字节计
$f_tmpname = $_FILES[$elename]["tmp_name"]; //存储在服务器的文件的临时副本的名称
$f_error = $_FILES[$elename]["error"]; //由文件上传导致的错误代码
//是否发生错误
if($f_error) $this->uploadFileError($f_error);
//文件后缀
$f_ext = $this->getFileExt($f_name);
//检查上传类型
//--是否在禁止列表
$forbidext = $this->forbidext;
if(in_array($f_ext, $forbidext)) {
throw new Exception('文件类型禁止上传', 901);
}
//--是否在允许列表
$allowext = $this->allowext;
if(!in_array($f_ext, $allowext)) {
throw new Exception('文件类型未被允许', 902);
}
//文件大小是否允许
$allowsize = $this->allowsize;
if($f_size > $allowsize) {
throw new Exception('文件超过允许的大小', 903);
}
//文件是否是上传的文件
if(!is_uploaded_file($f_tmpname)) {
throw new Exception('非上传的文件', 904);
}
//文件重命名 按时间命名 方便查看
if(empty($newname) && $auto_rename)
$new_name = $this->setFileNameByDate().'.'.$f_ext;
elseif(!empty($newname))
$new_name = $newname.'.'.$f_ext;
else
$new_name = $f_name;
//保存文件
$f_path = $savepath.$new_name;
if(move_uploaded_file($f_tmpname, $f_path)){
return $new_name;//上传成功,返回文件名。
}else{
throw new Exception('文件写入失败,请检查上传目录是否可写', 905);
}
}
//生成一个日期命名的文件名
private function setFileNameByDate(){
return date('YmdHis').rand(1000,9999);
}
完成了上传文件的功能接下来就要对这个文件进行压缩处理了。在这里用到了在脚本之家学习到的方法,做了一些改变。
function resizeImage($im, $dest, $maxwidth, $maxheight) {
$img = getimagesize($im);
switch ($img[2]) {
case 1:
$im = @imagecreatefromgif($im);
break;
case 2:
$im = @imagecreatefromjpeg($im);
break;
case 3:
$im = @imagecreatefrompng($im);
break;
}
$pic_width = imagesx($im);
$pic_height = imagesy($im);
$resizewidth_tag = false;
$resizeheight_tag = false;
if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
if ($maxwidth && $pic_width > $maxwidth) {
$widthratio = $maxwidth / $pic_width;
$resizewidth_tag = true;
}
if ($maxheight && $pic_height > $maxheight) {
$heightratio = $maxheight / $pic_height;
$resizeheight_tag = true;
}
if ($resizewidth_tag && $resizeheight_tag) {
if ($widthratio < $heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}
if ($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;
if ($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;
if (function_exists("imagecopyresampled")) {
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
} else {
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
}
imagejpeg($newim, $dest);
imagedestroy($newim);
} else {
imagejpeg($im, $dest);
}
}
function getThumb($public_path,$sFile,$iWidth,$iHeight){
//图片公共路径 $public_path 这里改为有外部传入 根据具体情况进行修改
//判断该图片是否存在
if(!file_exists($public_path.$sFile)) return $sFile;
//判断图片格式(图片文件后缀)
$extend = explode("." , $sFile);
$attach_fileext = strtolower($extend[count($extend) - 1]);
if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
return '';
}
//压缩图片文件名称
$sFileNameS = str_replace(".".$attach_fileext, "_".$iWidth.'_'.$iHeight.'.'.$attach_fileext, $sFile);
//判断是否已压缩图片,若是则返回压缩图片路径
if(file_exists($public_path.$sFileNameS)){
return $sFileNameS;
}
//生成压缩图片,并存储到原图同路径下
self::resizeImage($public_path.$sFile, $public_path.$sFileNameS, $iWidth, $iHeight);
if(!file_exists($public_path.$sFileNameS)){
return $sFile;
}
return $sFileNameS;
}
这样上传的图片就按给定的width、height按相同比例压缩 这里resizeImage指定了最大的宽,高也可根据实际需求情况去调整。
转载于:https://www.cnblogs.com/cyworz/p/10565211.html
总结
以上是生活随笔为你收集整理的php 实现图片上传并压缩功能的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: sqlsever 转mysql 出错
- 下一篇: 数据表操作