![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
PHP、HTML5上传图片自动压缩问题 10
PHP代码:publicfunctionupload_face(){if($attach=$_FILES['avatar']){$data=array();if($a=K...
PHP代码:
public function upload_face(){
if($attach = $_FILES['avatar']){
$data = array();
if($a = K::M('magic/upload')->upload($attach, 'car')){
$data['face'] = $a['photo'];
}
//修改头像
if($up = K::M('member/member')->update($this->uid,$data)){
$this->msgbox->add('头像设置成功');
$this->msgbox->set_data("forward", $this->mklink('ucenter/info:index',null));
}else{
$this->msgbox->add('设置失败',211);
}
}
}
HTML代码:
//上传图片
function upload_face(file)
{
var formData = new FormData();
formData.append('avatar', file);
$.ajax({
url : "<{link ctl='ucenter/info:upload_face'}>",
type : "POST",
data : formData,
processData : false,
contentType : false,
success : function(responseStr) {
layer.open({
content:"更换头像成功",
time:1.5
});
},
error : function(responseStr) {
layer.open({
content:"未知错误发生",
time:1.5
});
}
});
}
if (!window.applicationCache) {
alert("不支持HTML5");
}
//HTML5选图片
function fileSelected(obj){
var files = obj.files;
for(var i=0;i<files.length;i++){
var tag = '';
var rFilter = /^(image\/gif|image\/jpeg|image\/jpg|image\/png)$/i;
if (!rFilter.test(files[i].type)) {
alert("只允许上传JPG、PNG、GIF格式图片");
return false;
}
var reader = new FileReader();
reader.onloadstart = function(e){
$(".loading").show();
}
reader.onload = function(e){
$(".face_img").attr('src',e.target.result);
}
reader.readAsDataURL(files[i]);
}
upload_face(files[0]);
//隐藏弹出框
$(".accountInfor_headX_mask").hide();
$(".accountInfor_headX_mask").parent().find(".mask_bg").hide();
}
【以上代码怎么修改成自动压缩到指定的宽高大小?比如上传的是1MB的图片,经过压缩后自动缩小到几十KB,以原图比例压缩】 展开
public function upload_face(){
if($attach = $_FILES['avatar']){
$data = array();
if($a = K::M('magic/upload')->upload($attach, 'car')){
$data['face'] = $a['photo'];
}
//修改头像
if($up = K::M('member/member')->update($this->uid,$data)){
$this->msgbox->add('头像设置成功');
$this->msgbox->set_data("forward", $this->mklink('ucenter/info:index',null));
}else{
$this->msgbox->add('设置失败',211);
}
}
}
HTML代码:
//上传图片
function upload_face(file)
{
var formData = new FormData();
formData.append('avatar', file);
$.ajax({
url : "<{link ctl='ucenter/info:upload_face'}>",
type : "POST",
data : formData,
processData : false,
contentType : false,
success : function(responseStr) {
layer.open({
content:"更换头像成功",
time:1.5
});
},
error : function(responseStr) {
layer.open({
content:"未知错误发生",
time:1.5
});
}
});
}
if (!window.applicationCache) {
alert("不支持HTML5");
}
//HTML5选图片
function fileSelected(obj){
var files = obj.files;
for(var i=0;i<files.length;i++){
var tag = '';
var rFilter = /^(image\/gif|image\/jpeg|image\/jpg|image\/png)$/i;
if (!rFilter.test(files[i].type)) {
alert("只允许上传JPG、PNG、GIF格式图片");
return false;
}
var reader = new FileReader();
reader.onloadstart = function(e){
$(".loading").show();
}
reader.onload = function(e){
$(".face_img").attr('src',e.target.result);
}
reader.readAsDataURL(files[i]);
}
upload_face(files[0]);
//隐藏弹出框
$(".accountInfor_headX_mask").hide();
$(".accountInfor_headX_mask").parent().find(".mask_bg").hide();
}
【以上代码怎么修改成自动压缩到指定的宽高大小?比如上传的是1MB的图片,经过压缩后自动缩小到几十KB,以原图比例压缩】 展开
展开全部
给你个图片处理的类吧,图片剪裁处理后,也就等于将图片压缩了。
/**
* 图像处理类
* ============================================================================
* Copyright 2014 大秦科技,并保留所有权利。
* 网站地址: http://www.qintech.net;
* ============================================================================
*/
class Image{
//生成缩略图的方式
public $thumbType;
//缩略图的宽度
public $thumbWidth;
//缩略图的高度
public $thumbHeight;
//生成缩略图文件名后缀
public $thumbEndFix;
//缩略图文件前缀
public $thumbPreFix;
/**
* 构造函数
*/
public function __construct(){
$this->thumbType = 1;
$this->thumbWidth = 120;
$this->thumbHeight = 60;
$this->thumbPreFix ='';
$this->thumbEndFix = '_thumb';
}
/**
* 检测是否为图像文件
* @param $img 图像
* @return bool
*/
private function check($img){
$type = array(".jpg", ".jpeg", ".png", ".gif");
$imgType = strtolower(strrchr($img, '.'));
return extension_loaded('gd') && file_exists($img) && in_array($imgType, $type);
}
/**
* 获得缩略图的尺寸信息
* @param $imgWidth 原图宽度
* @param $imgHeight 原图高度
* @param $thumbWidth 缩略图宽度
* @param $thumbHeight 缩略图的高度
* @param $thumbType 处理方式
* 1 固定宽度 高度自增 2固定高度 宽度自增 3固定宽度 高度裁切
* 4 固定高度 宽度裁切 5缩放最大边 原图不裁切
* @return mixed
*/
private function thumbSize($imgWidth, $imgHeight, $thumbWidth, $thumbHeight, $thumbType){
//初始化缩略图尺寸
$w = $thumbWidth;
$h = $thumbHeight;
//初始化原图尺寸
$cuthumbWidth = $imgWidth;
$cuthumbHeight = $imgHeight;
switch ($thumbType) {
case 1 :
//固定宽度 高度自增
$h = $thumbWidth / $imgWidth * $imgHeight;
break;
case 2 :
//固定高度 宽度自增
$w = $thumbHeight / $imgHeight * $imgWidth;
break;
case 3 :
//固定宽度 高度裁切
$cuthumbHeight = $imgWidth / $thumbWidth * $thumbHeight;
break;
case 4 :
//固定高度 宽度裁切
$cuthumbWidth = $imgHeight / $thumbHeight * $thumbWidth;
break;
case 5 :
//缩放最大边 原图不裁切
if (($imgWidth / $thumbWidth) > ($imgHeight / $thumbHeight)) {
$h = $thumbWidth / $imgWidth * $imgHeight;
} elseif (($imgWidth / $thumbWidth) < ($imgHeight / $thumbHeight)) {
$w = $thumbHeight / $imgHeight * $imgWidth;
} else {
$w = $thumbWidth;
$h = $thumbHeight;
}
break;
default:
//缩略图尺寸不变,自动裁切图片
if (($imgHeight / $thumbHeight) < ($imgWidth / $thumbWidth)) {
$cuthumbWidth = $imgHeight / $thumbHeight * $thumbWidth;
} elseif (($imgHeight / $thumbHeight) > ($imgWidth / $thumbWidth)) {
$cuthumbHeight = $imgWidth / $thumbWidth * $thumbHeight;
}
// }
}
$arr [0] = $w;
$arr [1] = $h;
$arr [2] = $cuthumbWidth;
$arr [3] = $cuthumbHeight;
return $arr;
}
/**
* 图片裁切处理
* @param $img 原图
* @param string $outFile 另存文件名
* @param string $thumbWidth 缩略图宽度
* @param string $thumbHeight 缩略图高度
* @param string $thumbType 裁切图片的方式
* 1 固定宽度 高度自增 2固定高度 宽度自增 3固定宽度 高度裁切
* 4 固定高度 宽度裁切 5缩放最大边 原图不裁切 6缩略图尺寸不变,自动裁切最大边
* @return bool|string
*/
public function thumb($img, $outFile = '', $thumbWidth = '', $thumbHeight = '', $thumbType = ''){
if (!$this->check($img)) {
return false;
}
//基础配置
$thumbType = $thumbType ? $thumbType : $this->thumbType;
$thumbWidth = $thumbWidth ? $thumbWidth : $this->thumbWidth;
$thumbHeight = $thumbHeight ? $thumbHeight : $this->thumbHeight;
//获得图像信息
$imgInfo = getimagesize($img);
$imgWidth = $imgInfo [0];
$imgHeight = $imgInfo [1];
$imgType = image_type_to_extension($imgInfo [2]);
//获得相关尺寸
$thumb_size = $this->thumbSize($imgWidth, $imgHeight, $thumbWidth, $thumbHeight, $thumbType);
//原始图像资源
$func = "imagecreatefrom" . substr($imgType, 1);
$resImg = $func($img);
//缩略图的资源
if ($imgType == '.gif') {
$res_thumb = imagecreate($thumb_size [0], $thumb_size [1]);
$color = imagecolorallocate($res_thumb, 255, 0, 0);
} else {
$res_thumb = imagecreatetruecolor($thumb_size [0], $thumb_size [1]);
imagealphablending($res_thumb, false); //关闭混色
imagesavealpha($res_thumb, true); //储存透明通道
}
//绘制缩略图X
if (function_exists("imagecopyresampled")) {
imagecopyresampled($res_thumb, $resImg, 0, 0, 0, 0, $thumb_size [0], $thumb_size [1], $thumb_size [2], $thumb_size [3]);
} else {
imagecopyresized($res_thumb, $resImg, 0, 0, 0, 0, $thumb_size [0], $thumb_size [1], $thumb_size [2], $thumb_size [3]);
}
//处理透明色
if ($imgType == '.gif') {
imagecolortransparent($res_thumb, $color);
}
//配置输出文件名
$imgInfo = pathinfo($img);
$outFile = $outFile ? $outFile :dirname($img).'/'. $this->thumbPreFix . $imgInfo['filename'] . $this->thumbEndFix . "." . $imgInfo['extension'];
Files::create(dirname($outFile));
$func = "image" . substr($imgType, 1);
$func($res_thumb, $outFile);
if (isset($resImg))
imagedestroy($resImg);
if (isset($res_thumb))
imagedestroy($res_thumb);
return $outFile;
}
}
追问
怎么调用?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询