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序列号生成函数和字符串替换函数代码
Jun 07 PHP
PHP在线生成二维码(google api)的实现代码详解
Jun 04 PHP
php自动加载机制的深入分析
Jun 08 PHP
php使用date和strtotime函数输出指定日期的方法
Nov 14 PHP
php实现的一个简单json rpc框架实例
Mar 30 PHP
php实现求相对时间函数
Jun 15 PHP
如何使用php脚本给html中引用的js和css路径打上版本号
Nov 18 PHP
PHP接收json 并将接收数据插入数据库的实现代码
Dec 01 PHP
php mysql实现mysql_select_db选择数据库
Dec 30 PHP
php实现文件上传及头像预览功能
Jan 15 PHP
form表单传递数组数据、php脚本接收的实例
Feb 09 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
关于二级目录拖拽排序的实现(源码示例下载)
2013/04/26 PHP
学习php分页代码实例
2013/10/24 PHP
php中session使用示例
2014/03/29 PHP
PHP发送邮件确认验证注册功能示例【修改别人邮件类】
2019/11/09 PHP
解决extjs在firefox中关闭窗口再打开后iframe中js函数访问不到的问题
2008/11/06 Javascript
5个javascript的数字格式化函数分享
2011/12/07 Javascript
web的各种前端打印方法之jquery打印插件jqprint实现网页打印
2013/01/09 Javascript
jQuery中实现动画效果的基本操作介绍
2013/04/16 Javascript
深入理解javascript中的立即执行函数(function(){…})()
2014/06/12 Javascript
基于Jquery和CSS3制作数字时钟附源码下载(CSS3篇)
2015/11/24 Javascript
javascript学习小结之prototype
2015/12/03 Javascript
node.js中 stream使用教程
2016/08/28 Javascript
JS实现拖拽的方法分析
2016/12/20 Javascript
js控制文本框禁止输入特殊字符详解
2017/04/07 Javascript
微信小程序 下拉菜单简单实例
2017/04/13 Javascript
Vuejs入门教程之Vue生命周期,数据,手动挂载,指令,过滤器
2017/04/19 Javascript
react 国际化的实现代码示例
2018/09/14 Javascript
js JSON.stringify()基础详解
2019/06/19 Javascript
[01:11:10]2014 DOTA2华西杯精英邀请赛 5 24 iG VS VG加赛
2014/05/26 DOTA
[00:13]天涯墨客二技能展示
2018/08/25 DOTA
用python代码做configure文件
2014/07/20 Python
python快速查找算法应用实例
2014/09/26 Python
Python获取网页上图片下载地址的方法
2015/03/11 Python
python爬虫爬取快手视频多线程下载功能
2018/02/28 Python
Python requests发送post请求的一些疑点
2018/05/20 Python
python的sorted用法详解
2019/06/25 Python
PyCharm 专业版安装图文教程
2020/02/20 Python
世界上最受欢迎的钓鱼诱饵:Rapala
2019/05/02 全球购物
往来会计岗位职责
2013/12/19 职场文书
党员实事承诺书
2014/03/26 职场文书
家长建议怎么写
2014/05/15 职场文书
投标服务承诺书
2014/05/28 职场文书
体育教师个人总结
2015/02/09 职场文书
2015年预防青少年违法犯罪工作总结
2015/05/22 职场文书
2015年保险公司内勤工作总结
2015/05/23 职场文书
Linux中各个目录的作用与内容
2022/06/28 Servers