Hi,

I am tryiny to write a program which will allow me to upload a image and after which i had uploaded the image, I will be able to calculate the RGB values of the image which i have uploaded.

The code works this way:

//user to upload image file
if ($_FILES)
{
$image_types = Array (
"image/bmp",
"image/jpeg",
"image/pjpeg",
"image/gif",
"image/x-png",
"application/octet-stream");


if (is_uploaded_file ($_FILES))
{
$userfile = addslashes (fread
(fopen ($_FILES["userfile"]["tmp_name"], "r"),
filesize ($_FILES["userfile"]["tmp_name"])));
$file_name = $_FILES["userfile"]["name"];
$file_size = $_FILES["userfile"]["size"];
$file_type = $_FILES["userfile"]["type"];
$tmp_name = $_FILES;


if (in_array (strtolower ($file_type), $image_types))
{
$sql = "INSERT INTO ".$table." "
. "(image_type, image, image_size, image_name, image_date) ";
$sql.= "VALUES (";
$sql.= "'{$file_type}', '{$userfile}', '{$file_size}', '{$file_name}', NOW())";
@mysql_query ($sql, $conn);
Header("Location:".$_SERVER["PHP_SELF"]);
exit();
}
}
}


// Do this process if user has click
// a file name to view or remove
if ($_GET)
{
$iid = $_GET;
$act = $_GET;


switch ($act)
{
case 'view':


$sql = "select * from ".$table." where image_id=" .$iid;
$result = mysql_query($sql,$conn);
if (mysql_num_rows ($result)>0)
{
$row = @mysql_fetch_array($result);
$image_type = $row["image_type"];
$image = $row["image"];
Header("Content-type: $image_type");
print $image ;


}
break;


case 'rem':
$sql = "DELETE FROM ".$table." WHERE image_id=" .$iid;
@mysql_query ($sql, $conn);
Header("Location:".$_SERVER["PHP_SELF"]);
exit();
break;


default:
print "<img src=\"image.php?iid=$iid\">";
break;
}
}


//to convert the size of the image
#
#FUNCTIONS
#
function get_filesize ($dsize)
{
if (strlen($dsize) <= 9 && strlen($dsize) >= 7)
{
$dsize = number_format($dsize / 1048576,1);
return "$dsize MB";
}
elseif (strlen($dsize) >= 10)
{
$dsize = number_format($dsize / 1073741824,1);
return "$dsize GB";
}
else
{
$dsize = number_format($dsize / 1024,1);
return "$dsize KB";
}
}
?>


//html script to display the image and particulars
<html>
<head>
<title>Upload User Image</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Select Image File:
<input type="file" name="userfile" size="40">
<input type="submit" value="submit">
</form>


<?php
$sql = "SELECT * FROM ".$table." ORDER BY image_id";
$result = mysql_query ($sql, $conn);
$i=0;
$str='';
if (mysql_num_rows($result)>0)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$i++;
$str .= $i.". ";
$str .= "<a href=\"userimage.php?act=view&iid=".$row["image_id"]."\">";
$str .= "<img border=\"0\" height=\"90\" width=\"100\" src=\"test_userimage.php?act=view&iid=".$row["image_id"]."\"></a> ";
$str .= "[Name: ".$row["image_name"]."] ";
$str .= "[Date: ".$row["image_date"]."] ";
$str .= "[Size: ". get_filesize($row["image_size"])."] ";
$str .= "[<a href=\"test_userimage.php?act=rem&iid=".$row["image_id"]."\">Remove</a>]<br>";
$str .= "[<a href=\"retrievefromdb.php?act=rem&iid=".$row["image_id"]."\">Retrieve</a>]<br>";
}
print $str;
}


//calculation of RGB values
$image=imageCreateFromJPEG(????);


$x=0;
$y=0;


echo"<table border=\"1\" align=\"center\">";
echo"<tr><th>Position</th>";
echo"<th>Red</th>";
echo"<th>Green</th>";
echo"<th>Blue</th>";
//echo"<th>Alpha</th>";


for($x=0;$x<=240;$x++){
for($y=0;$y<=160;$y++){


$colorindex = imagecolorat($image,$x,$y);
$colorrgb = imagecolorsforindex($image,$colorindex);


echo"<tr><td>";
echo $x;
echo ":";
echo $y;
echo"</td><td>";
echo $colorrgb;
echo"</td><td>";
echo $colorrgb;
echo"</td><td>";
echo $colorrgb;
//echo"</td><td>";
//echo $colorrgb;
echo "</td></tr>";
}
}
echo "</table>";
?>
</body>
</html>

I hope its not too long and draggy, but my question is what is the arguement i should pass in ???? so that the calculation will be based on the image that the user had uploaded.
I have tried a lot of methods but i always get the error:

"Warning: imagecolorat(): supplied argument is not a valid Image resource "

rgds,
tristan

Edited 3 Years Ago by happygeek: fixed formatting

I didnt go through the code.

But $image=imageCreateFromJPEG(????); should take a string filename or the url to the jpg file. So shouldnt you move the uploaded file (in the temporary folder) to a permanent folder and use its path/url. Or just use the filepath to the temp file folder?

:)

This article has been dead for over six months. Start a new discussion instead.