尝试查找图像大小时出现未定义的索引错误

时间:2013-12-16 10:19:27

标签: php image

我想使用getimagesize找到上传的图片尺寸,我收到了此错误:

  

第47行image中的未定义索引:C:\wamp\www\cbir\process.php

给出错误的代码行:

$info = getimagesize($_FILES['image']['tmp_name']);
$img = imagecreatefromjpeg($_FILES['image']['tmp_name']);

整个代码:

<form action="process.php" method="post" enctype="multipart/form-data">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<!-- Name of input element determines name in $_FILES array -->
<input name="userfile" type="file" id="userfile">
<input name="upload" type="submit" id="upload" value="Upload binary file">
</form>
<?php

$dbdatabase = "cbir";
$config_carname = "Content Based Image Retrieval";
$config_author = "Copyright &copy; 2013 shahd & arwa . All Rights Reserved.<br>      Designed by shahd & arwa";
$config_basedir = "http://localhost/cbir/";
$SCRIPT_NAME="process.php"; 

if(isset($_POST['upload']))
{
  //get file information --step1
  $fileName = $_FILES['userfile']['name'];
  $tmpName = $_FILES['userfile']['tmp_name'];
  $fileSize = $_FILES['userfile']['size'];

  //get file content -- step1
  $fp = fopen($tmpName, 'r');
  $content = fread($fp, $fileSize);
  $content = addslashes($content);
  fclose($fp);

  $link = mysql_connect("localhost", "root", "root");
  mysql_select_db($dbdatabase, $link)or die("<b>Unable to specified database</b>");


  $reds = array_fill(0, 256, 0);
  $blues = array_fill(0, 256, 0);
  $greens = array_fill(0, 256, 0);

  $info = getimagesize($_FILES['image']['tmp_name']);

  $width = $info[0];
  $height = $info[1];
  $totalpixels = $width * $height;

  $img = imagecreatefromjpeg($_FILES['image']['tmp_name']);
  if ($img) {

    for ($i = 0; $i < $height; $i++) {
      for ($j = 0; $j < $width; $j++) {
        $rgb = imagecolorat($img, $j, $i);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;

        if (!isset($reds[$r])) {
          $reds[$r] = 0;
        }
        if (!isset($greens[$r])) {
          $greens[$r] = 0;
        }
        if (!isset($blues[$r])) {
          $blues[$r] = 0;
        }

        // Add counts to our histogram arrays for each color.

        $reds[$r]++;
        $greens[$g]++;
        $blues[$b]++;
      }
    }

    $red_count=count($reds);
    $blue_count=count($blues);
    $green_count=count($greens);        }

    //insert into database --step3 
    $query = "INSERT INTO cbir (image, red_count,blue_count,green_count) VALUES    ('$content','$red_count','$blue_count','$green_count')";
    mysql_query($query) or die('Error, query failed');

    //return insert information to client
    $id= mysql_insert_id();
    echo "File<b> $fileName</b> uploaded as id= $id<br>";
  }

  ?>

1 个答案:

答案 0 :(得分:0)

您是否已将enctype="multipart/form-data"放入form代码?

编辑

$_FILES['image']['tmp_name']应该是什么?

不应该与$_FILES['userfile']['tmp_name']相同吗?

您的HTML代码中没有任何name="image"字段。

由于您创建了$tmpName变量,因此您应该重复使用它,而不是每次都调用$_FILES['image']['tmp_name']