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

我赞美你品格高尚,崇敬你洁白无瑕。我爱你、想你、盼你,像对每一个季节那样。我爱你、想你、盼你,不管世俗的偏见怎样厉害。冬――四季之一的冬,你来吧!我喜欢你纯净的身躯,喜欢你严厉的性格,我要在你的怀抱中锻炼、奋斗、成熟……你可以和春天的万花,夏天的麦浪,秋天的瓜果……比美!

本文实例为大家分享了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取得图像大小
getimagesizefromstringGet 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 格式将图像输出到浏览器或文件
imageaffineReturn an image containing the affine tramsformed src image, using an optional clipping area
imageaffinematrixconcatConcat two matrices (as in doing many ops in one go)
imageaffinematrixgetReturn 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新建一个真彩色图像
imagecropCrop an image using the given coordinates and size, x, y, width and height
imagecropautoCrop an image automatically using one of the available modes
imagedashedline画一虚线
imagedestroy销毁一图像
imageellipse画一个椭圆
imagefill区域填充
imagefilledarc画一椭圆弧且填充
imagefilledellipse画一椭圆并填充
imagefilledpolygon画一多边形并填充
imagefilledrectangle画一矩形并填充
imagefilltoborder区域填充到指定颜色的边界为止
imagefilter对图像使用过滤器
imageflipFlips an image using a given mode
imagefontheight取得字体高度
imagefontwidth取得字体宽度
imageftbbox给出一个使用 FreeType 2 字体的文本框
imagefttext使用 FreeType 2 字体将文本写入图像
imagegammacorrect对 GD 图像应用 gamma 修正
imagegd2将 GD2 图像输出到浏览器或文件
imagegd将 GD 图像输出到浏览器或文件
imagegif输出图象到浏览器或文件。
imagegrabscreenCaptures the whole screen
imagegrabwindowCaptures a window
imageinterlace激活或禁止隔行扫描
imageistruecolor检查图像是否为真彩色图像
imagejpeg输出图象到浏览器或文件。
imagelayereffect设定 alpha 混色标志以使用绑定的 libgd 分层效果
imageline画一条线段
imageloadfont载入一新字体
imagepalettecopy将调色板从一幅图像拷贝到另一幅
imagepalettetotruecolorConverts 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 通道信息(与单一透明色相反)
imagescaleScale an image using the given new width and height
imagesetbrush设定画线用的画笔图像
imagesetinterpolationSet the interpolation method
imagesetpixel画一个单一像素
imagesetstyle设定画线的风格
imagesetthickness设定画线的宽度
imagesettile设定用于填充的贴图
imagestring水平地画一行字符串
imagestringup垂直地画一行字符串
imagesx取得图像宽度
imagesy取得图像高度
imagetruecolortopalette将真彩色图像转换为调色板图像
imagettfbbox取得使用 TrueType 字体的文本的范围
imagettftext用 TrueType 字体向图像写入文本
imagetypes返回当前 PHP 版本所支持的图像类型
imagewbmp以 WBMP 格式将图像输出到浏览器或文件
imagewebpOutput an WebP image to browser or file
imagexbm将 XBM 图像输出到浏览器或文件
iptcembed将二进制 IPTC 数据嵌入到一幅 JPEG 图像中
iptcparse将二进制 IPTC 块解析为单个标记
jpeg2wbmp将 JPEG 图像文件转换为 WBMP 图像文件
png2wbmp将 PNG 图像文件转换为 WBMP 图像文件

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

您可能有感兴趣的文章
PHP等比例压缩图片的实例代码

php实现等比例压缩图片

php等比例缩放图片及剪切图片代码分享

PHP性能优化案例分享

PHP实现短信验证码的发送次数限制