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的ajax框架xajax入门与试用介绍
Dec 19 PHP
PHP常用开发函数解析之数组篇[未完结]
Jul 30 PHP
thinkphp3.0 模板中函数的使用
Nov 13 PHP
基于magic_quotes_gpc与magic_quotes_runtime的区别与使用介绍
Apr 22 PHP
php 批量替换html标签的实例代码
Nov 26 PHP
php获取域名的google收录示例
Mar 24 PHP
浅析PHP编程中10个最常见的错误
Aug 08 PHP
PHP中is_file()函数使用指南
May 08 PHP
PHP 微信扫码支付源代码(推荐)
Nov 03 PHP
php实现不通过扩展名准确判断文件类型的方法【finfo_file方法与二进制流】
Apr 18 PHP
php实现socket推送技术的示例
Dec 20 PHP
Laravel 简单实现Ajax滚动加载示例
Oct 22 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中http_build_query 的一个问题
2012/03/25 PHP
Linux下CoreSeek及PHP扩展模块的安装
2012/09/23 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十二)
2014/06/25 PHP
php如何获取文件的扩展名
2015/10/28 PHP
学习php设计模式 php实现合成模式(composite)
2015/12/08 PHP
解决laravel5.4下的group by报错的问题
2019/10/16 PHP
PHP 数组操作详解【遍历、指针、函数等】
2020/05/13 PHP
用javascript父窗口控制只弹出一个子窗口
2007/04/10 Javascript
3Z版基于jquery的图片复选框(asp.net+jquery)
2010/04/12 Javascript
jQuery操作 input type=checkbox的实现代码
2012/06/14 Javascript
js+数组实现网页上显示时间/星期几的实用方法
2013/01/18 Javascript
JavaScript根据数据生成百分比图和柱状图的实例代码
2013/07/14 Javascript
javascript 判断整数方法分享
2014/12/16 Javascript
JavaScript实现彩虹文字效果的方法
2015/04/16 Javascript
详解vue-cli中配置sass
2017/06/21 Javascript
使用angular帮你实现拖拽的示例
2017/07/05 Javascript
form表单数据封装成json格式并提交给服务器的实现方法
2017/12/14 Javascript
使用Vuex实现一个笔记应用的方法
2018/03/13 Javascript
ios设备中angularjs无法改变页面title的解决方法
2018/09/13 Javascript
React 组件渲染和更新的实现代码示例
2019/02/21 Javascript
JS学习笔记之贪吃蛇小游戏demo实例详解
2019/05/29 Javascript
Python网络编程中urllib2模块的用法总结
2016/07/12 Python
Python实现识别图片内容的方法分析
2018/07/11 Python
python+flask实现API的方法
2018/11/21 Python
浅谈pycharm的xmx和xms设置方法
2018/12/03 Python
css3实现信纸/同学录效果的示例代码
2018/12/11 HTML / CSS
浅谈基于Canvas的手绘风格图形库Rough.js
2018/03/19 HTML / CSS
世界上最大的家庭自动化公司:Smarthome
2017/12/20 全球购物
Pat McGrath Labs官网:世界上最有影响力的化妆师推出的彩妆品牌
2018/01/07 全球购物
大学专科生推荐信范文
2013/11/23 职场文书
机关门卫岗位职责
2013/12/30 职场文书
个人工作作风整改措施思想汇报
2014/10/13 职场文书
北京英语导游词
2015/02/12 职场文书
2016大学生毕业实习心得体会
2016/01/23 职场文书
幼儿园2016年感恩节活动总结
2016/04/01 职场文书
阿里云服务器搭建Php+Apache运行环境的详细过程
2021/05/15 PHP