Php – supplied argument is not a valid Image resource in php codeigniter

codeigniterPHP

<?php
  if (!defined('BASEPATH'))
      exit('No direct script access allowed');
  function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
  {
      $defaults = array('word' => '', 'word_length' => 6, 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
      foreach ($defaults as $key => $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(" ", microtime());
      $now = ((float)$usec + (float)$sec);
      $current_dir = @opendir($img_path);
      while ($filename = @readdir($current_dir)) {
          if ($filename != "." and $filename != ".." and $filename != "index.html") {
              $name = str_replace(".jpg", "", $filename);
              if (($name + $expiration) < $now) {
                  @unlink($img_path . $filename);
              }
          }
      }
      @closedir($current_dir);
      
      
      
      $pool = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
      $str = '';
      for ($i = 0; $i < 6; $i++) {
          $str .= substr($pool, mt_rand(0, strlen($pool) - 1), 1);
      }
      $text = $str;
      
      
      
      $im = @imagecreatefromjpeg('".base_url()."\images\captch.jpg');
      
      $white = imagecolorallocate($im, 255, 255, 255);
      $grey = imagecolorallocate($im, 128, 128, 128);
      $black = imagecolorallocate($im, 0, 0, 0);
      
      
      
      $font = '".base_url()."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 = "<img src=\"" . base_url() . "$img_url$img_name\"   style=\"border:0;\" alt=\" \" />";
      imagedestroy($im);
      return array('word' => $text, 'time' => $now, 'image' => $img);
  }
?>

I am using the above code to generate the captcha image in codeigniter. When i use this code got error message as

PHP Error was encountered
Severity: Warning
Message: imagecolorallocate():
supplied argument is not a valid Image resource
Filename: plugins/captcha_pi.php
Line Number: 236

A PHP Error wasencountered
Severity: Warning
Message: imagecolorallocate():
supplied argument is not a valid Image resource
Filename: plugins/captcha_pi.php
Line Number: 237

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

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

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

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

Please give me the solution for this if anybody knows

Best Answer

This line is your problem:

$im = @imagecreatefromjpeg('".base_url()."\images\captch.jpg'); 

I recommend changing that path and trying again...

$im = @imagecreatefromjpeg("images\\captch.jpg"); 
Related Topic