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 相关文章推荐
用Apache反向代理设置对外的WWW和文件服务器
Oct 09 PHP
一道求$b相对于$a的相对路径的php代码
Aug 08 PHP
php入门学习知识点六 PHP文件的读写操作代码
Jul 14 PHP
php中$美元符号与Zen Coding冲突问题解决方法分享
May 28 PHP
CodeIgniter框架URL路由总结
Sep 03 PHP
CI框架装载器Loader.php源码分析
Nov 04 PHP
php实现的一个简单json rpc框架实例
Mar 30 PHP
WordPress中缩略图的使用以及相关技巧
Nov 24 PHP
PHP多维数组转一维数组的简单实现方法
Dec 23 PHP
CodeIgniter视图使用注意事项
Jan 20 PHP
YII中Ueditor富文本编辑器文件和图片上传的配置图文教程
Mar 15 PHP
PHP设计模式(九)外观模式Facade实例详解【结构型】
May 02 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中的字符串函数
2006/10/09 PHP
Zend 输出产生XML解析错误
2009/03/03 PHP
PHP动态生成javascript文件的2个例子
2014/04/11 PHP
最常用的8款PHP调试工具
2014/07/06 PHP
PHP中的traits实现代码复用使用实例
2015/05/13 PHP
PHP中set error handler函数用法小结
2015/11/11 PHP
ecshop添加菜单及权限分配问题
2017/11/21 PHP
node.js中的events.emitter.once方法使用说明
2014/12/10 Javascript
jQuery中hide()方法用法实例
2014/12/24 Javascript
原生javascript实现图片按钮切换
2015/01/12 Javascript
JavaScript实现更改网页背景与字体颜色的方法
2015/02/02 Javascript
利用jQuery中的ajax分页实现代码
2016/02/25 Javascript
扩展bootstrap的modal模态框-动态添加modal框-弹出多个modal框
2017/02/21 Javascript
Angular2生命周期钩子函数的详细介绍
2017/07/10 Javascript
Vue实现PopupWindow组件详解
2018/04/28 Javascript
JS+HTML5实现获取手机验证码倒计时按钮
2018/08/08 Javascript
vue 属性拦截实现双向绑定的实例代码
2018/10/24 Javascript
详解vue组件中使用路由方法
2019/02/12 Javascript
微信提示 在浏览器打开 效果实现过程解析
2019/09/10 Javascript
Vue-cli项目部署到Nginx服务器的方法
2019/11/01 Javascript
vue 避免变量赋值后双向绑定的操作
2020/11/07 Javascript
使用Python设置tmpfs来加速项目的教程
2015/04/17 Python
Python中编写ORM框架的入门指引
2015/04/29 Python
Python os模块学习笔记
2015/06/21 Python
python django中8000端口被占用的解决
2019/12/17 Python
Jupyter notebook 启动闪退问题的解决
2020/04/13 Python
分布式全文检索引擎ElasticSearch原理及使用实例
2020/11/14 Python
简单html5代码获取地理位置
2014/03/31 HTML / CSS
美国祛痘、抗衰老药妆品牌:Murad
2016/08/27 全球购物
全球速卖通俄罗斯站:AliExpress俄罗斯
2019/06/17 全球购物
安全教育心得体会
2013/12/29 职场文书
医院检讨书范文
2014/02/01 职场文书
幼儿园园务工作总结2015
2015/05/18 职场文书
法定代表人资格证明书
2015/06/18 职场文书
Logback 使用TurboFilter实现日志级别等内容的动态修改操作
2021/08/30 Java/Android
Java获取字符串编码格式实现思路
2022/09/23 Java/Android