需要使用gd库的方法 php需要引入gd扩展支持
/*
* 图片压缩
----------------------------------------------------------------------
函数:调整图片尺寸或生成缩略图
返回: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'
返回图片名称,不含路径
);
*/
public function Img($Image,$exif=0,$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;
}
//如果是执行调整尺寸操作则
$w=imagesx($Img);
$h=imagesy($Img);
$width = $w;
$height = $h;
if($Type==1){
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;
}else{
$width=$width;
$height=$height;
}
if($ImgInfo[2] == 3)
{
imagesavealpha($Img,true);
}
$nImg = imagecreatetruecolor($width,$height); //新建一个真彩色画布
if($ImgInfo[2] == 3)
{
imagealphablending($nImg,false);
imagesavealpha($nImg,true);
} imagecopyresampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);//重采样拷贝部分图像并调整大小
//如果是执行生成缩略图操作则
}else{
$nImg = imagecreatetruecolor($Dw,$Dh);
if($h/$w>$Dh/$Dw){ //高比较大
$height=$h*$Dw/$w;
$IntNH=$height-$Dh;
imagecopyresampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
}else{ //宽比较大
$width=$w*$Dh/$h;
$IntNW=$width-$Dw;
imagecopyresampled($nImg, $Img, -$IntNW/1.8, 0, 0, 0, $width, $Dh, $w, $h);
}
}
if(!empty($exif))
{
switch ($exif) {
case 8:
$image = imagerotate($nImg, 90, 0);
break;
case 3:
$image = imagerotate($nImg, 180, 0);
break;
case 6:
$image = imagerotate($nImg, -90, 0);
break;
}
}
switch($ImgInfo[2]){
case 1:
imagegif($nImg,$Image);
Break;
case 2:
imagejpeg($nImg,$Image);
Break;
case 3:
imagepng($nImg,$Image);
Break;
} $fileDirArr = explode('/',$Image);
return end($fileDirArr);
}

最新文章

  1. 开发者的利器:Docker 理解与使用
  2. Android中使用Notification实现宽视图通知栏(Notification示例二)
  3. 全面分析Java的垃圾回收机制
  4. Web应用安全之文件上传漏洞详解
  5. js 检查是否为手机端
  6. 用于Simple.Data的ASP.NET Identity Provider
  7. C写的扫描器源码
  8. Java之谜 —— 来自Neal Gafter的演讲
  9. springboot中关闭eureka server中已注册服务列表自我保护配置
  10. 带分页功能的SSH整合,DAO层经典封装
  11. ethereum/EIPs-1193 Ethereum Provider JavaScript API 如metamask更新后的接口
  12. PSP个人项目耗时对比记录表:四则运算
  13. Git学习笔记(三)
  14. 用xshell ssh连接测试服务器时候出的问题
  15. [转]VS 2010 : 如何开发和部署Outlook 2010插件(Add-in)
  16. UVA 10194 (13.08.05)
  17. STM32F429I-DISCO 和GPS的亲热接触
  18. (二)Spring 之IOC 详解
  19. 安全篇:弱密码python检测工具
  20. 关于自适应屏幕,设置子元素浮动,父div不能包裹子div,子元素中内容溢出的问题。

热门文章

  1. 最短路之SPFA(单源)HDU 2544
  2. ZOJ Seven-Segment Display 暴力dfs + 剪枝
  3. 移动端 mui框架中input输入框或任何输入框聚焦后页面自动上移
  4. 持续集成~Jenkins里的NuGet和MSBuild插件
  5. react注意点
  6. HTTP/1.1 持久连接 persistent connection
  7. 初探ant design pro
  8. Servlet和JSP之JSTL学习
  9. Codeforces Round #318 (Div. 2) C Bear and Poker (数学)
  10. WPF知识点全攻略02- WPF体系结构