日期:2014-05-16  浏览次数:20453 次

为什么上传图片中含有透明通道,图片就会变成黑色?
p.s. 改的图片上传类原文地址:http://blog.csdn.net/a957651480/article/details/23124257

<?php

class Images{

var $inputName;                 //控件名
var $allowType = array(
'image/gif','image/jpg','image/jpeg','image/png','image/x-png','image/pjpeg'
);                            //上传类型
var $allowSize = 1048576; //限制大小
var $saveDir = "";      //保存目录
var $FileName = ""; //文件名
var $isRename = false;               //是否重命名,默认为true
var $errID = 0;                     //错误代码,默认为0
var $errMsg = "";                   //错误信息
var $savePath = "";                 //保存路径
var $ImgSize = 0; //图片尺寸

function __construct($inputName,$allowType="",$allowSize="",$saveDir="",$isRename=true){
if(empty($inputName)){
$this->chk_err(-1);       //无传入控件名
}else{
$this->inputName = $inputName;
}

if(!empty($allowType)) $this->allowType = $allowType;
if(!empty($allowSize)) $this->allowSize = $allowSize;
if(!empty($saveDir)) $this->saveDir = $saveDir;
if(!empty($isRename)) $this->isRename = $isRename;
}

function is_uploaded(){
if(empty($_FILES[$this->inputName]['name'])){
$this->chk_err(4);    //没有文件被上传
}else{
if(is_uploaded_file($_FILES[$this->inputName]['tmp_name'])){
return true;
}else{
$this->chk_err(-2);       //文件上传不合法
}
}
}

function chk_type(){
if(!in_array($_FILES[$this->inputName]['type'],$this->allowType)){
$this->chk_err(-3);         //上传的文件类型不被允许
}else{
return true;
}
}

function chk_size(){
if($_FILES[$this->inputName]['size'] > $this->allowSize){
$this->chk_err(-4);          //上传的文件过大
}else{
return true;
}
}

function move_uploaded(){        //移动上传文件
if(!$this->is_uploaded()){
return false;
}

if(!$this->chk_size()){
return false;
}

if(!$this->chk_type()){
return false;
}

//重命名
if($this->isRename){
$arrTmp = pathinfo($_FILES[$this->inputName]['name']);
$extension = strtolower($arrTmp['extension']);
$file_newname = $this->FileName; //重命名新文件
}else{
$file_newname = $_FILES[$this->inputName]['name'];
}

if(!file_exists($this->saveDir)){       //判断保存目录是否存在
mkdir($this->saveDir,0777,true);    //