繁体   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