libgd.bitbucket.org / pages / faq.html

<!DOCTYPE html>
<html lang="en">
<head>
        <title>Frequently Asked Questions</title>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="../theme/css/main.css" type="text/css" />
                <link href="http://libgd.org/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="GD Graphics Library Atom Feed" />
                
        <!--[if IE]>
                <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->

        <!--[if lte IE 7]>
                <link rel="stylesheet" type="text/css" media="all" href="../css/ie.css"/>
                <script src="../js/IE8.js" type="text/javascript"></script><![endif]-->

        <!--[if lt IE 7]>
                <link rel="stylesheet" type="text/css" media="all" href="../css/ie6.css"/><![endif]-->

</head>

<body id="index" class="home">
        <header id="banner" class="body">
				<img src="/theme/images/logo.png" style="float: right; margin-top: -35px;">

                <nav><ul>
                                                                    <li><a href="../pages/about.html">About</a></li>
                                    <li><a href="../pages/downloads.html">Downloads</a></li>
                                    <li><a href="../pages/faq.html">Frequently Asked Questions</a></li>
                                                                    <li ><a href="../category/news.html">News</a></li>
                                </ul></nav>
        </header><!-- /#banner -->
                
<section id="content" class="body">    
    <h1 class="entry-title">Frequently Asked Questions</h1>
        
        <div class="section" id="what-does-gd-stand-for">
<h2>What does &quot;gd&quot; stand for?</h2>
<p>In gd 1.0, it stood for &quot;gif draw.&quot; After the Unisys patent on the LZW
compression used in GIF came to light and GIF support was dropped, it
did not officially stand for anything, but let's just say &quot;graphics
draw&quot; and leave it at that. (GIF support is back, thanks to the
expiration of the patent, but gd can draw much more than GIFs.)</p>
</div>
<div class="section" id="does-gd-support-gif-images">
<h2>Does gd support GIF images?</h2>
<p>Yes. Support for GIF was restored in gd 2.0.28 on July 21st,
2004. Support for creating GIF animations is also available. Note that
gdlib-config --features can be used to list the image formats
supported by gd. Versions of gdlib-config prior to recent updates do
not support the --features option, which can be understood to mean
that GIF is not available.</p>
</div>
<div class="section" id="how-do-i-install-gd-on-linux">
<h2>How do I install gd on Linux?</h2>
<p>Well, what do you really want?</p>
<p>If you want gd for a PHP application, just do (for Fedora):</p>
<div class="highlight"><pre>yum install php-gd
</pre></div>
<p>Or, for Red Hat Enterprise Linux or CentOS:</p>
<div class="highlight"><pre>up2date php-gd
</pre></div>
<p>Then do:</p>
<div class="highlight"><pre>service httpd restart
</pre></div>
<p>If your system is Debian based (Debian/Ubuntu/...) then you need to install <cite>php5-gd</cite> package:</p>
<div class="highlight"><pre>apt-get install php5-gd
</pre></div>
<p>Other Linux distributions may have gd already compiled into PHP, or
they may have a similar php-gd package that brings in PHP's gd
module. PHP includes its own distribution of gd, you do not need mine.</p>
<p>If you really want gd for C programming or some other language that
has an extension built on top of my distribution of gd, do:</p>
<div class="highlight"><pre>yum install gd-devel
</pre></div>
<p>or on Debian based systems do:</p>
<div class="highlight"><pre>apt-get install libgd2-xpm-dev
</pre></div>
<p>Or the equivalent for your Linux distribution.</p>
<p>Note: this might not install the latest, most-cutting edge version of
gd, depending on the Linux distribution you are running and how
current their gd packages are at the moment.</p>
</div>
<div class="section" id="how-do-i-load-an-image-file-from-a-buffer-in-memory">
<h2>How do I load an image file from a buffer in memory?</h2>
<p>The following C code shows how to load an entire image file into a
buffer in memory, then ask gd to read the image from that
buffer. Please note that since you are responsible for allocating the
buffer, You are also responsible for freeing the buffer with your
normal memory management functions.</p>
<p>Of course, there is a gdImageCreateFromJpegPtr function available as
well.  This particular example loads a PNG image.</p>
<div class="highlight"><pre>#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;stdlib.h&gt;

gdImagePtr myLoadPng(char *filename)
{
  FILE *in;
  struct stat stat_buf;
  gdImagePtr im;
  in = fopen(&quot;myimage.png&quot;, &quot;rb&quot;);
  if (!in) {
    /* Error */
  }
  if (fstat(fileno(in), &amp;stat_buf) != 0) {
    /* Error */
  }
  /* Read the entire thing into a buffer
    that we allocate */
  char *buffer = malloc(stat_buf.st_size);
  if (!buffer) {
    /* Error */
  }
  if (fread(buffer, 1, stat_buf.st_size, in)
    != stat_buf.st_size)
  {
    /* Error */
  }
  im = gdImageCreateFromPngPtr(
    stat_buf.st_size, buffer);
  /* WE allocated the memory, WE free
    it with our normal free function */
  free(buffer);
  fclose(in);
  return im;
}
</pre></div>
</div>
<div class="section" id="how-do-i-save-an-image-to-a-buffer-in-memory">
<h2>How do I save an image to a buffer in memory?</h2>
<p>The following C code shows how to save a gd image to a memory buffer,
and then write that buffer to a file on disk. You could also write it
directly to stdout, preceded by a Content-type: image/png header and
two CR/LF sequences, to directly return an image from a CGI program.</p>
<p>For your convenience, gd allocates the buffer for you; when you are
done with it, you must call gdFree to release it.</p>
<p>Of course, there is a gdImageJpegPtr function available as well.  This
particular example saves a PNG image.</p>
<div class="highlight"><pre>#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;stdlib.h&gt;

void mySavePng(char *filename,
  gdImagePtr im)
{
  FILE *out;
  int size;
  char *data;
  out = fopen(&quot;filename, &quot;wb&quot;);
  if (!out) {
    /* Error */
  }
  data = (char *) gdImagePngPtr(im, &amp;size);
  if (!data) {
    /* Error */
  }
  if (fwrite(data, 1, size, out) != size) {
    /* Error */
  }
  if (fclose(out) != 0) {
    /* Error */
  }
  gdFree(data);
}
</pre></div>
</div>
<div class="section" id="why-doesn-t-my-gd-1-x-program-work-well-with-gd-2-x">
<h2>Why doesn't my gd 1.x program work well with gd 2.x?</h2>
<p>There are two common reasons:</p>
<ol class="arabic simple">
<li>You were using an ancient version of gd 1.x with GIF support, and now you are using a not-quite-new-enough version of gd 2.x without GIF support. Solution: get the latest gd 2.x which again supports GIF.</li>
<li>You are trying to make thumbnails, or otherwise copy photographic-quality images like JPEG files. But you are creating the new image with gdImageCreate, which makes a palette-color image not suitable for photographs. Solution: use gdImageCreateTrueColor (new in gd 2.x), which creates a true-color image. This sort of &quot;worked&quot; in gd 1.x because gd 1.x did a quick and nasty job of converting JPEGs to palette color when reading them. But you will get much better results doing it the right way in gd 2.x. If you really want to, you can reduce to palette color after the copy using gdImageTrueColorToPalette.</li>
</ol>
</div>
<div class="section" id="why-does-gd-cause-my-php-script-to-run-out-of-memory">
<h2>Why does gd cause my PHP script to run out of memory?</h2>
<p>Most often, the problem is that the memory_limit parameter in php.ini
is set to something very conservative, like 8M (eight
megabytes). Increase that setting and restart the web server.  Of
course, opening truly huge images can cause real memory problems, if
several are open at once. 8,000 pixels times 8,000 pixels times four
bytes for truecolor equals a walloping 256 megabytes.</p>
<p>How can I determine the image dimensions without loading the entire
image into memory (and possibly running out)?</p>
<p>Very large images can cause gd to run out of memory (see the previous
question). And sometimes the image file itself isn't terribly large—
consider a JPEG of a completely blank field, 8,000 pixels on a side:
the file compresses well but representing it in memory as a bitmap is
impractical.  If you are coding in PHP, you can check for this
situation with the getimagesize function, which determines the image
dimensions without using gd. This is possible because the popular
image formats all store the image dimensions near the beginning of the
file where they are easily accessible. Perl programmers can use the
similar Image::Size CPAN module. If you are not using PHP or Perl and
your language of choice does not offer a similar feature, you can
implement the technique yourself. See the GIF specification, the JPEG
specification, and the PNG specification.</p>
</div>

</section>
        <section id="extras" class="body">
                        <div class="blogroll">
                        <h2>blogroll</h2>
                        <ul>
                                                    <li><a href="https://bitbucket.org/libgd/gd-libgd">BitBucket</a></li>
                                                    <li><a href="https://bitbucket.org/libgd/gd-libgd/issues">Issue Tracker</a></li>
                                                    <li><a href="https://bitbucket.org/libgd/gd-libgd/wiki/Home">Wiki</a></li>
                                                </ul>
                </div><!-- /.blogroll -->
                        </section><!-- /#extras -->

        <footer id="contentinfo" class="body">
                <address id="about" class="vcard body">
                Proudly powered by <a href="http://getpelican.com/">Pelican</a>, which takes great advantage of <a href="http://python.org">Python</a>.
                </address><!-- /#about -->
        </footer><!-- /#contentinfo -->

</body>
</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.