{"id":472,"date":"2006-02-02T18:37:59","date_gmt":"2006-02-02T16:37:59","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/my-projects\/images-php-gd-gradient-fill\/"},"modified":"2009-08-04T17:32:19","modified_gmt":"2009-08-04T15:32:19","slug":"images-php-gd-gradient-fill","status":"publish","type":"page","link":"https:\/\/planetozh.com\/blog\/my-projects\/images-php-gd-gradient-fill\/","title":{"rendered":"PHP and GD : Emulate Gradient Fill"},"content":{"rendered":"<p>Here is a small PHP function I wrote that emulates a gradient fill of an image.<br \/>\n<em>Disclaimer<\/em> : there are probably existing alternatives or classes, maybe in PEAR. I didn&#39;t check. I wanted to write a function myself, as part of my GD training grounds.<\/p>\n<h2>Usage<\/h2>\n<p>The function is packaged as a PHP class, the code you need is something like :<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">require_once<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'\/path\/to\/gd-gradient-fill.php'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span> <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$image<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000000;font-weight: bold\">new<\/span> gd_gradient_fill<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$width<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$height<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$direction<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$startcolor<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$endcolor<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$step<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>The class call needs 5 arguments and accepts an optional 6th :<\/p>\n<ol>\n<li>integer <strong>$width<\/strong><br \/>\nWidth of the image<\/li>\n<li>integer <strong>$height<\/strong><br \/>\nHeight of the image<\/li>\n<li>string <strong>$direction<\/strong><br \/>\nThe shape or direction of the gradient, which can be any of : vertical, horizontal, ellipse, ellipse2, circle, circle2, rectangle, diamond.<\/li>\n<li>string <strong>$startcolor<\/strong><br \/>\nGradient start color, 3 or 6 digit hexadecimal (&quot;#ff0&quot; or &quot;#dd4578&quot;)<\/li>\n<li>string <strong>$endcolor<\/strong><br \/>\nGradient end color<\/li>\n<li>Optional : <strong>$step<\/strong><br \/>\nBreaks the gradient smooth blending<\/li>\n<\/ol>\n<h2>Results<\/h2>\n<p>Here are a few examples of gradients from color #101040 to #a1a1ff. Hover the image for some unnecessary explanation.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/images\/gd-gradient-fill-vertical.png\" title=\"Gradient Fill : Vertical\" class=\"imgleft\"\/> <img decoding=\"async\" src=\"\/blog\/images\/gd-gradient-fill-horizontal.png\" title=\"Gradient Fill : Horizontal Fill\" class=\"imgleft\"\/> <img decoding=\"async\" src=\"\/blog\/images\/gd-gradient-fill-ellipse.png\" title=\"Gradient Fill : Ellipse\" class=\"imgleft\"\/> <img decoding=\"async\" src=\"\/blog\/images\/gd-gradient-fill-rectangle.png\" title=\"Gradient Fill : Rectangle\" class=\"imgleft\"\/> <img decoding=\"async\" src=\"\/blog\/images\/gd-gradient-fill-diamond.png\" title=\"Gradient Fill : Diamond\" class=\"imgleft\"\/><br clear=\"both\"><\/p>\n<p>Of course, the function handles non-square images just fine as well.<\/p>\n<h2>Get the function<\/h2>\n<ul>\n<li><a href=\"http:\/\/planetozh.com\/download\/gd-gradient-fill.txt\">gd-gradient-fill.txt<\/a> (download and save as .php)<\/li>\n<li><a href=\"http:\/\/planetozh.com\/download\/gd-gradient-fill.php\">gd-gradient-fill.php<\/a> (highlighted code, copy and paste in a blank file, but do not download as is)<\/li>\n<\/ul>\n<h2>Credits<\/h2>\n<p>I started from file <em>header-img.php<\/em>, 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.<\/p>\n<p>If you happen to use this function in any project, be sure to let me know. This makes my day :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#39;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":241,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-472","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/pages\/472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=472"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/pages\/472\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/pages\/241"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}