使用php上传透明背景的png图像并保持背景透明

时间:2013-11-28 14:07:07

标签: php image-processing transparency gd imagecreatefrompng

我有一个php页面,它会获取用户图像并将其上传到我的网络服务器。

我刚刚注意到,如果我有一个透明背景的png并上传它,背景会变为白色。

例如,我在具有透明背景的photoshop中创建新图像,然后在页面中添加彩色圆圈。我通过ftp将图像保存为png和uplod到服务器,然后图像在网页上显示为圆形,因为背景是透明的。

如果我使用带文件输入框的php上传图像,图像将变为白色。

我曾尝试使用imagealphablending,imagecolorallocatealpha,imagefill和imagesavealpha来保留透明背景,但它只是将bg更改为黑色。

任何线索?

继承我的PHP代码

        <?php
    $path = "../userfiles/orig/";
    $userpath="../".$_REQUEST['userpath']."/";

    function getExtension($str) {
        $i = strrpos($str,".");
        if (!$i) { return ""; } 
        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
        return $ext;
    }

    $valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");

    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
        $name = $_FILES['photoimg']['name'];
        $size = $_FILES['photoimg']['size'];

        if(strlen($name)){
                 $ext = getExtension($name);

                if(in_array($ext,$valid_formats)){

                    if($size<(2048*2048)){
                        $time1=time();
                        $actual_image_name =$time1."_".str_replace(" ", "_", $name);
                        $tmp = $_FILES['photoimg']['tmp_name'];

                        if(move_uploaded_file($tmp, $path.$actual_image_name)){                                     
                                     $image=$actual_image_name;                     
                                     /*--------resize image-----------*/
                                     $size = 320; // the imageheight
                                     $filedir = '../userfiles/orig/'; // the directory for the original image
                                     $thumbdir = $userpath; // the directory for the resized image
                                     $prefix = $time1.'_'; // the prefix to be added to the original name
                                     $maxfile = '20000000'; 
                                     $mode = '0666';
                                     $userfile_name =str_replace(" ", "", $_FILES['photoimg']['name']);
                                     $userfile_tmp = str_replace(" ", "", $_FILES['photoimg']['tmp_name']);
                                     $userfile_size =$_FILES['photoimg']['size'];
                                     $userfile_type = $_FILES['photoimg']['type'];

                                     if (isset($_FILES['photoimg']['name'])){
                                         $prod_img = $filedir.$actual_image_name;
                                         $prod_img_thumb = $thumbdir.$prefix.$userfile_name;
                                         move_uploaded_file($userfile_tmp, $prod_img);
                                         chmod ($prod_img, octdec($mode));
                                         $sizes = getimagesize($prod_img);
                                         $aspect_ratio = $sizes[1]/$sizes[0]; 

                                         if ($sizes[1] <= $size){
                                             $new_width = $sizes[0];
                                             $new_height = $sizes[1];
                                         }else{
                                             $new_height = $size;
                                             $new_width = abs($new_height/$aspect_ratio);
                                         }
                                         $destimg=ImageCreateTrueColor($new_width,$new_height)
                                             or die('Problem In Creating image');

                                        switch($ext){
                                            case "jpg":
                                            case "jpeg":
                                            case "JPG":
                                            case "JPEG":
                                                $srcimg=ImageCreateFromJPEG($prod_img)or die('Problem In opening Source Image');
                                            break;
                                            case "PNG":
                                            case "png":
                                                $srcimg = imageCreateFromPng($prod_img)or die('Problem In opening Source Image');
                                                imagealphablending($destimg, false);
                                                $colorTransparent = imagecolorallocatealpha($destimg, 0, 0, 0x7fff0000);
                                                imagefill($destimg, 0, 0, $colorTransparent);                                                   
                                                imagesavealpha($destimg, true);

                                            break;
                                            case "BMP":
                                            case "bmp":
                                                $srcimg = imageCreateFromBmp($prod_img)or die('Problem In opening Source Image');
                                            break;
                                            case "GIF":
                                            case "gif":
                                                $srcimg = imageCreateFromGif($prod_img)or die('Problem In opening Source Image');
                                            break;
                                            default:
                                                $srcimg=ImageCreateFromJPEG($prod_img)or die('Problem In opening Source Image');
                                        }

                                        if(function_exists('imagecopyresampled')){
                                             imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,imagesX($srcimg),imagesY($srcimg))
                                             or die('Problem In resizing');
                                         }else{
                                             Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,imagesX($srcimg),imagesY($srcimg))
                                             or die('Problem In resizing');
                                         }
                                         ImageJPEG($destimg,$prod_img_thumb,90)
                                             or die('Problem In saving');

                                     }
                                unlink($prod_img);
                                echo "<img src='".$prod_img_thumb."'>";
                            }else{
                            echo "Fail upload folder with read access.";
                            }
                    }else{
                    echo "Image file size max 3 MB";
                    }

                }else{
                    echo "Invalid file format..";
                }
            }else{
            echo "Please select image..!";
            }

        exit;
    }
    ?>

1 个答案:

答案 0 :(得分:5)

<?php
$path = "../userfiles/orig/";
$userpath="../".$_REQUEST['userpath']."/";

function getExtension($str) {
  $i = strrpos($str,".");
  if (!$i) { return ""; }
  $l = strlen($str) - $i;
  $ext = substr($str,$i+1,$l);
  return $ext;
}

$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
  $name = $_FILES['photoimg']['name'];
  $size = $_FILES['photoimg']['size'];

  if(strlen($name)){
    $ext = getExtension($name);

    if(in_array($ext,$valid_formats)){

      if($size<(2048*2048)){
        $time1=time();
        $actual_image_name =$time1."_".str_replace(" ", "_", $name);
        $tmp = $_FILES['photoimg']['tmp_name'];

        if(move_uploaded_file($tmp, $path.$actual_image_name)){
          $image=$actual_image_name;
          /*--------resize image-----------*/
          $size = 320; // the imageheight
          $filedir = '../userfiles/orig/'; // the directory for the original image
          $thumbdir = $userpath; // the directory for the resized image
          $prefix = $time1.'_'; // the prefix to be added to the original name
          $maxfile = '20000000';
          $mode = '0666';
          $userfile_name =str_replace(" ", "", $_FILES['photoimg']['name']);
          $userfile_tmp = str_replace(" ", "", $_FILES['photoimg']['tmp_name']);
          $userfile_size =$_FILES['photoimg']['size'];
          $userfile_type = $_FILES['photoimg']['type'];

          if (isset($_FILES['photoimg']['name'])){
            $prod_img = $filedir.$actual_image_name;
            $prod_img_thumb = $thumbdir.$prefix.$userfile_name;
            move_uploaded_file($userfile_tmp, $prod_img);
            chmod ($prod_img, octdec($mode));
            $sizes = getimagesize($prod_img);
            $aspect_ratio = $sizes[1]/$sizes[0];

            if ($sizes[1] <= $size){
              $new_width = $sizes[0];
              $new_height = $sizes[1];
            }else{
              $new_height = $size;
              $new_width = abs($new_height/$aspect_ratio);
            }
            $destimg=ImageCreateTrueColor($new_width,$new_height)
            or die('Problem In Creating image');

            switch($ext){
              case "jpg":
              case "jpeg":
              case "JPG":
              case "JPEG":
                $srcimg=ImageCreateFromJPEG($prod_img)or die('Problem In opening Source Image');
                break;
              case "PNG":
              case "png":
                $srcimg = imageCreateFromPng($prod_img)or die('Problem In opening Source Image');
                imagealphablending($destimg, false);
                $colorTransparent = imagecolorallocatealpha($destimg, 0, 0, 0, 0x7fff0000);
                imagefill($destimg, 0, 0, $colorTransparent);
                imagesavealpha($destimg, true);

                break;
              case "BMP":
              case "bmp":
                $srcimg = imageCreateFromBmp($prod_img)or die('Problem In opening Source Image');
                break;
              case "GIF":
              case "gif":
                $srcimg = imageCreateFromGif($prod_img)or die('Problem In opening Source Image');
                break;
              default:
                $srcimg=ImageCreateFromJPEG($prod_img)or die('Problem In opening Source Image');
            }

            if(function_exists('imagecopyresampled')){
              imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,imagesX($srcimg),imagesY($srcimg))
              or die('Problem In resizing');
            }else{
              Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,imagesX($srcimg),imagesY($srcimg))
              or die('Problem In resizing');
            }



            // Saving an image
            switch(strtolower($ext)){
              case "jpg":
              case "jpeg":
                ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving');
                break;

              case "png":
                imagepng($destimg,$prod_img_thumb) or die('Problem In saving');
                break;

              case "bmp":
                imagewbmp($destimg, $prod_img_thumb)or die('Problem In saving');
                break;

              case "gif":
                imagegif($destimg,$prod_img_thumb) or die('Problem In saving');
                break;

              default:
                // if image format is unknown, and you whant save it as jpeg, maybe you should change file extension
                imagejpeg($destimg,$prod_img_thumb,90) or die('Problem In saving');
            }



          }
          unlink($prod_img);
          echo "<img src='".$prod_img_thumb."'>";
        }else{
          echo "Fail upload folder with read access.";
        }
      }else{
        echo "Image file size max 3 MB";
      }

    }else{
      echo "Invalid file format..";
    }
  }else{
    echo "Please select image..!";
  }

  exit;
}
?>

抱歉我的英文。问题是您尝试使用imagejpeg保存图像,这意味着新图像不再使用透明色。如果源图像是PNG,则应使用imagepng保存。