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访问查询mysql数据的三种方法
Oct 09 PHP
PHP得到某段时间区间的时间戳 php定时任务
Apr 12 PHP
php中选择什么接口(mysql、mysqli)访问mysql
Feb 06 PHP
浅析php插件 HTMLPurifier HTML解析器
Jul 01 PHP
PHP中func_get_args(),func_get_arg(),func_num_args()的区别
Sep 30 PHP
php网站判断用户是否是手机访问的方法
Nov 01 PHP
php打印一个边长为N的实心和空心菱型的方法
Mar 02 PHP
php代码架构的八点注意事项
Jan 25 PHP
PHP闭包函数传参及使用外部变量的方法
Mar 15 PHP
PHP封装curl的调用接口及常用函数详解
May 31 PHP
Thinkphp页面跳转设置跳转等待时间的操作
Oct 16 PHP
在TP5数据库中四个字段实现无限分类的示例
Oct 18 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
eAccelerator的安装与使用详解
2013/06/13 PHP
php以post形式发送xml的方法
2014/11/04 PHP
PHP直接修改表内容DataGrid功能实现代码
2015/09/24 PHP
javascript 事件绑定问题
2011/01/01 Javascript
jquery拖动插件(jquery.drag)使用介绍
2013/06/18 Javascript
详解jquery uploadify 上传文件
2013/11/09 Javascript
实例分析javascript中的call()和apply()方法
2014/11/28 Javascript
node.js中使用socket.io的方法
2014/12/15 Javascript
JS实现网页上随机产生超链接地址的方法
2015/11/09 Javascript
浅析AMD CMD CommonJS规范--javascript模块化加载学习心得总结
2016/03/16 Javascript
浅谈JavaScript 浏览器对象
2016/06/03 Javascript
创建简单的node服务器实例(分享)
2017/06/23 Javascript
jQuery之动画ajax事件(实例讲解)
2017/07/18 jQuery
React-Native中禁用Navigator手势返回的示例代码
2017/09/09 Javascript
Three.js实现绘制字体模型示例代码
2017/09/26 Javascript
Vue2.0设置全局样式(less/sass和css)
2017/11/18 Javascript
webpack vue 项目打包生成的文件,资源文件报404问题的修复方法(总结篇)
2018/01/09 Javascript
react native 文字轮播的实现示例
2018/07/27 Javascript
微信小程序用户位置权限的获取方法(拒绝后提醒)
2018/11/15 Javascript
用Vue.js在浏览器中实现裁剪图像功能
2019/06/18 Javascript
Js跳出两级循环方法代码实例
2020/09/22 Javascript
Python实现Smtplib发送带有各种附件的邮件实例
2017/06/05 Python
Django实现的自定义访问日志模块示例
2017/06/23 Python
Anaconda入门使用总结
2018/04/05 Python
Python中实现单例模式的n种方式和原理
2018/11/14 Python
Python datetime和unix时间戳之间相互转换的讲解
2019/04/01 Python
python中PS 图像调整算法原理之亮度调整
2019/06/28 Python
Python使用Turtle库绘制一棵西兰花
2019/11/23 Python
HTML5 canvas基本绘图之绘制线段
2016/06/27 HTML / CSS
太阳镜仓库,售价20美元或更少:Sunglass Warehouse
2016/09/28 全球购物
SQL中where和having的区别
2012/06/17 面试题
影视广告专业求职信
2014/09/02 职场文书
2014领导班子四风问题对照检查材料思想汇报
2014/09/21 职场文书
小学教师党员承诺书
2015/04/27 职场文书
幼儿园毕业致辞
2015/07/29 职场文书
JavaScript parseInt0.0000005打印5原理解析
2022/07/23 Javascript