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 tp验证表单与自动填充函数代码
Feb 22 PHP
基于php缓存的详解
May 15 PHP
php实现图片转换成ASCII码的方法
Apr 03 PHP
PHP中使用register_shutdown_function函数截获fatal error示例
Apr 21 PHP
php基本函数汇总
Jul 09 PHP
基于PHP技术开发客服工单系统
Jan 06 PHP
PHP下使用mysqli的函数连接mysql出现warning: mysqli::real_connect(): (hy000/1040): ...
Feb 14 PHP
PHP封装函数实现生成随机的字符串验证码
Jan 24 PHP
PHP实现的mysql操作类【MySQL与MySQLi方式】
Oct 07 PHP
PHP类与对象后期静态绑定操作实例详解
Dec 20 PHP
PHP下用Swoole实现Actor并发模型的方法
Jun 12 PHP
Laravel框架中缓存的使用方法分析
Sep 06 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操作数组的一些函数整理介绍
2011/07/17 PHP
php实现构建排除当前元素的乘积数组方法
2018/10/06 PHP
event.srcElement 用法笔记e.target
2009/12/18 Javascript
JS焦点图切换,上下翻转
2011/05/12 Javascript
jQuery调用WebService的实现代码
2011/06/19 Javascript
jquery获取当前日期的方法
2015/01/14 Javascript
JavaScript 学习笔记之数据类型
2015/01/14 Javascript
jQuery学习笔记之jQuery中的$
2015/01/19 Javascript
JavaScript检测浏览器cookie是否已经启动的方法
2015/02/27 Javascript
关于JS中prototype的理解
2015/09/07 Javascript
Javascript的表单验证-揭开正则表达式的面纱
2016/03/18 Javascript
AngularJS使用ng-repeat和ng-if实现数据的删选显示效果示例【适用于表单数据的显示】
2016/12/13 Javascript
微信小程序商品详情页规格属性选择示例代码
2017/10/30 Javascript
vue监听键盘事件的快捷方法【推荐】
2018/07/11 Javascript
详解JavaScript中的坐标和距离
2019/05/27 Javascript
JavaScript实现轮播图片完整代码
2020/03/07 Javascript
[01:34]2016国际邀请赛中国区预选赛IG战队教练采访
2016/06/27 DOTA
[59:00]DOTA2-DPC中国联赛 正赛 Ehome vs PSG.LGD BO3 第一场 3月7日
2021/03/11 DOTA
详解python函数传参是传值还是传引用
2018/01/16 Python
IntelliJ IDEA安装运行python插件方法
2018/12/10 Python
解决Pycharm后台indexing导致不能run的问题
2019/06/27 Python
python 使用matplotlib 实现从文件中读取x,y坐标的可视化方法
2019/07/04 Python
Python 保持登录状态进行接口测试的方法示例
2019/08/06 Python
解决Django连接db遇到的问题
2019/08/29 Python
Python Django view 两种return的实现方式
2020/03/16 Python
Python lxml库的简单介绍及基本使用讲解
2020/12/22 Python
美国现代家具网站:Design Within Reach
2018/07/19 全球购物
数据库设计的包括哪两种,请分别进行说明
2016/07/15 面试题
机电专业大学生求职信
2013/10/04 职场文书
测绘工程个人的自我评价
2013/11/10 职场文书
大学本科毕业生的自我鉴定
2013/11/26 职场文书
高中打架检讨书
2014/02/13 职场文书
开业典礼主持词
2014/03/21 职场文书
国家助学金感谢信
2015/01/21 职场文书
质量承诺书格式范文
2015/04/28 职场文书
六年级上册《闻官军收河南河北》的教学设计
2019/11/15 职场文书