簡體   English   中英

如何在PHP中將PNG轉換為JPEG透明度?

[英]How to convert PNG to JPEG with transparency in PHP?

上傳PNG文件時透明度似乎有問題。 為了解決這個問題,我需要添加什么?

<?php
  include 'thumbnailer_func.php';
  $image_file = str_replace ('..', '', $_SERVER['QUERY_STRING']);
  $image_path = '' . $_REQUEST['image'];
  $img = null;
  $ext = strtolower (end (explode ('.', $image_path)));
  if (($ext == 'jpg' OR $ext == 'jpeg')) {
    $img = @imagecreatefromjpeg ($image_path);
  } else {
    if ($ext == 'png') {
      $img = @imagecreatefrompng ($image_path);
    } else {
      if ($ext == 'gif') {
        $img = @imagecreatefromgif ($image_path);
      }
    }
  }

  if ($img) {
    $width = imagesx ($img);
    $height = imagesy ($img);
    $scale = min (MAX_WIDTH / $width, MAX_HEIGHT / $height);
    if ($scale < 1) {
     $new_width = floor ($scale * $width);
     $new_height = floor ($scale * $height);
     $tmp_img = imagecreatetruecolor ($new_width, $new_height);
     imagecopyresized ($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
     imagedestroy ($img);
     $img = $tmp_img;
   }
}

if (!$img) {
  $img = imagecreate (MAX_WIDTH, MAX_HEIGHT);
  imagecolorallocate ($img, 0, 0, 0);
  $c = imagecolorallocate ($img, 255, 255, 255);
  imageline ($img, 0, 0, MAX_WIDTH, MAX_HEIGHT, $c2);
  imageline ($img, MAX_WIDTH, 0, 0, MAX_HEIGHT, $c2);
}

header ('Content-type: image/jpeg');
imagejpeg ($img, null, $image_quality);
imagedestroy ($img);
?> 

你不能。 JPEG圖像格式不支持透明度。

暫無
暫無

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

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