簡體   English   中英

無法使用此代碼上傳具有透明背景的png圖像,適用於jpg圖像

[英]can't upload png image with transparent background with this code works good for jpg image

無法使用此代碼上傳具有透明背景的png圖像,因此對jpg圖像有效。

                <?php
   $hid =$_POST['id'];

   if(is_array($_FILES)) {
          if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
          $sourcePath = $_FILES['userImage']['tmp_name'];
          $targetPath = "images/hotels/".$_FILES['userImage']['name'];
          $image_info = getimagesize($targetPath); 

          if(move_uploaded_file($sourcePath,$targetPath)) {
    $orig_image = imagecreatefromjpeg($targetPath);
  $image_info = getimagesize($targetPath); 
  $width_orig  = $image_info[0]; // current width as found in image file
     $height_orig = $image_info[1]; // current height as found in image file
  $width = 1024; // new image width
     $height = 768; // new image height
   $destination_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($destination_image, $orig_image, 0, 0, 0, 0, $width, $height,          $width_orig, $height_orig);
// This will just copy the new image over the original at the same filePath.
         imagejpeg($destination_image, $targetPath, 100);
       $full = "http://somewebsite.co.in/".$targetPath;
       ?> 

    <?php
   }
     else
    {
         echo "upload Failed";
      }
    }
     }
       ?>

如PedroLobito所述,您的代碼僅用於支持JPG。 您至少使用2個僅用於JPG格式的功能: imagecreatefromjpegimagejpeg 對於PNG,請改用imagecreatefrompngimagepng

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM