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读取30天之内的根据算法排序的代码
Apr 06 PHP
PHP运行SVN命令显示某用户的文件更新记录的代码
Jan 03 PHP
ThinkPHP令牌验证实例
Jun 18 PHP
使用 PHPStorm 开发 Laravel
Mar 24 PHP
PHP对称加密函数实现数据的加密解密
Oct 27 PHP
浅析PHP数据导出知识点
Feb 17 PHP
PHP+Ajax实现的博客文章添加类别功能示例
Mar 29 PHP
Laravel框架分页实现方法分析
Jun 12 PHP
phpinfo无法显示的原因及解决办法
Feb 15 PHP
php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率完整示例
May 09 PHP
Laravel Eloquent ORM 多条件查询的例子
Oct 10 PHP
Laravel框架源码解析之反射的使用详解
May 14 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
VOLVO车载收音机
2021/03/02 无线电
自定义php类(查找/修改)xml文档
2013/03/26 PHP
解密ThinkPHP3.1.2版本之模块和操作映射
2014/06/19 PHP
jQuery checkbox全选/取消全选实现代码
2009/11/14 Javascript
jQuery实现id模糊查询的小例子
2013/03/19 Javascript
如何使用jquery控制CSS样式,并且取消Css样式(如背景色,有实例)
2013/07/09 Javascript
可兼容IE的获取及设置cookie的jquery.cookie函数方法
2013/09/02 Javascript
js如何判断不同系统的浏览器类型
2013/10/28 Javascript
使用nodejs、Python写的一个简易HTTP静态文件服务器
2014/07/18 NodeJs
jQuery实现tab标签自动切换的方法
2015/02/28 Javascript
javascript实现随机生成DIV背景色
2016/06/20 Javascript
浅谈jQuery操作类数组的工具方法
2016/12/23 Javascript
tablesorter.js表格排序使用方法(支持中文排序)
2017/02/10 Javascript
BootStrap Datepicker 插件修改为默认中文的实现方法
2017/02/10 Javascript
vue.js中mint-ui框架的使用方法
2017/05/12 Javascript
如何在vue里添加好看的lottie动画
2018/08/02 Javascript
使用ECharts实现状态区间图
2018/10/25 Javascript
JavaScript简单实现动态改变HTML内容的方法示例
2018/12/25 Javascript
JavaScript函数式编程(Functional Programming)箭头函数(Arrow functions)用法分析
2019/05/22 Javascript
js实现点击生成随机div
2020/01/16 Javascript
JS Html转义和反转义(html编码和解码)的实现与使用方法总结
2020/03/10 Javascript
Python转码问题的解决方法
2008/10/07 Python
全面解读Python Web开发框架Django
2014/06/30 Python
python爬虫框架scrapy实现模拟登录操作示例
2018/08/02 Python
python实现简单日期工具类
2019/04/24 Python
Python爬虫实现验证码登录代码实例
2019/05/10 Python
Python音频操作工具PyAudio上手教程详解
2019/06/26 Python
python定间隔取点(np.linspace)的实现
2019/11/27 Python
pytorch使用tensorboardX进行loss可视化实例
2020/02/24 Python
Python3 shelve对象持久存储原理详解
2020/03/23 Python
你需要学会的8个Python列表技巧
2020/06/24 Python
师范大学毕业自我鉴定
2013/11/21 职场文书
工程项目经理岗位职责
2013/12/15 职场文书
北体毕业生求职信
2014/02/28 职场文书
决心书格式范文
2015/09/23 职场文书
mysql通过group by分组取最大时间对应数据的两种有效方法
2022/09/23 MySQL