-1

I want to find an uploaded image size using getimagesize and I got this error:

Undefined index: image in C:\wamp\www\cbir\process.php on line 47.

Lines of code that give the error:

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

the whole code:

<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>";
  }

  ?>
Thibault
  • 1,566
  • 15
  • 22
user3054453
  • 55
  • 1
  • 1
  • 6

1 Answers1

0

Have you put enctype="multipart/form-data" in your form tag?

EDIT :

What is supposed to be in $_FILES['image']['tmp_name']?

Shouldn't it be the same than $_FILES['userfile']['tmp_name']?

You don't have any name="image" field in your HTML code.

Since you created a $tmpName variable, you should reuse it instead of recalling $_FILES['image']['tmp_name'] each time.

Thibault
  • 1,566
  • 15
  • 22
  • Yes I did so . Would you like to see the whole code ? – user3054453 Dec 16 '13 at 10:26
  • Of course yes please paste it in your question. – Thibault Dec 16 '13 at 10:26
  • I edited my answer based on your HTML Code. – Thibault Dec 16 '13 at 10:41
  • I tried every thing as a parameter to getimagesize. but still have an error so the las parameter i used was $_FILES['image']['tmp_name'] – user3054453 Dec 16 '13 at 10:44
  • And you just try random keywords until it works? – Thibault Dec 16 '13 at 10:47
  • No I tried solutions from the internet. Thank I replaced $_FILES['image']['tmp_name'] with $tmpName and it worked – user3054453 Dec 16 '13 at 10:49
  • I have a new problem the $red_count, $blue_count and $green_count are all have the same value which is 256 – user3054453 Dec 16 '13 at 10:51
  • `$red_count = count($reds)` count the size of the array. Since you `array_filled` `$reds` array with 256 `0`, it's size is indeed 256. What would you want in `$red_count`? – Thibault Dec 16 '13 at 10:53
  • Do you have an idea about how to count array elements? I cant replace array_filled with array() because it keep gives me errors – user3054453 Dec 16 '13 at 10:57
  • Your histogram algorithm seems ok. But what are you trying to count as `$red_counts`? What do you want to insert into your database? – Thibault Dec 16 '13 at 11:01
  • I want to retrieve similar images. The user will upload an image and the count of r, g and b will be extracted to compare it with stored r, g and b for each image in database. the images with the smallest diference will be displayed. – user3054453 Dec 16 '13 at 11:06
  • Well, maybe you don't want to `count($reds)` then, but to `array_sum($reds)`. It will be more relevant to compare red, blue and green sum values. – Thibault Dec 16 '13 at 12:28