title: supplied argument is not a valid Image resource in php codeigniter
body: <pre><code>&lt;?php
    if (!defined('BASEPATH'))
        exit('No direct script access allowed');
    function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
    {
        $defaults = array('word' =&gt; '', 'word_length' =&gt; 6, 'img_path' =&gt; '', 'img_url' =&gt; '', 'img_width' =&gt; '150', 'img_height' =&gt; '30', 'font_path' =&gt; '', 'expiration' =&gt; 7200);
        foreach ($defaults as $key =&gt; $val) {
            if (!is_array($data)) {
                if (!isset($$key) or $$key == '') {
                    $$key = $val;
                }
            } else {
                $$key = (!isset($data[$key])) ? $val : $data[$key];
            }
        }
        if ($img_path == '' or $img_url == '') {
            return false;
        }
        if (!@is_dir($img_path)) {
            return false;
        }
        if (!is_really_writable($img_path)) {
            return false;
        }
        if (!extension_loaded('gd')) {
            return false;
        }
        
        
        
        
        
        list($usec, $sec) = explode(&quot; &quot;, microtime());
        $now = ((float)$usec + (float)$sec);
        $current_dir = @opendir($img_path);
        while ($filename = @readdir($current_dir)) {
            if ($filename != &quot;.&quot; and $filename != &quot;..&quot; and $filename != &quot;index.html&quot;) {
                $name = str_replace(&quot;.jpg&quot;, &quot;&quot;, $filename);
                if (($name + $expiration) &lt; $now) {
                    @unlink($img_path . $filename);
                }
            }
        }
        @closedir($current_dir);
        
        
        
        $pool = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $str = '';
        for ($i = 0; $i &lt; 6; $i++) {
            $str .= substr($pool, mt_rand(0, strlen($pool) - 1), 1);
        }
        $text = $str;
        
        
        
        $im = @imagecreatefromjpeg('&quot;.base_url().&quot;\images\captch.jpg');
        
        $white = imagecolorallocate($im, 255, 255, 255);
        $grey = imagecolorallocate($im, 128, 128, 128);
        $black = imagecolorallocate($im, 0, 0, 0);
        
        
        
        $font = '&quot;.base_url().&quot;system\fonts\Alan Den.ttf';
        
        
        
        imagettftext($im, 30, 0, 10, 40, $black, $font, $text);
        
        
        
        $now = date('YmdHis');
        $img_name = $now . '.jpg';
        
        imagejpeg($im, $img_path . $img_name);
        $img = &quot;&lt;img src=\&quot;&quot; . base_url() . &quot;$img_url$img_name\&quot;   style=\&quot;border:0;\&quot; alt=\&quot; \&quot; /&gt;&quot;;
        imagedestroy($im);
        return array('word' =&gt; $text, 'time' =&gt; $now, 'image' =&gt; $img);
    }
  ?&gt;
  </code></pre>
  <p>I am using the above code to generate the captcha image in codeigniter. When i use this code got error message as</p>
  <blockquote>
  <p>PHP Error was encountered
  Severity: Warning
  Message: imagecolorallocate():
  supplied argument is not a valid Image resource
  Filename: plugins/captcha_pi.php
  Line Number: 236</p>
  <p>A PHP Error wasencountered
  Severity: Warning
  Message: imagecolorallocate():
  supplied argument is not a valid Image resource
  Filename: plugins/captcha_pi.php
  Line Number: 237</p>
  <p>A PHP Error was encountered Severity:
  Warning Message: imagecolorallocate():
  supplied argument is not a valid Image
  resource Filename:
  plugins/captcha_pi.php Line Number:
  238</p>
  <p>A PHP Error was encountered Severity:
  Warning Message: imagettftext()
  expects parameter 1 to be resource,
  boolean given Filename:
  plugins/captcha_pi.php Line Number:
  251</p>
  <p>A PHP Error was encountered Severity:
  Warning Message: imagejpeg(): supplied
  argument is not a valid Image resource
  Filename: plugins/captcha_pi.php Line
  Number: 259</p>
  <p>A PHP Error was encountered Severity:
  Warning Message: imagedestroy():
  supplied argument is not a valid Image
  resource Filename:
  plugins/captcha_pi.php Line Number:
  263</p>
  </blockquote>
  <p>Please give me the solution for this if anybody knows</p>