php图片压缩上传-PHP、HTML5上传图片自动压缩问题
php 怎么压缩图片后 在发给前端

<?php
/*
----------------------------------------------------------------------
函数:调整图片尺寸或生成缩略图
返回:True/False
参数:
$Image 需要调整的图片(含路径)
$Dw=450 调整时最大宽度;缩略图时的绝对宽度
$Dh=450 调整时最大高度;缩略图时的绝对高度
$Type=1 1,调整尺寸; 2,生成缩略图
$path='img/';//路径
$phtypes=array(
'img/gif',
'img/jpg',
'img/jpeg',
'img/bmp',
'img/pjpeg',
'img/x-png'
);
Function Img($Image,$Dw=450,$Dh=450,$Type=1){
IF(!File_Exists($Image)){
Return False;
}
//如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值
IF($Type!=1){
Copy($Image,Str_Replace(".","_x.",$Image));
$Image=Str_Replace(".","_x.",$Image);
}
//取得文件的类型,根据不同的类型建立不同的对象
$ImgInfo=GetImageSize($Image);
Switch($ImgInfo[2]){
Case 1:
$Img = @ImageCreateFromGIF($Image);
Break;
Case 2:
$Img = @ImageCreateFromJPEG($Image);
Break;
Case 3:
$Img = @ImageCreateFromPNG($Image);
Break;
}
//如果对象没有创建成功,则说明非图片文件
IF(Empty($Img)){
//如果是生成缩略图的时候出错,则需要删掉已经复制的文件
IF($Type!=1){Unlink($Image);}
Return False;
}
//如果是执行调整尺寸操作则
IF($Type==1){
$w=ImagesX($Img);
$h=ImagesY($Img);
$width = $w;
$height = $h;
IF($width>$Dw){
$Par=$Dw/$width;
$width=$Dw;
$height=$height*$Par;
IF($height>$Dh){
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
}
}ElseIF($height>$Dh){
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
IF($width>$Dw){
$Par=$Dw/$width;
$width=$Dw;
$height=$height*$Par;
}
}Else{
$width=$width;
$height=$height;
}
$nImg = ImageCreateTrueColor($width,$height); //新建一个真彩色画布
ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);//重采样拷贝部分图像并调整大小
ImageJpeg ($nImg,$Image); //以JPEG格式将图像输出到浏览器或文件
Return True;
//如果是执行生成缩略图操作则
}Else{
$w=ImagesX($Img);
$h=ImagesY($Img);
$width = $w;
$height = $h;
$nImg = ImageCreateTrueColor($Dw,$Dh);
IF($h/$w>$Dh/$Dw){ //高比较大
$width=$Dw;
$height=$h*$Dw/$w;
$IntNH=$height-$Dh;
ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
}Else{ //宽比较大
$height=$Dh;
$width=$w*$Dh/$h;
$IntNW=$width-$Dw;
ImageCopyReSampled($nImg, $Img, -$IntNW/1.8, 0, 0, 0, $width, $Dh, $w, $h);
}
ImageJpeg ($nImg,$Image);
Return True;
}
}
?>
<html><body>
<form method="post" enctype="multipart/form-data" name="form1">
<table>
<tr><td>上传图片</td></tr>
<tr><td><input type="file" name="photo" size="20" /></td></tr>
<tr><td><input type="submit" value="上传"/></td></tr>
</table>
允许上传的文件类型为:<?=implode(', ',$phtypes)?></form>
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
if (!is_uploaded_file($_FILES["photo"][tmp_name])){
echo "图片不存在";
exit();
}
if(!is_dir('img')){//路径若不存在则创建
mkdir('img');
}
$upfile=$_FILES["photo"];
$pinfo=pathinfo($upfile["name"]);
$name=$pinfo['basename'];//文件名
$tmp_name=$upfile["tmp_name"];
$file_type=$pinfo['extension'];//获得文件类型
$showphpath=$path.$name;
if(in_array($upfile["type"],$phtypes)){
echo "文件类型不符!";
exit();
}
if(move_uploaded_file($tmp_name,$path.$name)){
echo "成功!";
Img($showphpath,100,800,2);
}
echo "<img src=\"".$showphpath."\" />";
}
?>
</body>
</html>
如何利用php把上传的图片压缩

<?php
// The file
$filename = '';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>
PHP、HTML5上传图片自动压缩问题

给你个图片处理的类吧,图片剪裁处理后,也就等于将图片压缩了。
/**
* 图像处理类
* ============================================================================
* Copyright 2014 大秦科技,并保留所有权利。
* 网站地址: ;
* ============================================================================
*/
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;
}
}
-
果盘绘画图片大全简单创作方法快速制作技巧2024-01-02 16:57:54
-
小年插画图片创作方法快速制作技巧2024-01-02 16:57:20
-
小年绘画作品制作技巧2024-01-02 16:55:45
-
小年插画图片创作方法快速制作技巧2024-01-02 16:55:02
-
立春插画图片如何制作2024-01-02 16:54:19
-
立春插画图片创作方法快速制作技巧2024-01-02 16:53:32






扫描二维码添加客服微信
扫描二维码关注