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控制用户的浏览器--ob*函数的使用说明
Mar 16 PHP
php中根据某年第几天计算出日期年月日的代码
Feb 24 PHP
ThinkPHP模板Switch标签用法示例
Jun 30 PHP
CodeIgniter辅助之第三方类库third_party用法分析
Jan 20 PHP
php倒计时出现-0情况的解决方法
Jul 28 PHP
php使用Jpgraph创建折线图效果示例
Feb 15 PHP
PHP基于SMTP协议实现邮件发送实例代码
Apr 27 PHP
ThinkPHP 3.2.3实现页面静态化功能的方法详解
Aug 03 PHP
PHP数据库操作二:memcache用法分析
Aug 16 PHP
php图片合成方法(多张图片合成一张)
Nov 25 PHP
php session_decode函数用法讲解
May 26 PHP
laravel5.6框架操作数据curd写法(查询构建器)实例分析
Jan 26 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
全国FM电台频率大全 - 2 天津市
2020/03/11 无线电
Laravel模板引擎Blade中section的一些标签的区别介绍
2015/02/10 PHP
php通过function_exists检测函数是否存在的方法
2015/03/18 PHP
详解WordPress中添加和执行动作的函数使用方法
2015/12/29 PHP
PHP基于单例模式实现的mysql类
2016/01/09 PHP
PHP记录和读取JSON格式日志文件
2016/07/07 PHP
PHP带节点操作的无限分类实现方法详解
2016/11/09 PHP
在 PHP 和 Laravel 中使用 Traits的方法
2019/11/13 PHP
基于jQuery的合并表格中相同文本的相邻单元格的代码
2011/04/06 Javascript
js 判断一个元素是否在页面中存在
2012/12/27 Javascript
鼠标滚轮改变图片大小的示例代码
2013/11/20 Javascript
JS 操作Array数组的方法及属性实例解析
2014/01/08 Javascript
jquery中change()用法实例分析
2015/02/06 Javascript
JS实现兼容性好,带缓冲的动感网页右键菜单效果
2015/09/18 Javascript
通过js获取上传的图片信息(临时保存路径,名称,大小)然后通过ajax传递给后端的方法
2015/10/01 Javascript
javascript的document中的动态添加标签实现方法
2016/10/24 Javascript
AngularJS入门教程之过滤器用法示例
2016/11/02 Javascript
微信小程序 表单Form实例详解(附源码)
2016/12/22 Javascript
基于webpack 实用配置方法总结
2017/09/28 Javascript
Angular4学习教程之DOM属性绑定详解
2018/01/04 Javascript
jQuery添加新内容的四个常用方法分析【append,prepend,after,before】
2019/03/19 jQuery
JS hasOwnProperty()方法检测一个属性是否是对象的自有属性的方法
2021/01/29 Javascript
[15:56]Heroes18_暗影萨满(完美)
2014/10/31 DOTA
python实现删除文件与目录的方法
2014/11/10 Python
Python根据指定日期计算后n天,前n天是哪一天的方法
2018/05/29 Python
对python中的 os.mkdir和os.mkdirs详解
2018/10/16 Python
Python 脚本获取ES 存储容量的实例
2018/12/27 Python
使用NumPy读取MNIST数据的实现代码示例
2019/11/20 Python
python 常用日期处理-- datetime 模块的使用
2020/09/02 Python
Diamondback自行车:拥有你的冒险
2019/04/22 全球购物
如何写一个自定义标签
2012/12/28 面试题
高二物理教学反思
2014/02/08 职场文书
自行车广告词大全
2014/03/21 职场文书
节水口号标语
2014/06/19 职场文书
2015年七一建党节活动方案
2015/05/05 职场文书
2016年母亲节寄语
2015/12/04 职场文书