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制作静态网站的模板框架(一)
Oct 09 PHP
PHP mkdir()定义和用法
Jan 14 PHP
php生成静态文件的多种方法分享
Jul 17 PHP
PHP获取浏览器信息类和客户端地理位置的2个方法
Apr 24 PHP
PHP中error_log()函数的使用方法
Jan 20 PHP
php根据指定位置和长度获得子字符串的方法
Mar 17 PHP
php导出中文内容excel文件类实例
Jul 06 PHP
10款PHP开源商城系统汇总介绍
Jul 23 PHP
PHP+Ajax无刷新带进度条图片上传示例
Feb 08 PHP
详解PHP中的外观模式facade pattern
Feb 05 PHP
php微信公众号开发之关键词回复
Oct 20 PHP
Yii框架视图、视图布局、视图数据块操作示例
Oct 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
PHP中路径问题的解决方案
2006/10/09 PHP
PHP+ajax 无刷新删除数据
2010/02/20 PHP
PHPCMS忘记后台密码的解决办法
2016/10/30 PHP
浅谈laravel-admin的sortable和orderby使用问题
2019/10/03 PHP
JS动画效果代码3
2008/04/03 Javascript
js最简单的拖拽效果实现代码
2010/09/24 Javascript
解析javascript 实用函数的使用详解
2013/05/10 Javascript
JavaScript实现自动对页面上敏感词进行屏蔽的方法
2015/07/27 Javascript
JavaScript实现动态删除列表框值的方法
2015/08/12 Javascript
jQuery深拷贝Json对象简单示例
2016/07/06 Javascript
Bootstrap基本插件学习笔记之Alert警告框(20)
2016/12/08 Javascript
Angular实现的table表格排序功能完整示例
2017/12/22 Javascript
微信小程序实现星级评分和展示
2018/07/05 Javascript
浅谈Vue.js组件(二)
2019/04/09 Javascript
Sublime Text3 配置 NodeJs 环境的方法
2020/05/20 NodeJs
一分钟学会JavaScript中的try-catch
2020/12/14 Javascript
python函数参数*args**kwargs用法实例
2013/12/04 Python
python和shell实现的校验IP地址合法性脚本分享
2014/10/23 Python
Python使用reportlab将目录下所有的文本文件打印成pdf的方法
2015/05/20 Python
Django中处理出错页面的方法
2015/07/15 Python
python中的随机函数小结
2018/01/27 Python
用Python调用win命令行提高工作效率的实例
2019/08/14 Python
python 已知平行四边形三个点,求第四个点的案例
2020/04/12 Python
Python Django搭建网站流程图解
2020/06/13 Python
使用HTML5的Notification API制作web通知的教程
2015/05/08 HTML / CSS
Whittard官方海外旗舰店:英国百年茶叶品牌
2018/02/22 全球购物
乌克兰鞋类购物网站:Eobuv.com.ua
2020/11/28 全球购物
PHP面试题-$message和$$message的区别
2015/12/08 面试题
会计电算化毕业生自荐信
2014/03/03 职场文书
追悼会主持词
2014/03/20 职场文书
《夕阳真美》教学反思
2014/04/27 职场文书
党的群众路线教育实践活动领导班子整改措施
2014/10/28 职场文书
节水倡议书
2015/01/19 职场文书
2015初中团支部工作总结
2015/07/21 职场文书
Python 多线程之threading 模块的使用
2021/04/14 Python
Python利用FlashText算法实现替换字符串
2022/03/31 Python