【问题标题】:How to convert PNG to JPEG with transparency in PHP?如何在 PHP 中将 PNG 转换为具有透明度的 JPEG?
【发布时间】:2014-06-30 20:17:51
【问题描述】:

上传 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格式(header ('Content-type: image/jpeg')); jpeg 无法支持透明度。
  • 那么我可以通过更改那段代码将其更改为 png 吗?

标签: php png transparency


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-09-14
    • 2011-11-28
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2015-01-26
    • 2011-04-23
    • 2021-12-06
    • 1970-01-01
    相关资源
    最近更新 更多