php gd等比例缩放压缩图片函数


Posted in PHP onJune 12, 2016

本文实例为大家分享了php gd等比例缩放压缩图片函数,供大家参考,具体内容如下

<?php
  /**
   * 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;
  }

  /**
  * desription 压缩图片
  * @param sting $imgsrc 图片路径
  * @param string $imgdst 压缩后保存路径
  */
  function compressed_image($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);
          //75代表的是质量、压缩图片容量大小
          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);
        //75代表的是质量、压缩图片容量大小
        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);
        //75代表的是质量、压缩图片容量大小
        imagejpeg($image_wp, $imgdst,75);
        imagedestroy($image_wp);
        break;
    }
  }

php and gd 函数参考表
gd_info 取得当前安装的 GD 库的信息
getimagesize 取得图像大小
getimagesizefromstring Get the size of an image from a string
image_type_to_extension 取得图像类型的文件后缀
image_type_to_mime_type 取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的图像类型的 MIME 类型
image2wbmp 以 WBMP 格式将图像输出到浏览器或文件
imageaffine Return an image containing the affine tramsformed src image, using an optional clipping area
imageaffinematrixconcat Concat two matrices (as in doing many ops in one go)
imageaffinematrixget Return an image containing the affine tramsformed src image, using an optional clipping area
imagealphablending 设定图像的混色模式
imageantialias 是否使用抗锯齿(antialias)功能
imagearc 画椭圆弧
imagechar 水平地画一个字符
imagecharup 垂直地画一个字符
imagecolorallocate 为一幅图像分配颜色
imagecolorallocatealpha 为一幅图像分配颜色 + alpha
imagecolorat 取得某像素的颜色索引值
imagecolorclosest 取得与指定的颜色最接近的颜色的索引值
imagecolorclosestalpha 取得与指定的颜色加透明度最接近的颜色
imagecolorclosesthwb 取得与给定颜色最接近的色度的黑白色的索引
imagecolordeallocate 取消图像颜色的分配
imagecolorexact 取得指定颜色的索引值
imagecolorexactalpha 取得指定的颜色加透明度的索引值
imagecolormatch 使一个图像中调色板版本的颜色与真彩色版本更能匹配
imagecolorresolve 取得指定颜色的索引值或有可能得到的最接近的替代值
imagecolorresolvealpha 取得指定颜色 + alpha 的索引值或有可能得到的最接近的替代值
imagecolorset 给指定调色板索引设定颜色
imagecolorsforindex 取得某索引的颜色
imagecolorstotal 取得一幅图像的调色板中颜色的数目
imagecolortransparent 将某个颜色定义为透明色
imageconvolution 用系数 div 和 offset 申请一个 3x3 的卷积矩阵
imagecopy 拷贝图像的一部分
imagecopymerge 拷贝并合并图像的一部分
imagecopymergegray 用灰度拷贝并合并图像的一部分
imagecopyresampled 重采样拷贝部分图像并调整大小
imagecopyresized 拷贝部分图像并调整大小
imagecreate 新建一个基于调色板的图像
imagecreatefromgd2 从 GD2 文件或 URL 新建一图像
imagecreatefromgd2part 从给定的 GD2 文件或 URL 中的部分新建一图像
imagecreatefromgd 从 GD 文件或 URL 新建一图像
imagecreatefromgif 由文件或 URL 创建一个新图象。
imagecreatefromjpeg 由文件或 URL 创建一个新图象。
imagecreatefrompng 由文件或 URL 创建一个新图象。
imagecreatefromstring 从字符串中的图像流新建一图像
imagecreatefromwbmp 由文件或 URL 创建一个新图象。
imagecreatefromwebp 由文件或 URL 创建一个新图象。
imagecreatefromxbm 由文件或 URL 创建一个新图象。
imagecreatefromxpm 由文件或 URL 创建一个新图象。
imagecreatetruecolor 新建一个真彩色图像
imagecrop Crop an image using the given coordinates and size, x, y, width and height
imagecropauto Crop an image automatically using one of the available modes
imagedashedline 画一虚线
imagedestroy 销毁一图像
imageellipse 画一个椭圆
imagefill 区域填充
imagefilledarc 画一椭圆弧且填充
imagefilledellipse 画一椭圆并填充
imagefilledpolygon 画一多边形并填充
imagefilledrectangle 画一矩形并填充
imagefilltoborder 区域填充到指定颜色的边界为止
imagefilter 对图像使用过滤器
imageflip Flips an image using a given mode
imagefontheight 取得字体高度
imagefontwidth 取得字体宽度
imageftbbox 给出一个使用 FreeType 2 字体的文本框
imagefttext 使用 FreeType 2 字体将文本写入图像
imagegammacorrect 对 GD 图像应用 gamma 修正
imagegd2 将 GD2 图像输出到浏览器或文件
imagegd 将 GD 图像输出到浏览器或文件
imagegif 输出图象到浏览器或文件。
imagegrabscreen Captures the whole screen
imagegrabwindow Captures a window
imageinterlace 激活或禁止隔行扫描
imageistruecolor 检查图像是否为真彩色图像
imagejpeg 输出图象到浏览器或文件。
imagelayereffect 设定 alpha 混色标志以使用绑定的 libgd 分层效果
imageline 画一条线段
imageloadfont 载入一新字体
imagepalettecopy 将调色板从一幅图像拷贝到另一幅
imagepalettetotruecolor Converts a palette based image to true color
imagepng 以 PNG 格式将图像输出到浏览器或文件
imagepolygon 画一个多边形
imagepsbbox 给出一个使用 PostScript Type1 字体的文本方框
imagepsencodefont 改变字体中的字符编码矢量
imagepsextendfont 扩充或精简字体
imagepsfreefont 释放一个 PostScript Type 1 字体所占用的内存
imagepsloadfont 从文件中加载一个 PostScript Type 1 字体
imagepsslantfont 倾斜某字体
imagepstext 用 PostScript Type1 字体把文本字符串画在图像上
imagerectangle 画一个矩形
imagerotate 用给定角度旋转图像
imagesavealpha 设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息(与单一透明色相反)
imagescale Scale an image using the given new width and height
imagesetbrush 设定画线用的画笔图像
imagesetinterpolation Set the interpolation method
imagesetpixel 画一个单一像素
imagesetstyle 设定画线的风格
imagesetthickness 设定画线的宽度
imagesettile 设定用于填充的贴图
imagestring 水平地画一行字符串
imagestringup 垂直地画一行字符串
imagesx 取得图像宽度
imagesy 取得图像高度
imagetruecolortopalette 将真彩色图像转换为调色板图像
imagettfbbox 取得使用 TrueType 字体的文本的范围
imagettftext 用 TrueType 字体向图像写入文本
imagetypes 返回当前 PHP 版本所支持的图像类型
imagewbmp 以 WBMP 格式将图像输出到浏览器或文件
imagewebp Output an WebP image to browser or file
imagexbm 将 XBM 图像输出到浏览器或文件
iptcembed 将二进制 IPTC 数据嵌入到一幅 JPEG 图像中
iptcparse 将二进制 IPTC 块解析为单个标记
jpeg2wbmp 将 JPEG 图像文件转换为 WBMP 图像文件
png2wbmp 将 PNG 图像文件转换为 WBMP 图像文件

以上就是本文的全部内容,希望对大家学习PHP程序设计有所帮助。

PHP 相关文章推荐
php日历制作代码分享
Jan 20 PHP
深入理解PHP中的global
Aug 19 PHP
从零开始学YII2框架(一)通过Composer安装Yii2框架
Aug 20 PHP
php中base_convert()进制数字转换函数实例
Nov 20 PHP
PHP的pcntl多进程用法实例
Mar 19 PHP
Symfony2安装第三方Bundles实例详解
Feb 04 PHP
php unicode编码和字符串互转的方法
Aug 12 PHP
基于thinkPHP3.2实现微信接入及查询token值的方法
Apr 18 PHP
Ecshop 后台添加新功能栏目及管理权限设置教程
Nov 21 PHP
php实现mysql连接池效果实现代码
Jan 25 PHP
thinkPHP3.2.3结合Laypage实现的分页功能示例
May 28 PHP
thinkphp5.1 框架导入/导出excel文件操作示例
May 25 PHP
详解PHP中cookie和session的区别及cookie和session用法小结
Jun 12 #PHP
yii2中结合gridview如何使用modal弹窗实例代码详解
Jun 12 #PHP
最新最全PHP生成制作验证码代码详解(推荐)
Jun 12 #PHP
再谈PHP中单双引号的区别详解
Jun 12 #PHP
PHP中strpos、strstr和stripos、stristr函数分析
Jun 11 #PHP
linux下php上传文件注意事项
Jun 11 #PHP
php设计模式之单例模式代码
Jun 11 #PHP
You might like
操作Oracle的php类
2006/10/09 PHP
使用 MySQL 开始 PHP 会话
2006/12/21 PHP
PHP 5.3.0 安装分析心得
2009/08/07 PHP
PHP读取XML值的代码(推荐)
2011/01/01 PHP
php中通过正则表达式下载内容中的远程图片的函数代码
2012/01/10 PHP
php获取从html表单传递数组的方法
2015/03/20 PHP
必须收藏的23个php实用代码片段
2016/02/02 PHP
PHP对称加密算法(DES/AES)类的实现代码
2017/11/14 PHP
表单提交时自动复制内容到剪贴板的js代码
2007/03/16 Javascript
JavaScript Eval 函数使用
2010/03/23 Javascript
模拟多级复选框效果的jquery代码
2013/08/13 Javascript
jQuery鼠标经过方形图片切换成圆边效果代码分享
2015/08/20 Javascript
深入浅析javascript中的作用域(推荐)
2016/07/19 Javascript
JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)
2016/12/14 Javascript
使用ionic切换页面卡顿的解决方法
2016/12/16 Javascript
js实现本地时间同步功能
2017/08/26 Javascript
详解性能更优越的小程序图片懒加载方式
2018/07/18 Javascript
VSCode搭建Vue项目的方法
2020/04/30 Javascript
VUE子组件向父组件传值详解(含传多值及添加额外参数场景)
2020/09/01 Javascript
[49:08]完美世界DOTA2联赛PWL S2 LBZS vs FTD.C 第一场 11.27
2020/12/01 DOTA
python使用新浪微博api上传图片到微博示例
2014/01/10 Python
python中使用smtplib和email模块发送邮件实例
2014/04/22 Python
浅谈python 线程池threadpool之实现
2017/11/17 Python
在Pandas中给多层索引降级的方法
2018/11/16 Python
Django中更改默认数据库为mysql的方法示例
2018/12/05 Python
在linux系统下安装python librtmp包的实现方法
2019/07/22 Python
Python实现将蓝底照片转化为白底照片功能完整实例
2019/12/13 Python
PyCharm无法引用自身项目解决方式
2020/02/12 Python
巾帼建功标兵事迹材料
2014/05/11 职场文书
红楼梦读书笔记
2015/06/25 职场文书
mysql查询的控制语句图文详解
2021/04/11 MySQL
详解Javascript实践中的命令模式
2021/05/05 Javascript
80行代码写一个Webpack插件并发布到npm
2021/05/24 Javascript
Javascript设计模式之原型模式详细
2021/10/05 Javascript
Java基础——Map集合
2022/04/01 Java/Android
win10滚动条自动往上跑怎么办?win10滚动条自动往上跑的解决方法
2022/08/05 数码科技