<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Allocate a color for an image</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.imagecolorallocate.html">imagecolorallocate</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.imagecolorat.html">imagecolorat</a></div> <div class="up"><a href="ref.image.html">GD and Image Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.imagecolorallocatealpha" class="refentry"> <div class="refnamediv"> <h1 class="refname">imagecolorallocatealpha</h1> <p class="verinfo">(PHP 4 >= 4.3.2, PHP 5)</p><p class="refpurpose"><span class="refname">imagecolorallocatealpha</span> — <span class="dc-title">Allocate a color for an image</span></p> </div> <div class="refsect1 description" id="refsect1-function.imagecolorallocatealpha-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>imagecolorallocatealpha</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$image</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$red</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$green</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$blue</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$alpha</code></span> )</div> <p class="para rdfs-comment"> <span class="function"><strong>imagecolorallocatealpha()</strong></span> behaves identically to <span class="function"><a href="function.imagecolorallocate.html" class="function">imagecolorallocate()</a></span> with the addition of the transparency parameter <em><code class="parameter">alpha</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.imagecolorallocatealpha-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter"> image</code></em></span><dd> <p class="para">An image resource, returned by one of the image creation functions, such as <span class="function"><a href="function.imagecreatetruecolor.html" class="function">imagecreatetruecolor()</a></span>.</p></dd> </dt> <dt> <span class="term"><em><code class="parameter">red</code></em></span> <dd> <p class="para">Value of red component.</p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">green</code></em></span> <dd> <p class="para">Value of green component.</p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">blue</code></em></span> <dd> <p class="para">Value of blue component.</p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">alpha</code></em></span> <dd> <p class="para"> A value between <em>0</em> and <em>127</em>. <em>0</em> indicates completely opaque while <em>127</em> indicates completely transparent. </p> </dd> </dt> </dl> The colors parameters are integers between 0 and 255 or hexadecimals between 0x00 and 0xFF. </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.imagecolorallocatealpha-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> A color identifier or <strong><code>FALSE</code></strong> if the allocation failed. </p> <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function may return Boolean <strong><code>FALSE</code></strong>, but may also return a non-Boolean value which evaluates to <strong><code>FALSE</code></strong>. Please read the section on <a href="language.types.boolean.html" class="link">Booleans</a> for more information. Use <a href="language.operators.comparison.html" class="link">the === operator</a> for testing the return value of this function.</p></div> </div> <div class="refsect1 changelog" id="refsect1-function.imagecolorallocatealpha-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.1.3</td> <td> Returns <strong><code>FALSE</code></strong> if the allocation failed. Previously <em>-1</em> was returned. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.imagecolorallocatealpha-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3184"> <p><strong>Example #1 Example of using <span class="function"><strong>imagecolorallocatealpha()</strong></span></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$size </span><span style="color: #007700">= </span><span style="color: #0000BB">300</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$image</span><span style="color: #007700">=</span><span style="color: #0000BB">imagecreatetruecolor</span><span style="color: #007700">(</span><span style="color: #0000BB">$size</span><span style="color: #007700">, </span><span style="color: #0000BB">$size</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// something to get a white background with black border<br /></span><span style="color: #0000BB">$back </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">, </span><span style="color: #0000BB">255</span><span style="color: #007700">, </span><span style="color: #0000BB">255</span><span style="color: #007700">, </span><span style="color: #0000BB">255</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$border </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">imagefilledrectangle</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$size </span><span style="color: #007700">- </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">$size </span><span style="color: #007700">- </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">$back</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">imagerectangle</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$size </span><span style="color: #007700">- </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">$size </span><span style="color: #007700">- </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">$border</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$yellow_x </span><span style="color: #007700">= </span><span style="color: #0000BB">100</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$yellow_y </span><span style="color: #007700">= </span><span style="color: #0000BB">75</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$red_x </span><span style="color: #007700">= </span><span style="color: #0000BB">120</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$red_y </span><span style="color: #007700">= </span><span style="color: #0000BB">165</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$blue_x </span><span style="color: #007700">= </span><span style="color: #0000BB">187</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$blue_y </span><span style="color: #007700">= </span><span style="color: #0000BB">125</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$radius </span><span style="color: #007700">= </span><span style="color: #0000BB">150</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// allocate colors with alpha values<br /></span><span style="color: #0000BB">$yellow </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecolorallocatealpha</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">, </span><span style="color: #0000BB">255</span><span style="color: #007700">, </span><span style="color: #0000BB">255</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">75</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$red </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecolorallocatealpha</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">, </span><span style="color: #0000BB">255</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">75</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$blue </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecolorallocatealpha</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">255</span><span style="color: #007700">, </span><span style="color: #0000BB">75</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// drawing 3 overlapped circle<br /></span><span style="color: #0000BB">imagefilledellipse</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">, </span><span style="color: #0000BB">$yellow_x</span><span style="color: #007700">, </span><span style="color: #0000BB">$yellow_y</span><span style="color: #007700">, </span><span style="color: #0000BB">$radius</span><span style="color: #007700">, </span><span style="color: #0000BB">$radius</span><span style="color: #007700">, </span><span style="color: #0000BB">$yellow</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">imagefilledellipse</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">, </span><span style="color: #0000BB">$red_x</span><span style="color: #007700">, </span><span style="color: #0000BB">$red_y</span><span style="color: #007700">, </span><span style="color: #0000BB">$radius</span><span style="color: #007700">, </span><span style="color: #0000BB">$radius</span><span style="color: #007700">, </span><span style="color: #0000BB">$red</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">imagefilledellipse</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">, </span><span style="color: #0000BB">$blue_x</span><span style="color: #007700">, </span><span style="color: #0000BB">$blue_y</span><span style="color: #007700">, </span><span style="color: #0000BB">$radius</span><span style="color: #007700">, </span><span style="color: #0000BB">$radius</span><span style="color: #007700">, </span><span style="color: #0000BB">$blue</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// don't forget to output a correct header!<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Type: image/png'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// and finally, output the result<br /></span><span style="color: #0000BB">imagepng</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">imagedestroy</span><span style="color: #007700">(</span><span style="color: #0000BB">$image</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="mediaobject"> <div class="imageobject"> <img src="images/21009b70229598c6a80eef8b45bf282b-imagecolorallocatealpha.png" alt="Output of example : Example of using imagecolorallocatealpha()" width="300" height="300" /> </div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.imagecolorallocatealpha-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function requires GD 2.0.1 or later (2.0.28 or later is recommended).</span></p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.imagecolorallocatealpha-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.imagecolorallocate.html" class="function" rel="rdfs-seeAlso">imagecolorallocate()</a> - Allocate a color for an image</span></li> <li class="member"> <span class="function"><a href="function.imagecolordeallocate.html" class="function" rel="rdfs-seeAlso">imagecolordeallocate()</a> - De-allocate a color for an image</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.imagecolorallocate.html">imagecolorallocate</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.imagecolorat.html">imagecolorat</a></div> <div class="up"><a href="ref.image.html">GD and Image Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>