On: 2006/02/02 Viewed: 26175 times

Here is a small PHP function I wrote that emulates a gradient fill of an image.
Disclaimer : there are probably existing alternatives or classes, maybe in PEAR. I didn't check. I wanted to write a function myself, as part of my GD training grounds.

Usage

The function is packaged as a PHP class, the code you need is something like :

PHP:
  1. require_once('/path/to/gd-gradient-fill.php');
  2. $image = new gd_gradient_fill($width,$height,$direction,$startcolor,$endcolor,$step);

The class call needs 5 arguments and accepts an optional 6th :

  1. integer $width
    Width of the image
  2. integer $height
    Height of the image
  3. string $direction
    The shape or direction of the gradient, which can be any of : vertical, horizontal, ellipse, ellipse2, circle, circle2, rectangle, diamond.
  4. string $startcolor
    Gradient start color, 3 or 6 digit hexadecimal ("#ff0" or "#dd4578")
  5. string $endcolor
    Gradient end color
  6. Optional : $step
    Breaks the gradient smooth blending

Results

Here are a few examples of gradients from color #101040 to #a1a1ff. Hover the image for some unnecessary explanation.

Gradient Fill : Vertical Gradient Fill : Horizontal Fill Gradient Fill : Ellipse Gradient Fill : Rectangle Gradient Fill : Diamond

Of course, the function handles non-square images just fine as well.

Get the function

Credits

I started from file header-img.php, part of Wordpress core file, which contains a similar hack to draw a gradient fill from lines. I believe this smart code is from Andy Skelton.

If you happen to use this function in any project, be sure to let me know. This makes my day :)

Related posts

Metastuff

This page "PHP and GD : Emulate Gradient Fill" was posted on 02/02/2006 at 6:37 pm
Watch this discussion : Comments RSS 2.0. You can trackback this post from your own site

27 Blablas

    Pages: « 1 [2] Show All

  1. 21
    Ozh France »
    commented, on 13/Sep/07 at 8:16 pm # :

    Tiziano » this is a pure CSS issue. Something like the following would work:

    CSS:
    1. td {background-image: url('url/to/image.php')

    Where image.php would be the file that generates your gradient.

    Orlando » this requires PHP extension GD, which you probably don't have on your server then.

  2. 22
    Dragen Great Britain (UK) »
    replied, on 15/Sep/07 at 6:17 pm # :

    hi,
    I've been testing your script for a while and am also using in some of my work, but during testing with ellipse and cirle it seems that ellipse is the same as circle2 and circle2 is the same as ellipse..
    Is this just me?

    Thanks

  3. 23
    kiuwenayk Philippines »
    replied, on 21/Oct/07 at 5:51 am # :

    hi,
    how do i stretch a gradient image to fill up the entire screen to use as background color. Like one whole ellipse filling the entire screen. Great code indeed!

  4. 24
    fahad Great Britain (UK) »
    replied, on 07/Feb/08 at 7:22 pm # :

    I used it in my site :)
    I used to use a static gradient, now I use your script, that either draws the static gradient, or uses the gradient stored in a cookie.

  5. 25
    Jordan Shaw Canada »
    thought, on 04/Apr/08 at 1:19 am # :

    This is great, thanks so much! works like a new million dollar bill.

  6. 26
    arne Netherlands »
    replied, on 27/Jun/08 at 10:13 pm # :

    hello, does anyone know how to get a vertical gradient that fades from color to transparant? i can't get it to work :(

  7. 27
    Asad Hasan United States »
    replied, on 21/Jul/08 at 11:51 pm # :

    Amazing job, this is GD at its best!

Pages: « 1 [2] Show All

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Close
E-mail It