【问题标题】:How Do I Draw A Slightly Diagonal Gradient Fill in PHP?如何在 PHP 中绘制一个稍微对角渐变的填充?
【发布时间】:2010-02-10 21:59:55
【问题描述】:

我发现以下函数可以在 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);

【问题讨论】:

    标签: php image gd gradient


    【解决方案1】:

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

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

    【讨论】:

    • imagerotate() 在 PHP 5.2.4 上不适合我。它在这个函数的 php.net 页面上说它是 GD 库函数之一,它有内存泄漏并且不包含在 Ubuntu 中(这是我正在运行的)。还有其他选择吗?
    • php.net/manual/en/function.imagerotate.php#93151我从来没有使用过这个功能 - 但是有人发布了一个替代的 imageRotate 功能来解决这个看起来很有希望的问题。
    • 我尝试了很多,发现 imagerotateEquivalent() 成功了!谢谢,太子。
    • 只是想补充一下 thetaiko 的建议,如果我首先将 $nWidth 和 $nHeight 的大小加倍(在我的代码顶部),然后将 $x 设置为 25 并将 $y 设置为 171他的代码,然后他的代码中的 $width 和 $height 为 $nWidth 和 $nHeight 的一半,它使用 imagerotateEquivalent 100% 解决了这个问题。
    • 这不是线程安全的,但是如果安装了 ImageMagick,PHP 可以在 Linux 上执行这个命令: convert -size 1000x400 gradient:#bfb-#4b4 -rotate -5 +repage -裁剪“960x250+25+100”greenbar.png。但是,PHP 确实有一个 ImageMagick 扩展来在内部执行此操作。
    【解决方案2】:

    下面的 sn-p 运行速度比 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多