0

我已经尝试了所有方法,但我找不到正确的解决方案,为什么我无法使用 alpha 获取我的 png,它们都是黑色或没有透明度。我到处搜索,无法让它正常工作。

<?php
ini_set('memory_limit', '-1');
function create_thumb($directory, $image, $destination) {
  $image_file = $image;
  $image = $directory.$image;
  if (file_exists($image)) {
    $source_size = getimagesize($image);
    if ($source_size !== false) {
      $thumb_width = 189;
      $thumb_height = 120;
      switch($source_size["mime"]) {
        case 'image/jpeg':
             $source = imagecreatefromjpeg($image);
        break;
        case 'image/jpg':
             $source = imagecreatefromjpeg($image);
        break;
        case 'image/png':
             $source = imagecreatefrompng($image);
             imagealphablending($png, false);
             imagesavealpha($png, true);
        break;
        case 'image/gif':
             $source = imagecreatefromgif($image);
        break;
      }
      $source_aspect = round(($source_size[0] / $source_size[1]), 1);
      $thumb_aspect = round(($thumb_width / $thumb_height), 1);
      if ($source_aspect < $thumb_aspect) {
        $new_size = array($thumb_width, ($thumb_width / $source_size[0]) * $source_size[1]);
        $source_pos = array(0, ($new_size[1] - $thumb_height) / 2);
      } else if ($source_aspect > $thumb_aspect) {
        $new_size = array(($thumb_width / $source_size[1]) * $source_size[0], $thumb_height);
        $source_pos = array(($new_size[0] - $thumb_width) / 2, 0);
      } else {
        $new_size = array($thumb_width, $thumb_height);
        $source_pos = array(0, 0);
      }
      if ($new_size[0] < 1) $new_size[0] = 1;
      if ($new_size[1] < 1) $new_size[1] = 1;
      $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
      imagealphablending($source, false);
      imagesavealpha($source, true);
      $transparent = imagecolorallocatealpha($source, 255, 255, 255, 127);
      imagecopyresampled($thumb, $source, 0, 0, 0, 0, $source_pos[0],
       $source_pos[1], $new_size[0], $new_size[1], $source_size[0], $source_size[1]);
      switch($source_size["mime"]) {
        case 'image/jpeg':
             imagejpeg($thumb, $destination.$image_file);
        break;
        case 'image/jpg':
             imagejpeg($thumb, $destination.$image_file);
        break;
        case 'image/png':
              imagepng($thumb, $destination.$image_file);
        break;
        case 'image/gif':
             imagegif($thumb, $destination.$image_file);
        break;
      }
    }
  }
}

?>
4

1 回答 1

0

我也遇到过这个问题。一种解决方法是首先使用图像中不太可能出现的背景颜色填充图像。然后将该颜色设置为透明颜色。

于 2013-03-29T16:32:16.980 回答