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 相关文章推荐
mysql 字段类型说明
Apr 27 PHP
ionCube 一款类似zend的PHP加密/解密工具
Jul 25 PHP
php统计文件大小,以GB、MB、KB、B输出
May 29 PHP
第4章 数据处理-php字符串的处理-郑阿奇(续)
Jul 04 PHP
php 保留字列表
Oct 04 PHP
探讨如何使用SimpleXML函数来加载和解析XML文档
Jun 07 PHP
使用PHP获取当前url路径的函数以及服务器变量
Jun 29 PHP
php获取目标函数执行时间示例
Mar 04 PHP
图文介绍PHP添加Redis模块及连接
Jul 28 PHP
PHP基本语法实例总结
Sep 09 PHP
PHP使用file_get_contents发送http请求功能简单示例
Apr 29 PHP
PHP合并两个或多个数组的方法
Jan 20 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
Ajax+PHP边学边练 之五 图片处理
2009/12/03 PHP
基于PHP文件操作的详解
2013/06/05 PHP
解析php中session的实现原理以及大网站应用应注意的问题
2013/06/17 PHP
PHP、Python和Javascript的装饰器模式对比
2015/02/03 PHP
PHP 反射(Reflection)使用实例
2015/05/12 PHP
PHP中把有符号整型转换为无符号整型方法
2015/05/27 PHP
php获取系统变量方法小结
2015/05/29 PHP
PHP设计模式之装饰器模式实例详解
2018/02/07 PHP
JS在IE和FF下attachEvent,addEventListener学习笔记
2009/11/26 Javascript
优化javascript的执行速度
2010/01/23 Javascript
Javascript面象对象成员、共享成员变量实验
2010/11/19 Javascript
javascript类型转换使用方法
2014/02/08 Javascript
VUEJS实战之构建基础并渲染出列表(1)
2016/06/13 Javascript
浅析jQuery 3.0中的Data
2016/06/14 Javascript
极力推荐10个短小实用的JavaScript代码段
2016/08/03 Javascript
js制作网站首页图片轮播特效代码
2016/08/30 Javascript
ionic由于使用了header和subheader导致被遮挡的问题的两种解决方法
2016/09/22 Javascript
原生JS实现图片轮播效果
2016/12/26 Javascript
vue-resource 拦截器使用详解
2017/02/21 Javascript
日期时间范围选择插件:daterangepicker使用总结(必看篇)
2017/09/14 Javascript
浅析Vue.js 中的条件渲染指令
2018/11/19 Javascript
React+Antd+Redux实现待办事件的方法
2019/03/14 Javascript
vue实现随机验证码功能的实例代码
2019/04/30 Javascript
Vue请求java服务端并返回数据代码实例
2019/11/28 Javascript
基于vue和websocket的多人在线聊天室
2020/02/01 Javascript
Vue获取页面元素的相对位置的方法示例
2020/02/05 Javascript
javascript实现打砖块小游戏(附完整源码)
2020/09/18 Javascript
Python对HTML转义字符进行反转义的实现方法
2019/04/28 Python
python实现两个经纬度点之间的距离和方位角的方法
2019/07/05 Python
python画微信表情符的实例代码
2019/10/09 Python
CSS3教程(10):CSS3 HSL声明设置颜色
2009/04/02 HTML / CSS
html5 svg 中元素点击事件添加方法
2013/01/16 HTML / CSS
以特惠价提供在线奢侈品购物:FRMODA.com
2018/01/25 全球购物
婴儿鞋,独特的婴儿服装和配件:Zutano
2018/11/03 全球购物
铭宣海淘转运:美国、日本、英国转运等全球转运公司
2019/09/10 全球购物
大学生暑期社会实践证明范本
2014/10/24 职场文书