{"id":297,"date":"2011-04-19T15:25:42","date_gmt":"2011-04-19T15:25:42","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=297"},"modified":"2012-07-26T13:27:04","modified_gmt":"2012-07-26T13:27:04","slug":"generate-gradient-image-with-php","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/web-development\/php\/generate-gradient-image-with-php\/","title":{"rendered":"Generate gradient image with php"},"content":{"rendered":"<p>There are a lot of nice effects you can get using php&#8217;s <a title=\"PHP's GB Library\" href=\"http:\/\/php.net\/manual\/en\/book.image.php\">gd library<\/a>. The one I decided to show now is a very simple way to render an <strong>image <\/strong>with a <strong>gradient <\/strong>between <strong>any two colors<\/strong>.<\/p>\n<p>You will be able to use this script for a wide range of needs considering that it gives you the possibility to send as a parameter all the needed variables (initial color, final color, size and orientation).<\/p>\n<p>The main idea behind this script is the fact that if you know the size of the needed output (in pixels) you can use it as the number of steps needed in making the <strong>transition <\/strong>between the two colors. Also, because each color can be expressed using the red, blue and green components with values between 0 and 255 you can get the arithmetic progression&#8217;s common difference by dividing the difference between each colors&#8217; components to the number of steps (-1 for the initial step).<\/p>\n<p>This way if the initial color has the red component 10 and the final color 50 and you need a 5 pixels image the red values for the steps will be 10, 20, 30, 40 and 50.<\/p>\n<p>So at each step we will use something like this:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$color_red = floor($step * ($final_color_red-$initial_color_red) \/ $image_size)+$initial_color_red;\r\n<\/pre>\n<p>In order to assign the colors to the <strong>gd resource<\/strong> we can use the <a title=\"php ImageColorAllocate function\" href=\"http:\/\/php.net\/imagecolorallocate\">imagecolorallocate<\/a> function this way:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$color = imagecolorallocate($image, $color_red, $color_green, $color_blue);\r\n<\/pre>\n<p>Now that we have the color for the current step we need to <strong>draw a line<\/strong> using it, like this:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nimageline($image, 0, $step, $image_width, $step, $color);\r\n<\/pre>\n<p>That&#8217;s all the theory you need to understand this script.<br \/>\nYou can <strong>download a full working example<\/strong> <a title=\"Download script\" href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/04\/gradient.zip\">here<\/a>.<\/p>\n<p>To use this script you can call it in its default configuration like this http:\/\/localhost\/gradient.php and you will get a picture like this one:<\/p>\n<div id=\"attachment_295\" style=\"width: 110px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/04\/default.png\"><img aria-describedby=\"caption-attachment-295\" loading=\"lazy\" class=\"size-full wp-image-295\" title=\"Default Script Output\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/04\/default.png\" alt=\"Default Script Output\" width=\"100\" height=\"100\" \/><\/a><p id=\"caption-attachment-295\" class=\"wp-caption-text\">Default Script Output<\/p><\/div>\n<p>A custom way to use this script would be like this: http:\/\/localhost\/gradient.php?width=300&amp;height=200&amp;from_r=0&amp;from_g=255&amp;from_b=0&amp;to_r=0&amp;to_g=0&amp;to_b=255 and you will get a 300&#215;200 image with a gradient between green and blue that will look like this:<\/p>\n<div id=\"attachment_296\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/04\/custom.png\"><img aria-describedby=\"caption-attachment-296\" loading=\"lazy\" class=\"size-full wp-image-296\" title=\"Custom Script Output\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/04\/custom.png\" alt=\"Custom Script Output\" width=\"300\" height=\"200\" \/><\/a><p id=\"caption-attachment-296\" class=\"wp-caption-text\">Custom Script Output<\/p><\/div>\n<p>That&#8217;s all about this script, I hope that you will like it and most of all find it useful. If you have any questions or comments please feel free to use the form below.<\/p>\n<div id=\"share-and-beer-container\">\t<div id=\"buy_me_a_beer_div\" class=\"single beer\">\n\t \t\n\t\t<div class=\"buy-beer\" onclick=\"document.getElementById('buy_me_a_beer_form').submit();\">\n\t\t\t<form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" id=\"buy_me_a_beer_form\" method=\"post\">\n\t\t\t\t<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" \/>\n\t\t\t\t<input type=\"hidden\" name=\"business\" value=\"info@directaccess.ro\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_name\" value=\"A Beer For Generate gradient image with php\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_number\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"return\" value=\"https:\/\/www.osd.net\/blog\/web-development\/php\/generate-gradient-image-with-php\/\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"amount\" value=\"5\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"undefined_quantity\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"currency_code\" value=\"USD\" \/>  \n\t\t\t<\/form>\n\t\t\t<p class=\"buy-beer-text\">If you liked this post <br \/> you can <strong>buy me a beer<\/strong><\/p>\n\t\t<\/div>\n\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>There are a lot of nice effects you can get using php&#8217;s gd library. The one I decided to show now is a very simple way to render an image with a gradient between any two colors. You will be able to use this script for a wide range of needs considering that it gives &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/generate-gradient-image-with-php\/\">Read more<\/a><\/div>\n<div class=\"like-excerpt\"><div\n        class=\"fb-like\"\n        data-href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/generate-gradient-image-with-php\/\"\n        data-layout=\"button_count\"\n        data-action=\"like\"\n        data-show-faces=\"false\"\n        data-share=\"false\">\n        <\/div><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[77,78,35],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/297"}],"collection":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/comments?post=297"}],"version-history":[{"count":5,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/297\/revisions"}],"predecessor-version":[{"id":787,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/297\/revisions\/787"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}