PHP 中的透明圆角

发布于 2024-10-03 08:07:06 字数 466 浏览 3 评论 0原文

是否可以使用 PHP 为动态图像创建透明角?我认为这是可能的,但我缺少一个在复制图像时保留 alpha 值的函数。

我的想法是创建一个相同宽度和高度的图像,然后应用透明角,但随后我需要保留 Alpha 通道并仅在该蒙版上复制图像,使透明仍然透明,但颜色更改为复制的图像(反之亦然) ,在图像上放置掩模)。

是否可以这样做?如果有的话,命令是什么?

更新:感谢您对此提供帮助。那是前一段时间了,我忘了,但如果有人遇到这个问题来寻找解决方案,请访问这个:http://www.pc-siete.g6.cz/galery.html 。我制作了渐变、径向渐变和圆角的函数,所以请随意使用:)。我并没有真正在我的网站上使用它,但准备好它们是很好的。

由于某种原因,下载的文件中只有广告。现在它存储在 zip 中并且可以正确下载。

Is it possible to create transparent corners for an image on fly with PHP? I think that it would be possible, but I am missing a function that would preserve alpha values when you copy your image.

My idea was to create an image of same width and height, then apply transparent corners, but then I need to preserve the alpha channel and just copy image on that mask, leaving transparent still transparent, but colors changed to copied image (or vice versa, put mask on image).

Is it possible to do that and what are commands for that if there are any?

Update: Thanks for helping with this. It was some time ago, and I forgot but if anybody cross this question to find a solution just visit this one: http://www.pc-siete.g6.cz/galery.html . I made functions for gradient, radial gradient and also those rounded corners so feel free to use :) . I'm not really using it on my webstie, but it's good to have them prepared.

For some reason the downloaded file had just ad in it. Now it's stored inside zip and downloads properly.

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

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

发布评论

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

评论(4

虐人心 2024-10-10 08:07:06

据我所知,没有内置函数可以做到这一点。不过,您可以按照以下方式自己创建一个:

function imageapplyroundedcorners(&$img,$radius) {
    // for each corner
        // loop through pixels between corner and (corner +- radius)
            // if distance between pixel and radius > radius, make transparent
            // elseif distance > radius-1 make partially transparent (for antialiasing)
}

As far as I know there is no built-in function to do this. However you can create one yourself along these lines:

function imageapplyroundedcorners(&$img,$radius) {
    // for each corner
        // loop through pixels between corner and (corner +- radius)
            // if distance between pixel and radius > radius, make transparent
            // elseif distance > radius-1 make partially transparent (for antialiasing)
}
假扮的天使 2024-10-10 08:07:06

我刚刚在 PHP 中使用 ImageMagick 创建了这个函数,它完全符合我的要求。您需要设置TEMP_DIR常量,并在执行路径上放置convert可执行文件。

function png_corners($image, $r = 12)
{
    //Dump out the image as a PNG file.
    $tmp_file = tempnam(TEMP_DIR, "image") . ".png";
    imagepng($image, $tmp_file);

    //Final image file.
    $tmp_out_file = tempnam(TEMP_DIR, "out") . ".png";

    $x = imagesx($image);
    $y = imagesy($image);

    $cmd = "convert -size {$x}x{$y} xc:none -fill white -draw 'roundRectangle 0,0, {$x} ,{$y}, {$r}, {$r}' {$tmp_file} -compose SrcIn -composite {$tmp_out_file}";
    exec($cmd);

    header("Content-Type: image/png");
    $contents = file_get_contents($tmp_out_file);
    echo $contents;
    exit;
}

I just created this function using ImageMagick in PHP which does exactly what I wanted. You need to set the TEMP_DIR constant and have the convert executable on your execution path.

function png_corners($image, $r = 12)
{
    //Dump out the image as a PNG file.
    $tmp_file = tempnam(TEMP_DIR, "image") . ".png";
    imagepng($image, $tmp_file);

    //Final image file.
    $tmp_out_file = tempnam(TEMP_DIR, "out") . ".png";

    $x = imagesx($image);
    $y = imagesy($image);

    $cmd = "convert -size {$x}x{$y} xc:none -fill white -draw 'roundRectangle 0,0, {$x} ,{$y}, {$r}, {$r}' {$tmp_file} -compose SrcIn -composite {$tmp_out_file}";
    exec($cmd);

    header("Content-Type: image/png");
    $contents = file_get_contents($tmp_out_file);
    echo $contents;
    exit;
}
箹锭⒈辈孓 2024-10-10 08:07:06

与其尝试重新发明轮子,不如使用 PHP 缩略图,它将为您完成一切:

require 'Thumbnailer.php';

$th=new Thumbnailer("photo.jpg");
$th->thumbFixed(120,90)->round()->save("thumb.jpg");

Instead of trying to reinvent the wheel use the PHP thumbnailer which will make everything for you:

require 'Thumbnailer.php';

$th=new Thumbnailer("photo.jpg");
$th->thumbFixed(120,90)->round()->save("thumb.jpg");
悟红尘 2024-10-10 08:07:06

请参阅 ImageMagick 参考。是的,你所描述的一切都是可能的,所有的工具都在那里。

See the ImageMagick reference. Yes, everything you describe is possible, all the tools are there.

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