使用PHP将图像旋转45度时如何避免黑色背景?

发布于 2024-09-05 14:37:01 字数 95 浏览 4 评论 0原文

您好,我必须先翻转缩略图,然后才能将其与另一个 jpeg 文件合并。但是当我使用 php.ini 旋转 45 度时它显示黑色背景。我怎样才能避免这种情况。任何机构都可以帮助我。

Hi I have to flip a thumpnail image before merge it with another jpeg file. but when I rotate 45 degree using php. It shows a black background. how can I avoid that. any body can help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里°也失望 2024-09-12 14:37:01

好吧,如果您要生成 jpg,请使用 PHP GD 将背景颜色设置为函数 imagerotate 的第三个选项。在此示例中,我假设您将 jpg 图像 $filename 旋转任意 $angle 度,并且您想要白色背景,即颜色代码 16777215

$rotatedImage = imagerotate(imagecreatefromjpeg($filename), ((360-$angle)%360), 16777215);

黑色是颜色代码0,这是默认值,其余的颜色伽玛介于两者之间,因此您只需决定要

编辑的 背景颜色:
对于透明背景,如果您要生成 png,您会这样做:

$destimg = imagecreatefromjpeg($filename);
$transColor = imagecolorallocatealpha($destimg, 255, 255, 255, 127);
$rotatedImage = imagerotate($destimg, ((360-$angle)%360), $transColor);

希望有帮助

Well, if you are generating a jpg, using PHP GD you set the color of the background as the third option of the function imagerotate. In this example I'm gonna assume that you are rotating a jpg image $filename by an arbitrary $angle degrees, and you want a white background, i.e. color code 16777215:

$rotatedImage = imagerotate(imagecreatefromjpeg($filename), ((360-$angle)%360), 16777215);

black is color code 0, which is default, and the rest of the color gamma is in between the two, so you just need to decide which background color you would like

EDIT:
for transparent backgrounds, if you are generating a png you would do:

$destimg = imagecreatefromjpeg($filename);
$transColor = imagecolorallocatealpha($destimg, 255, 255, 255, 127);
$rotatedImage = imagerotate($destimg, ((360-$angle)%360), $transColor);

Hope that helps

給妳壹絲溫柔 2024-09-12 14:37:01
<?
$image = "130.jpg";
$degrees = 25;
for($i=0;$i<count($data);$i++){
    $ext = "";
    $extarr = "";
    $extarr = explode(".", $data[$i]['name']);
    $ext = array_pop($extarr);
    if($ext == "png"){
        $rotate = imagecreatefrompng("images/".$data[$i]['name']);
        $transColor = imagecolorallocatealpha($rotate, 255, 255, 255, 270);
        $watermark1[$i] = imagerotate($rotate, ((360-$degrees)%360), $transColor);
    }
}

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opct){
    $w = imagesx($src_im);
    $h = imagesy($src_im);
    $cut = imagecreatetruecolor($src_w, $src_h);
    imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
    imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
    imagecopymerge($dst_im, $cut, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, (100 - $opct));
}


for($i=0; $i<count($watermark1); $i++){
    if($i == 0) imagecopymerge_alpha($image, $watermark1[$i], $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
    else imagecopymerge_alpha($image, $watermark1[$i], ($i*$dest_x)*3, ($i*$dest_y)*15, 0, 0, $watermark_width, $watermark_height, $opacity);
    imagedestroy($watermark1[$i]);
}
header("content-type: image/png");
imagepng($image);
imagedestroy($image);
?>

另外,您的水印图像有 Alpha 通道还是完全不透明?

<?
$image = "130.jpg";
$degrees = 25;
for($i=0;$i<count($data);$i++){
    $ext = "";
    $extarr = "";
    $extarr = explode(".", $data[$i]['name']);
    $ext = array_pop($extarr);
    if($ext == "png"){
        $rotate = imagecreatefrompng("images/".$data[$i]['name']);
        $transColor = imagecolorallocatealpha($rotate, 255, 255, 255, 270);
        $watermark1[$i] = imagerotate($rotate, ((360-$degrees)%360), $transColor);
    }
}

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opct){
    $w = imagesx($src_im);
    $h = imagesy($src_im);
    $cut = imagecreatetruecolor($src_w, $src_h);
    imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
    imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
    imagecopymerge($dst_im, $cut, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, (100 - $opct));
}


for($i=0; $i<count($watermark1); $i++){
    if($i == 0) imagecopymerge_alpha($image, $watermark1[$i], $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
    else imagecopymerge_alpha($image, $watermark1[$i], ($i*$dest_x)*3, ($i*$dest_y)*15, 0, 0, $watermark_width, $watermark_height, $opacity);
    imagedestroy($watermark1[$i]);
}
header("content-type: image/png");
imagepng($image);
imagedestroy($image);
?>

Also, do your watermark images have alpha channel or are they fully opaque?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文