如何在 PHP 中绘制稍微对角的渐变填充?

发布于 2024-08-21 05:11:44 字数 1441 浏览 5 评论 0原文

我发现下面的函数可以在PHP中绘制垂直渐变。然而,许多网页设计师喜欢他们的渐变有一个左上角的光源,以使渐变看起来更真实。那么,如何在垂直渐变上稍微改变角度,使其变成轻微渐变呢?我不想完全做得太过分,只是当它沿着垂直梯度向下移动时,只需向右轻微移动即可。

<?php

function hex2rgb($sColor) {
    $sColor = str_replace('#','',$sColor);
    $nLen = strlen($sColor) / 3;
    $anRGB = array();
    $anRGB[]=hexdec(str_repeat(substr($sColor,0,$nLen),2/$nLen));
    $anRGB[]=hexdec(str_repeat(substr($sColor,$nLen,$nLen),2/$nLen));
    $anRGB[]=hexdec(str_repeat(substr($sColor,2*$nLen,$nLen),2/$nLen));
    return $anRGB;
}

$nWidth = 960;
$nHeight = 250;
$sStartColor = '#2b8ae1';
$sEndColor = '#0054a1';
$nStep = 1;

$hImage = imagecreatetruecolor($nWidth,$nHeight);
$nRows = imagesy($hImage);
$nCols = imagesx($hImage);
list($r1,$g1,$b1) = hex2rgb($sStartColor);
list($r2,$g2,$b2) = hex2rgb($sEndColor);
$nOld_r = 0; $nOld_g = 0; $nOld_b = 0;
for ( $i = 0; $i < $nRows; $i=$i+1+$nStep ) {
    $r = ( $r2 - $r1 != 0 ) ? intval( $r1 + ( $r2 - $r1 ) * ( $i / $nRows ) ): $r1;
    $g = ( $g2 - $g1 != 0 ) ? intval( $g1 + ( $g2 - $g1 ) * ( $i / $nRows ) ): $g1;
    $b = ( $b2 - $b1 != 0 ) ? intval( $b1 + ( $b2 - $b1 ) * ( $i / $nRows ) ): $b1;
    if ( "$nOld_r,$nOld_g,$nOld_b" != "$r,$g,$b") {
        $hFill = imagecolorallocate( $hImage, $r, $g, $b );
    }
    imagefilledrectangle($hImage, 0, $i, $nCols, $i+$nStep, $hFill);
    $nOld_r= $r;
    $nOld_g= $g;
    $nOld_b= $b;
}
header("Content-type: image/png");
imagepng($hImage);

I found the following function can draw a vertical gradient in PHP. However, many web designers like their gradients to have an upper-left lightsource to make the gradient look more realistic. So, how do I change the angle slightly on the vertical gradient, making it into a slight gradient? I don't want to completely overdo it, but just a slight movement to the right as it proceeds down the vertical gradient.

<?php

function hex2rgb($sColor) {
    $sColor = str_replace('#','',$sColor);
    $nLen = strlen($sColor) / 3;
    $anRGB = array();
    $anRGB[]=hexdec(str_repeat(substr($sColor,0,$nLen),2/$nLen));
    $anRGB[]=hexdec(str_repeat(substr($sColor,$nLen,$nLen),2/$nLen));
    $anRGB[]=hexdec(str_repeat(substr($sColor,2*$nLen,$nLen),2/$nLen));
    return $anRGB;
}

$nWidth = 960;
$nHeight = 250;
$sStartColor = '#2b8ae1';
$sEndColor = '#0054a1';
$nStep = 1;

$hImage = imagecreatetruecolor($nWidth,$nHeight);
$nRows = imagesy($hImage);
$nCols = imagesx($hImage);
list($r1,$g1,$b1) = hex2rgb($sStartColor);
list($r2,$g2,$b2) = hex2rgb($sEndColor);
$nOld_r = 0; $nOld_g = 0; $nOld_b = 0;
for ( $i = 0; $i < $nRows; $i=$i+1+$nStep ) {
    $r = ( $r2 - $r1 != 0 ) ? intval( $r1 + ( $r2 - $r1 ) * ( $i / $nRows ) ): $r1;
    $g = ( $g2 - $g1 != 0 ) ? intval( $g1 + ( $g2 - $g1 ) * ( $i / $nRows ) ): $g1;
    $b = ( $b2 - $b1 != 0 ) ? intval( $b1 + ( $b2 - $b1 ) * ( $i / $nRows ) ): $b1;
    if ( "$nOld_r,$nOld_g,$nOld_b" != "$r,$g,$b") {
        $hFill = imagecolorallocate( $hImage, $r, $g, $b );
    }
    imagefilledrectangle($hImage, 0, $i, $nCols, $i+$nStep, $hFill);
    $nOld_r= $r;
    $nOld_g= $g;
    $nOld_b= $b;
}
header("Content-type: image/png");
imagepng($hImage);

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

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

发布评论

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

评论(2

你的他你的她 2024-08-28 05:11:44

我不打算做几何图形 - 而是创建垂直渐变作为更大的图像,然后旋转并裁剪它:

...
$degrees = -5;
$newImage = imagecreatetruecolor($nWidth, $nHeight);
$rotated = imagerotate($hImage, $degrees, 0);
imagecopy($newImage, $rotated, 0, 0, $x, $y, $width, $height)

I'm not going to do the geometry - but create the vertical gradient as a larger image then rotate and crop it:

...
$degrees = -5;
$newImage = imagecreatetruecolor($nWidth, $nHeight);
$rotated = imagerotate($hImage, $degrees, 0);
imagecopy($newImage, $rotated, 0, 0, $x, $y, $width, $height)
数理化全能战士 2024-08-28 05:11:44

以下代码片段的运行速度比 GD 库快得多,而且没有复杂性。不过,您必须安装 PHP 的 ImageMagick 扩展。

$oImage = new Imagick();
$oImage->newPseudoImage(1000, 400, 'gradient:#09F-#048' );
$oImage->rotateImage(new ImagickPixel(), -3);
$oImage->cropImage(960, 250, 25, 100);
$oImage->setImageFormat('png');
header( "Content-Type: image/png" );
echo $oImage;

The following snippet runs much faster than the GD libraries and without the complexity. You have to install the ImageMagick extension for PHP, though.

$oImage = new Imagick();
$oImage->newPseudoImage(1000, 400, 'gradient:#09F-#048' );
$oImage->rotateImage(new ImagickPixel(), -3);
$oImage->cropImage(960, 250, 25, 100);
$oImage->setImageFormat('png');
header( "Content-Type: image/png" );
echo $oImage;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文