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 删除数组元素
Jan 16 PHP
php开发过程中关于继承的使用方法分享
Jun 17 PHP
PHP 中检查或过滤IP地址的实现代码
Nov 27 PHP
PHP处理SQL脚本文件导入到MySQL的代码实例
Mar 17 PHP
PHP基于php_imagick_st-Q8.dll实现JPG合成GIF图片的方法
Jul 11 PHP
PHP实现模仿socket请求返回页面的方法
Nov 04 PHP
PHP图片处理之使用imagecopy函数添加图片水印实例
Nov 19 PHP
php单文件版在线代码编辑器
Mar 12 PHP
php.ini中的request_order推荐设置
May 10 PHP
php短信接口代码
May 13 PHP
Windows平台实现PHP连接SQL Server2008的方法
Jul 26 PHP
PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)
Nov 19 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
攻克CakePHP系列二 表单数据显示
2008/10/22 PHP
PHP下一个非常全面获取图象信息的函数
2008/11/20 PHP
php利用gd库为图片添加水印
2016/11/09 PHP
php微信公众号开发之快递查询
2018/10/20 PHP
ASP.NET jQuery 实例2 (表单中使用回车在TextBox之间向下移动)
2012/01/13 Javascript
基于jquery tab切换(防止页面刷新)
2012/05/23 Javascript
jQuery的:parent选择器定义和用法
2014/07/01 Javascript
JS倒计时代码汇总
2014/11/25 Javascript
jQuery+PHP星级评分实现方法
2015/10/02 Javascript
Bootstrap登陆注册页面开发教程
2016/07/12 Javascript
javascript实现的左右无缝滚动效果
2016/09/19 Javascript
js与jQuery实现的用户注册协议倒计时功能实例【三种方法】
2017/11/09 jQuery
js实现复制功能(多种方法集合)
2018/01/06 Javascript
详解vue2.0+vue-video-player实现hls播放全过程
2018/03/02 Javascript
vue.js实现的经典计算器/科学计算器功能示例
2018/07/11 Javascript
vue如何自动化打包测试环境和正式环境的dist/test文件
2019/06/06 Javascript
原生js实现表格翻页和跳转
2020/09/29 Javascript
Python中函数的参数传递与可变长参数介绍
2015/06/30 Python
Python处理JSON时的值报错及编码报错的两则解决实录
2016/06/26 Python
分享python数据统计的一些小技巧
2016/07/21 Python
Python爬取qq music中的音乐url及批量下载
2017/03/23 Python
python中实现字符串翻转的方法
2018/07/11 Python
Python实现的网页截图功能【PyQt4与selenium组件】
2018/07/12 Python
python 文本单词提取和词频统计的实例
2018/12/22 Python
一篇文章彻底搞懂Python中可迭代(Iterable)、迭代器(Iterator)与生成器(Generator)的概念
2019/05/13 Python
Python实现串口通信(pyserial)过程解析
2019/09/25 Python
日本运动品牌美津浓官方购物网站:MIZUNO SHOP
2016/08/21 全球购物
Mybag美国/加拿大:英国奢华包包和名牌手袋网站
2020/02/16 全球购物
办公室内勤岗位职责范本
2013/12/09 职场文书
计算机相关的自我评价
2014/01/15 职场文书
手机促销活动方案
2014/02/05 职场文书
优秀学生事迹材料
2014/02/08 职场文书
素质教育标语
2014/06/27 职场文书
国家领导干部党的群众路线教育实践活动批评与自我批评材料
2014/09/23 职场文书
工厂门卫岗位职责
2015/04/13 职场文书
Golang 切片(Slice)实现增删改查
2022/04/22 Golang