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中文乱码
Nov 26 PHP
php 空格,换行,跳格使用说明
Dec 18 PHP
php实现的一个很好用HTML解析器类可用于采集数据
Sep 23 PHP
PHP中source #N问题的解决方法
Jan 27 PHP
CMS中PHP判断系统是否已经安装的方法示例
Jul 26 PHP
PHP与MYSQL中UTF8 中文排序示例代码
Oct 23 PHP
thinkphp区间查询、统计查询与SQL直接查询实例分析
Nov 24 PHP
php+xml编程之SimpleXML的应用实例
Jan 24 PHP
Linux(CentOS)下PHP扩展PDO编译安装的方法
Apr 07 PHP
利用Laravel事件系统如何实现登录日志的记录详解
May 20 PHP
PHP PDOStatement::closeCursor讲解
Jan 30 PHP
实例讲解PHP表单验证功能
Feb 15 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
PHP 5.3新特性命名空间规则解析及高级功能
2010/03/11 PHP
在smarty模板中使用PHP函数的方法
2011/04/23 PHP
基于Zend的Config机制的应用分析
2013/05/02 PHP
PHP定义字符串的四种方式详解
2018/02/06 PHP
js类中的公有变量和私有变量
2008/07/24 Javascript
from 表单提交返回值用post或者是get方法实现
2013/08/21 Javascript
JavaScript实现基于十进制的四舍五入实例
2015/07/17 Javascript
jQuery树形下拉菜单特效代码分享
2015/08/15 Javascript
浅谈JavaScript超时调用和间歇调用
2015/08/30 Javascript
jQuery随手笔记之常用的jQuery操作DOM事件
2015/11/29 Javascript
你有必要知道的25个JavaScript面试题
2015/12/29 Javascript
js显示动态时间的方法详解
2016/08/20 Javascript
常用原生js自定义函数总结
2016/11/20 Javascript
js控制台输出的方法(详解)
2016/11/26 Javascript
正则中的回溯定义与用法分析【JS与java实现】
2016/12/27 Javascript
Bootstrap modal 多弹窗之叠加关闭阴影遮罩问题的解决方法
2017/02/27 Javascript
JSONP跨域请求
2017/03/02 Javascript
vue学习笔记之vue1.0和vue2.0的区别介绍
2017/05/17 Javascript
微信小程序template模板实例详解
2017/10/27 Javascript
vue.js 获取select中的value实例
2018/03/01 Javascript
Vue中实现权限控制的方法示例
2019/06/07 Javascript
vue 使用async写数字动态加载效果案例
2020/07/18 Javascript
[37:21]完美世界DOTA2联赛PWL S2 Inki vs Magma 第二场 11.22
2020/11/24 DOTA
Python PyQt5实现的简易计算器功能示例
2017/08/23 Python
利用Python-iGraph如何绘制贴吧/微博的好友关系图详解
2017/11/02 Python
Python中调用其他程序的方式详解
2019/08/06 Python
Opencv图像处理:如何判断图片里某个颜色值占的比例
2020/06/03 Python
python脚本使用阿里云slb对恶意攻击进行封堵的实现
2021/02/04 Python
实现CSS3中的border-radius(边框圆角)示例代码
2013/07/19 HTML / CSS
CSS3制作炫酷的自定义发光文字
2016/03/28 HTML / CSS
html5 svg 中元素点击事件添加方法
2013/01/16 HTML / CSS
《三顾茅庐》教学反思
2014/04/10 职场文书
师恩难忘教学反思
2014/04/27 职场文书
诉前财产保全担保书
2014/05/20 职场文书
团队队名口号大全
2014/06/06 职场文书
2015重阳节敬老活动总结
2015/07/29 职场文书