如何在PHP中将PNG转换为JPEG透明度?

时间:2014-05-13 08:42:06

标签: php png transparency

上传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);
?> 

1 个答案:

答案 0 :(得分:2)

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