【问题标题】:Rotate a PNG then resave with Image Transparency旋转 PNG,然后使用图像透明度重新保存
【发布时间】:2012-11-12 11:26:26
【问题描述】:

在正在旋转的 PNG 上获取 PNG 透明度时遇到了一些重大问题。

$filename = 'bird_up.png';
$source = imagecreatefrompng($filename) or die('Error opening file '.$filename);
imagealphablending($source, false);
imagesavealpha($source, true);
$rotation = imagerotate($source, $degrees, imageColorAllocateAlpha($source, 0, 0, 0, 127));
imagealphablending($source, false);
imagesavealpha($source, true);
header('Content-type: image/png');
imagepng($rotation);
imagedestroy($source);
imagedestroy($rotation);

【问题讨论】:

    标签: php rotation png transparent alpha


    【解决方案1】:

    我在下面添加了一个有效的评论版本

    <?php
    // this file writes the image into the http response,
    // so we cant have any output other than headers and the file data
    ob_start();
    
    $filename       = 'tibia.png';
    $degrees        = 45;
    
    // open the image file
    $im = imagecreatefrompng( $filename );
    
    // create a transparent "color" for the areas which will be new after rotation
    // r=0,b=0,g=0 ( black ), 127 = 100% transparency - we choose "invisible black"
    $transparency = imagecolorallocatealpha( $im,0,0,0,127 );
    
    // rotate, last parameter preserves alpha when true
    $rotated = imagerotate( $im, $degrees, $transparency, 1);
    
    // disable blendmode, we want real transparency
    imagealphablending( $rotated, false );
    // set the flag to save full alpha channel information
    imagesavealpha( $rotated, true );
    
    // now we want to start our output
    ob_end_clean();
    // we send image/png
    header( 'Content-Type: image/png' );
    imagepng( $rotated );
    // clean up the garbage
    imagedestroy( $im );
    imagedestroy( $rotated );
    

    演示 原图来自Wikipedia

    旋转 -45 度,新区域的不透明度约为 50% 用于演示

    $transparency = imagecolorallocatealpha( $im,0,0,0,55 );
    

    旋转 -45 度,100% 不透明度的新区域

    $transparency = imagecolorallocatealpha( $im,0,0,0,127 );
    

    【讨论】:

    • 这适用于 90/180,但超出 45/90/等的度数,例如 37 等,仍然会中断。有什么想法吗?
    • 抱歉,在 imagecolorallocatealpha 函数中有错字。现在已更正(图像资源参数是第一个)
    • 啊 - 更接近了 - 现在唯一剩下的问题是它似乎认为盒子仍然存在,这是 alpha,无论如何要在最后修剪它?
    • 如果这样更容易,我也可以给它一个固定的宽度/高度?
    • 可见与否——你所说的“盒子”只是图像数据。当然你可以在旋转后裁剪或调整大小
    【解决方案2】:

    <?php
                $text ="New"; $font = "fonts/AARDV.ttf"; $size = 100;
                function html2rgb()
                {
                    $color='FF0000';
                    if(strlen($color)==6) 
                    list($r, $g, $b)=array($color[0].$color[1],$color[2].$color[3],$color[4].$color[5]);
                    elseif (strlen($color) == 3)  
                    list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
                    else 
                    return false; 
                    $r = hexdec($r); 
                    $g = hexdec($g); 
                    $b = hexdec($b); 
                    return array($r, $g, $b);
                }
                $c=html2rgb(); 
                $bbox = imagettfbbox($size, 0, $font, $text); 
                $width = abs($bbox[2] - $bbox[0]); 
                $height = abs($bbox[7] - $bbox[1]);
                $image = imagecreatetruecolor($width, $height);
                $bgcolor = imagecolorallocate($image, 1, 1,0);
                imagecolortransparent($image, $bgcolor);
                $color = imagecolorallocate($image, $c[0],$c[1],$c[2]);
                $x = $bbox[0] + ($width / 2) - ($bbox[4] / 2); 
                $y = $bbox[1] + ($height / 2) - ($bbox[5] / 2);
                imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $bgcolor);
                imagettftext($image, $size, 0, $x, $y, $color, $font, $text); 
                $last_pixel= imagecolorat($image, 0, 0);
                for ($j = 0; $j < $height; $j++) 
                { 
                    for ($i = 0; $i < $width; $i++)
                    {
                        if (isset($blank_left) && $i >= $blank_left) 
                        {
                            break;      
                        } 
                        if (imagecolorat($image, $i, $j) !== $last_pixel)
                        {
                            if (!isset($blank_top))
                            {
                                $blank_top=$j;
                            }
                            $blank_left=$i;break;
                        }
                        $last_pixel=imagecolorat($image, $i, $j);
                    }
                }
    
                $x -= $blank_left;
                $y -= $blank_top;
                imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $bgcolor);
                imagettftext($image, $size, 0, $x, $y, $color, $font, $text);
                header('Content-type: image/png'); 
                $transparency = imagecolorallocatealpha( $image,0,0,0,127 );
                $image=imagerotate($image, 10,  $transparency, 1);
                imagealphablending( $image, false );
                imagesavealpha( $image, true );
                ob_end_clean();
                imagepng($image);
                imagedestroy($image);
                ?>
    
                This one not work..Black color
                any one please answer this..
    
    
                enter code here
    

    【讨论】:

      猜你喜欢
      • 2011-05-06
      • 2011-08-21
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      • 2011-10-14
      • 2012-06-06
      相关资源
      最近更新 更多