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 相关文章推荐
php5.2.0内存管理改进
Jan 22 PHP
php 获取可变函数参数的函数
Aug 26 PHP
PHP中的array数组类型分析说明
Jul 27 PHP
php visitFile()遍历指定文件夹函数
Aug 21 PHP
解析php mysql 事务处理回滚操作(附实例)
Aug 05 PHP
php实现水仙花数的4个示例分享
Apr 08 PHP
用 Composer构建自己的 PHP 框架之构建路由
Oct 30 PHP
php开发时容易忘记的一些技术细节
Feb 03 PHP
thinkPHP简单遍历数组方法分析
May 16 PHP
PHP对象克隆clone用法示例
Sep 28 PHP
PHP面向对象程序设计中的self、static、parent关键字用法分析
Aug 14 PHP
php中yar框架实例用法讲解
Dec 27 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
tp5(thinkPHP5框架)使用DB实现批量删除功能示例
2019/05/28 PHP
在你的网页中嵌入外部网页的方法
2007/04/02 Javascript
Javascript 类型转换方法
2010/10/24 Javascript
Firefox中autocomplete=&quot;off&quot; 设置不起作用Bug的解决方法
2011/03/25 Javascript
JavaScript 函数参数是传值(byVal)还是传址(byRef) 分享
2013/07/02 Javascript
JS实现随机化快速排序的实例代码
2013/08/01 Javascript
jquery浏览器滚动加载技术实现方案
2014/06/03 Javascript
基于JQuery制作可编辑的表格特效
2014/12/23 Javascript
js通过iframe加载外部网页的实现代码
2015/04/05 Javascript
JavaScript中字符串分割函数split用法实例
2015/04/07 Javascript
使用JavaScript判断手机浏览器是横屏还是竖屏问题
2016/08/02 Javascript
jQuery实现加入收藏夹功能(主流浏览器兼职)
2016/12/24 Javascript
bootstrap组件之按钮式下拉菜单小结
2017/01/19 Javascript
浅谈angularJS的$watch失效问题的解决方案
2017/08/11 Javascript
JS交互点击WKWebView中的图片实现预览效果
2018/01/05 Javascript
浅析Angular19 自定义表单控件
2018/01/31 Javascript
javaScript产生随机数的用法小结
2018/04/21 Javascript
浅谈webpack 构建性能优化策略小结
2018/06/13 Javascript
vue.js input框之间赋值方法
2018/08/24 Javascript
Vue模板语法中数据绑定的实例代码
2019/05/17 Javascript
Python通过websocket与js客户端通信示例分析
2014/06/25 Python
用python写爬虫简单吗
2020/07/28 Python
css3隔行变换色实现示例
2014/02/19 HTML / CSS
英国网上购买门:Direct Doors
2018/06/07 全球购物
手工制作的意大利太阳镜和光学元件:Illesteva
2019/01/19 全球购物
农田水利实习自我鉴定
2013/09/19 职场文书
大学毕业生文采飞扬的自我鉴定
2013/12/03 职场文书
大学毕业生自荐书怎么写?
2014/01/06 职场文书
校长先进事迹材料
2014/02/01 职场文书
重阳节登山活动方案
2014/02/03 职场文书
环境工程专业自荐信范文
2014/06/24 职场文书
机电一体化专业求职信
2014/07/22 职场文书
暑期培训心得体会
2014/09/02 职场文书
居委会四风问题个人对照检查材料
2014/09/25 职场文书
2015年后备干部工作总结
2015/05/15 职场文书
SQL Server数据定义——模式与基本表操作
2021/04/05 SQL Server