PHP实现对png图像进行缩放的方法(支持透明背景)


Posted in PHP onJuly 15, 2015

本文实例讲述了PHP实现对png图像进行缩放的方法。分享给大家供大家参考。具体实现方法如下:

function smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = 'file', $delete_original = true, $use_linux_commands = false )
{
    if ( $height <= 0 && $width <= 0 ) {
      return false;
    }
    $info = getimagesize($file);
    $image = '';
    $final_width = 0;
    $final_height = 0;
    list($width_old, $height_old) = $info;
    if ($proportional) {
      if ($width == 0) $factor = $height/$height_old;
      elseif ($height == 0) $factor = $width/$width_old;
      else $factor = min ( $width / $width_old, $height / $height_old); 
      $final_width = round ($width_old * $factor);
      $final_height = round ($height_old * $factor);
    }
    else {    
      $final_width = ( $width <= 0 ) ? $width_old : $width;
      $final_height = ( $height <= 0 ) ? $height_old : $height;
    }
    switch ($info[2] ) {
      case IMAGETYPE_GIF:
        $image = imagecreatefromgif($file);
      break;
      case IMAGETYPE_JPEG:
        $image = imagecreatefromjpeg($file);
      break;
      case IMAGETYPE_PNG:
        $image = imagecreatefrompng($file);
      break;
      default:
        return false;
    }
    $image_resized = imagecreatetruecolor( $final_width, $final_height );
    if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
      $trnprt_indx = imagecolortransparent($image);
      // If we have a specific transparent color
      if ($trnprt_indx >= 0) {
        // Get the original image's transparent color's RGB values
        $trnprt_color  = imagecolorsforindex($image, $trnprt_indx);
        // Allocate the same color in the new image resource
        $trnprt_indx  = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
        // Completely fill the background of the new image with allocated color.
        imagefill($image_resized, 0, 0, $trnprt_indx);
        // Set the background color for new image to transparent
        imagecolortransparent($image_resized, $trnprt_indx);
      }
      // Always make a transparent background color for PNGs that don't have one allocated already
      elseif ($info[2] == IMAGETYPE_PNG) {
        // Turn off transparency blending (temporarily)
        imagealphablending($image_resized, false);
        // Create a new transparent color for image
        $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
        // Completely fill the background of the new image with allocated color.
        imagefill($image_resized, 0, 0, $color);
        // Restore transparency blending
        imagesavealpha($image_resized, true);
      }
    }
    imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
    if ( $delete_original ) {
      if ( $use_linux_commands )
        exec('rm '.$file);
      else
        @unlink($file);
    }
    switch ( strtolower($output) ) {
      case 'browser':
        $mime = image_type_to_mime_type($info[2]);
        header("Content-type: $mime");
        $output = NULL;
      break;
      case 'file':
        $output = $file;
      break;
      case 'return':
        return $image_resized;
      break;
      default:
      break;
    }
    switch ($info[2] ) {
      case IMAGETYPE_GIF:
        imagegif($image_resized, $output);
      break;
      case IMAGETYPE_JPEG:
        imagejpeg($image_resized, $output);
      break;
      case IMAGETYPE_PNG:
        imagepng($image_resized, $output);
      break;
      default:
        return false;
    }
    return true;
}

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
在PHP里得到前天和昨天的日期的代码
Aug 16 PHP
php面向对象全攻略 (六)__set() __get() __isset() __unset()的用法
Sep 30 PHP
PHP 多维数组的排序问题 根据二维数组中某个项排序
Nov 09 PHP
php四种基础算法代码实例
Oct 29 PHP
微信公众平台天气预报功能开发
Jul 06 PHP
thinkphp数据查询和遍历数组实例
Nov 28 PHP
php递归删除指定文件夹的方法小结
Apr 20 PHP
PHP文件与目录操作示例
Dec 24 PHP
在php7中MongoDB实现模糊查询的方法详解
May 03 PHP
PHP基于正则批量替换Img中src内容实现获取缩略图的功能示例
Jun 07 PHP
PHP实现动态创建XML文档的方法
Mar 30 PHP
Laravel 5.5 异常处理 &amp; 错误日志的解决
Oct 17 PHP
php实现网页缓存的工具类分享
Jul 14 #PHP
浅谈php错误提示及查错方法
Jul 14 #PHP
浅谈php的优缺点
Jul 14 #PHP
使用URL传输SESSION信息
Jul 14 #PHP
利用“多说”制作留言板、评论系统
Jul 14 #PHP
php生成数字字母的验证码图片
Jul 14 #PHP
php算法实例分享
Jul 14 #PHP
You might like
Protoss魔法科技
2020/03/14 星际争霸
文件上传的实现
2006/10/09 PHP
php中json_encode中文编码问题分析
2011/09/13 PHP
多个PHP中文字符串截取函数
2013/11/12 PHP
PHP中的闭包(匿名函数)浅析
2015/02/07 PHP
php专用数组排序类ArraySortUtil用法实例
2015/04/03 PHP
jQuery对象和DOM对象使用说明
2010/06/25 Javascript
Google AJAX 搜索 API实现代码
2010/11/17 Javascript
js Event对象的5种坐标
2011/09/12 Javascript
探讨jQuery的ajax使用场景(c#)
2013/12/03 Javascript
微信小程序利用swiper+css实现购物车商品删除功能
2019/03/06 Javascript
Vue生命周期activated之返回上一页不重新请求数据操作
2020/07/26 Javascript
Element Collapse 折叠面板的使用方法
2020/07/26 Javascript
[01:19:54]DOTA2上海特级锦标赛主赛事日 - 2 败者组第二轮#1Alliance VS EHOME
2016/03/03 DOTA
Python 列表list使用介绍
2014/11/30 Python
python关键字and和or用法实例
2015/05/28 Python
快速入手Python字符编码
2016/08/03 Python
Python基于贪心算法解决背包问题示例
2017/11/27 Python
Python MySQLdb 使用utf-8 编码插入中文数据问题
2018/03/13 Python
用 Python 连接 MySQL 的几种方式详解
2018/04/04 Python
对Python 两大环境管理神器 pyenv 和 virtualenv详解
2018/12/31 Python
python3 打印输出字典中特定的某个key的方法示例
2019/07/06 Python
python 执行终端/控制台命令的例子
2019/07/12 Python
Python环境下安装PyGame和PyOpenGL的方法
2020/03/25 Python
python多进程使用函数封装实例
2020/05/02 Python
详解pandas映射与数据转换
2021/01/22 Python
css3 box-shadow阴影(外阴影与外发光)图示讲解
2017/08/11 HTML / CSS
html5使用canvas实现图片下载功能的示例代码
2017/08/26 HTML / CSS
浅析数据存储的三种方式 cookie sessionstorage localstorage 的异同
2020/06/04 HTML / CSS
澳大利亚电商Catch新西兰站:Catch.co.nz
2020/05/30 全球购物
我的中国梦演讲稿800字
2014/08/19 职场文书
群众路线领导班子整改方案
2014/10/25 职场文书
2014年质检员工作总结
2014/11/18 职场文书
Pytorch中Softmax和LogSoftmax的使用详解
2021/06/05 Python
Python Pandas常用函数方法总结
2021/06/15 Python
win10系统xps文件怎么打开?win10打开xps文件的两种操作方法
2022/07/23 数码科技