标签:
<?php
/**
* 图片压缩
* @authors lxw
* @date 2015-10-20
* @version lxw_zoom_file 1.0
* 如果需要封装成函数或类请自行封装
*/
header("Content-type:text/html;charset=utf-8");
$max = 400;
$file_dir = ‘../upload/‘;
$info = $_FILES[‘file‘];
switch ($info[‘error‘]) {
case 1:
echo ‘文件超过服务器限制!‘;
die();
break;
case 2:
echo ‘文件超过浏览器限制!‘;
die();
break;
case 3:
echo ‘部分文件被上传!‘;
die();
break;
case 4:
echo ‘没有找到要上传的文件!‘;
die();
break;
case 5:
echo ‘服务器临时文件夹丢失!‘;
die();
break;
case 6:
echo ‘文件写入到临时文件夹出错!‘;
die();
break;
}
$suffix = explode(‘.‘, $info[‘name‘]);
switch ($suffix[1]) {
case ‘jpg‘:
$im = imagecreatefromjpeg($info[‘tmp_name‘]);
break;
case ‘png‘:
$im = imagecreatefrompng($info[‘tmp_name‘]);
break;
case ‘gif‘:
$im = imagecreatefromgif($info[‘tmp_name‘]);
break;
default:
$im = imagecreatefromjpeg($info[‘tmp_name‘]);
break;
}
$pic_width = imagesx($im);
$pic_height = imagesy($im);
if ($pic_width > $max) {
$ratio = $max / $pic_width;
} else if ($pic_width <= $max) {
$ratio = 1;
}
$new_width = $pic_width * $ratio;
$new_height = $pic_height * $ratio;
$newim = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($newim, $im, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);
$file_name = $info[‘name‘];
$new_name = $file_dir . $file_name;
imagejpeg($newim, $new_name);
imagedestroy($newim);
标签:
原文地址:http://my.oschina.net/jackraul/blog/520936