相关疑难解决方法(0)

使用PHP的GDlib imagecopyresampled时,是否可以保留PNG图像透明度?

以下PHP代码段使用GD将浏览器上传的PNG大小调整为128x128.它的效果很好,除了原始图像中的透明区域在我的情况下被替换为纯黑色.

虽然imagesavealpha已经确定,但事情并不完全正确.

保留重采样图像透明度的最佳方法是什么?

$uploadTempFile = $myField[ 'tmp_name' ]
list( $uploadWidth, $uploadHeight, $uploadType ) 
  = getimagesize( $uploadTempFile );

$srcImage = imagecreatefrompng( $uploadTempFile );    
imagesavealpha( $targetImage, true );

$targetImage = imagecreatetruecolor( 128, 128 );
imagecopyresampled( $targetImage, $srcImage, 
                    0, 0, 
                    0, 0, 
                    128, 128, 
                    $uploadWidth, $uploadHeight );

imagepng(  $targetImage, 'out.png', 9 );
Run Code Online (Sandbox Code Playgroud)

php png transparency gd alpha

97
推荐指数
6
解决办法
9万
查看次数

PNG透明度与PHP

当我从它创建缩略图时,我在尝试保持png的透明度方面遇到一些麻烦,任何人都有这方面的经验吗?任何帮助都会很棒,这就是我目前正在做的事情:

$fileName= "../js/ajaxupload/tees/".$fileName;

list($width, $height) = getimagesize($fileName);

$newwidth = 257;
$newheight = 197;

$thumb = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($thumb, true);
$source = imagecreatefrompng($fileName);
imagealphablending($source, true);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagesavealpha($thumb, true);
imagepng($thumb,$newFilename);
Run Code Online (Sandbox Code Playgroud)

php png transparency

43
推荐指数
2
解决办法
5万
查看次数

imagecreatefrompng()使黑色背景而不是透明?

我使用PHP和GD库制作缩略图,但我的代码将png透明度变成纯黑色,是否有改进我的代码的解决方案?

这是我的php缩略图制造商代码:

function cropImage($nw, $nh, $source, $stype, $dest) {
     $size = getimagesize($source);
     $w = $size[0];
      $h = $size[1];

      switch($stype) {
          case 'gif':
          $simg = imagecreatefromgif($source);
          break;
          case 'jpg':
          $simg = imagecreatefromjpeg($source);
          break;
          case 'png':
          $simg = imagecreatefrompng($source);
          break;
      }

      $dimg = imagecreatetruecolor($nw, $nh);
      $wm = $w/$nw;
      $hm = $h/$nh;
      $h_height = $nh/2;
      $w_height = $nw/2;

      if($w> $h) {
          $adjusted_width = $w / $hm;
          $half_width = $adjusted_width / 2;
          $int_width = $half_width - $w_height;
          imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
      } elseif(($w <$h) || ($w == …
Run Code Online (Sandbox Code Playgroud)

php gd thumbnails

40
推荐指数
4
解决办法
7万
查看次数

标签 统计

php ×3

gd ×2

png ×2

transparency ×2

alpha ×1

thumbnails ×1