2013-11-28 7 views
0

사용자 이미지를 가져 와서 내 웹 서버에 업로드하는 PHP 페이지가 있습니다.PHP를 사용하여 배경을 투명하게 유지하고 배경을 투명하게 유지하는 PNG 이미지 업로드

투명 배경이있는 png를 업로드하고 업로드하면 배경이 흰색으로 변경됩니다.

예를 들어 투명한 배경을 가진 포토샵에서 새 이미지를 만든 다음 페이지에 색칠 된 원을 추가합니다. FTP로 이미지를 png 및 uplod로 저장하면 배경이 투명하기 때문에 이미지가 웹 페이지에 원으로 표시됩니다.

파일 입력 상자가있는 PHP를 사용하여 이미지를 업로드하면 이미지가 흰색으로 변경됩니다.

저는 transparental 배경을 유지하기 위해 imagealphablending, imagecolorallocatealpha, imagefill 및 imagesavealpha를 사용하려했지만 bg를 검정색으로 변경했습니다.

실마리가 있습니까? 내 영어에 대한

을 heres 내 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; 
    } 
    ?> 

답변

4
<?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를 사용하여 저장해야합니다.

+0

나는 무슨 뜻인지 알 수 있고 코드를 추가했지만, png는 여전히 검정색 배경을 가지고 있습니다. 나는 이것이 imagealphablending ($ destimg, false)와 관련이 있다고 생각합니다. $ colorTransparent = imagecolorallocatealpha ($ destimg, 0, 0, 0, 0x7fff0000); imagefill ($ destimg, 0, 0, $ colorTransparent); imagesavealpha ($ destimg, true); –

+0

0x7fff0000을 127로 바꾸십시오. – MaratMS

+0

죄송 합니다만 작동하지만 코드를 추가 할 때 imagejpeg을 삭제하는 것을 잊었습니다. –