PHP实现图片压缩的两则实例


Posted in PHP onJuly 19, 2014

本文介绍了PHP实现图片压缩的两种方法,读者可以根据具体应用参考或加以改进,以适应自身应用需求!废话不多说,主要代码部分如下:

实例1:

<?php 
/** 
* desription 压缩图片 
* @param sting $imgsrc 图片路径 
* @param string $imgdst 压缩后保存路径 
*/
function image_png_size_add($imgsrc,$imgdst){ 
  list($width,$height,$type)=getimagesize($imgsrc); 
  $new_width = ($width>600?600:$width)*0.9; 
  $new_height =($height>600?600:$height)*0.9; 
  switch($type){ 
    case 1: 
      $giftype=check_gifcartoon($imgsrc); 
      if($giftype){ 
        header('Content-Type:image/gif'); 
        $image_wp=imagecreatetruecolor($new_width, $new_height); 
        $image = imagecreatefromgif($imgsrc); 
        imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
        imagejpeg($image_wp, $imgdst,75); 
        imagedestroy($image_wp); 
      } 
      break; 
    case 2: 
      header('Content-Type:image/jpeg'); 
      $image_wp=imagecreatetruecolor($new_width, $new_height); 
      $image = imagecreatefromjpeg($imgsrc); 
      imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
      imagejpeg($image_wp, $imgdst,75); 
      imagedestroy($image_wp); 
      break; 
    case 3: 
      header('Content-Type:image/png'); 
      $image_wp=imagecreatetruecolor($new_width, $new_height); 
      $image = imagecreatefrompng($imgsrc); 
      imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
      imagejpeg($image_wp, $imgdst,75); 
      imagedestroy($image_wp); 
      break; 
  } 
} 
/** 
* desription 判断是否gif动画 
* @param sting $image_file图片路径 
* @return boolean t 是 f 否 
*/
function check_gifcartoon($image_file){ 
  $fp = fopen($image_file,'rb'); 
  $image_head = fread($fp,1024); 
  fclose($fp); 
  return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)?false:true; 
} 
?>

实例2:

<?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垃圾回收机制简单说明
Jul 22 PHP
PHP三元运算的2种写法代码实例
May 12 PHP
ThinkPHP分组下自定义标签库实例
Nov 01 PHP
PHP读取配置文件类实例(可读取ini,yaml,xml等)
Jul 28 PHP
PHP数组编码gbk与utf8互相转换的两种方法
Sep 01 PHP
PHP类型约束用法示例
Sep 28 PHP
php 修改上传文件大小限制实例详解
Oct 23 PHP
利用PHP访问带有密码的Redis方法示例
Feb 09 PHP
php记录搜索引擎爬行记录的实现代码
Mar 02 PHP
PHP数据对象映射模式实例分析
Mar 29 PHP
PHP实现提高SESSION响应速度的几种方法详解
Aug 09 PHP
Laravel模糊查询区分大小写的实例
Sep 29 PHP
PHP简单实现“相关文章推荐”功能的方法
Jul 19 #PHP
php实现获取局域网所有用户的电脑IP和主机名、及mac地址完整实例
Jul 18 #PHP
CodeIgniter中使用cookie的三种方式详解
Jul 18 #PHP
CodeIgniter采用config控制的多语言实现根据浏览器语言自动转换功能
Jul 18 #PHP
简单实用的网站PHP缓存类实例
Jul 18 #PHP
ThinkPHP表单自动提交验证实例教程
Jul 18 #PHP
ThinkPHP采用实现三级循环代码实例
Jul 18 #PHP
You might like
第十四节 命名空间 [14]
2006/10/09 PHP
php递归函数中使用return的注意事项
2014/01/17 PHP
举例详解PHP脚本的测试方法
2015/08/05 PHP
PHP 匿名函数与注意事项详细介绍
2016/11/26 PHP
PHP安装BCMath扩展的方法
2019/02/13 PHP
Laravel 修改默认日志文件名称和位置的例子
2019/10/17 PHP
jquery判断字符输入个数(数字英文长度记为1,中文记为2,超过长度自动截取)
2010/10/15 Javascript
jQuery中的ajax async同步和异步详解
2015/09/29 Javascript
AngularJS中的按需加载ocLazyLoad示例
2017/01/11 Javascript
详解vue表单验证组件 v-verify-plugin
2017/04/19 Javascript
基于JSONP原理解析(推荐)
2017/12/04 Javascript
使用webpack搭建react开发环境的方法
2018/05/15 Javascript
Element Alert警告的具体使用方法
2020/07/27 Javascript
vue-cli3项目打包后自动化部署到服务器的方法
2020/09/16 Javascript
[01:03]DOTA2新的征程 你的脚印值得踏上
2014/08/13 DOTA
详解Python中的from..import绝对导入语句
2016/06/21 Python
Python正则表达式如何进行字符串替换实例
2016/12/28 Python
异步任务队列Celery在Django中的使用方法
2018/06/07 Python
Python3爬虫学习之爬虫利器Beautiful Soup用法分析
2018/12/12 Python
python读取当前目录下的CSV文件数据
2020/03/11 Python
Shopee马来西亚:随拍即卖,最佳行动电商拍卖平台
2017/06/05 全球购物
Wedgwood英国官方网站:英式精致骨瓷餐具、礼品与生活精品,源于1759年
2019/09/02 全球购物
毕业生求职推荐信
2013/11/04 职场文书
车间主管岗位职责
2013/11/14 职场文书
银行实习生的自我评价
2013/12/09 职场文书
经理任命书模板
2014/06/06 职场文书
服务宗旨标语
2014/07/01 职场文书
机关作风整顿个人整改措施思想汇报
2014/09/29 职场文书
教师批评与自我批评材料
2014/10/16 职场文书
2015年银行员工工作总结
2015/04/24 职场文书
开票证明
2015/06/23 职场文书
教师教育教学随笔
2015/08/15 职场文书
《打电话》教学反思
2016/02/22 职场文书
Mysql数据库按时间点恢复实战记录
2021/06/30 MySQL
Redis之RedisTemplate配置方式(序列和反序列化)
2022/03/13 Redis
webpack介绍使用配置教程详解webpack介绍和使用
2022/06/25 Javascript