By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

Problem with image upoloading.....

P: 49
Hi..

i am having a strange problem.......the problem is i am trying to upload a file and to store it in db....when the file got stored in db...it got converted into black and white

image and the stored in db with the same name("b"appended in the begining of the filename).

now the problem is that...the following script only converts three images which i was using through out the testing phase......now it uploads only those three images ..it

doesn't converts anyother image...


can u plz tell me what kind of problem is this


<?php include("upload.php"); ?>
<?php require_once("connection/connection.php"); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>image uploading</title>
</head>

<body>

<?php
$ext= array ('gif','jpg','jpeg','png');
$upload = new upload('fleImage', 'image_store/', '777', $ext);

if ( $upload->message =="SUCCESS_FILE_SAVED_SUCCESSFULLY" )
{
$uploadfilename=$upload->filename;

/*-------------------------file conversion code starts here-------------------------------------*/

//getting image name and storing in a variable
$ori_img="image_store/".$uploadfilename;
$ori_img = "$uploadfilename";
$uploadfilenamebw="b_".$ori_img;
$destinationfile="image_store/b_".$ori_img;

//storing original file name and converted file in a variable
$originalFileName = "$ori_img";
$destinationFileName = "$destinationfile";

// checking file extension
$fullPath = explode(".",$originalFileName);
$lastIndex = sizeof($fullPath) - 1;
$extension = $fullPath[$lastIndex];
if (preg_match("/jpg|jpeg|JPG|JPEG/", $extension))
{
$sourceImage = imagecreatefromjpeg($originalFileName);


}
elseif (preg_match("/png|PNG/", $extension))
{
$sourceImage = imagecreatefrompng($originalFileName);
}

// get image dimensions
$img_width = imageSX($sourceImage);
$img_height = imageSY($sourceImage);

// convert to grayscale
for ($y = 0; $y <$img_height; $y++)
{
for ($x = 0; $x <$img_width; $x++)
{
$rgb = imagecolorat($sourceImage, $x, $y);
$red = ($rgb >> 16) & 0xFF;
$green = ($rgb >> 8) & 0xFF;
$blue = $rgb & 0xFF;

$gray = round(.299*$red + .587*$green + .114*$blue);

// shift gray level to the left
$grayR = $gray << 16; // R: red
$grayG = $gray << 8; // G: green
$grayB = $gray; // B: blue

// OR operation to compute gray value
$grayColor = $grayR | $grayG | $grayB;

// set the pixel color
imagesetpixel ($sourceImage, $x, $y, $grayColor);
imagecolorallocate ($sourceImage, $gray, $gray, $gray);
}
}

// copy pixel values to new file buffer
$destinationImage = ImageCreateTrueColor($img_width, $img_height);
imagecopy($destinationImage, $sourceImage, 0, 0, 0, 0, $img_width, $img_height);


// create file on disk
imagejpeg($destinationImage, $destinationFileName);
imagepng($destinationImage, $destinationFileName);

// destroy temp image buffers
imagedestroy($destinationImage);
imagedestroy($sourceImage);
/*-----------------------------------conversion ends here----------------------------------------*/


/*------------------------------inserting in DB----------------------------------------------*/
$query = "INSERT INTO wedding_image (img_name_orig,img_name_bw) VALUES ('$uploadfilename','$uploadfilenamebw')";
mysql_query($query);
/*--------------------------------------------------------------------------------------------*/


}
?>


<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="file" name="fleImage" id="fleImage" />
<br /><br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>







PLZ help me


Regards
Muddasir
Nov 19 '07 #1
Share this Question
Share on Google+
4 Replies


P: 49
it only converts "water lilies.jpg" windows default sample picture and one flower.png which i download ...it doesn't convert any other image file...i dont know why????
Nov 19 '07 #2

MarkoKlacar
Expert 100+
P: 296
it only converts "water lilies.jpg" windows default sample picture and one flower.png which i download ...it doesn't convert any other image file...i dont know why????
Hello my friend,

could you re-post using the code tags, looking at the code like this and reffering to the problem is really really difficult.

Cheers
Nov 19 '07 #3

P: 49
Thanks for reply Sir..
oh i dont know why i keep on forgetting this

here is the code

Expand|Select|Wrap|Line Numbers
  1. <?php include("upload.php"); ?>
  2. <?php require_once("connection/connection.php"); ?>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  6. <title>image uploading</title>
  7. </head>
  8.  
  9. <body>
  10.  
  11. <?php
  12.         $ext= array ('gif','jpg','jpeg','png');
  13.         $upload = new upload('fleImage', 'image_store/', '777', $ext);
  14.  
  15.         if ( $upload->message =="SUCCESS_FILE_SAVED_SUCCESSFULLY" )
  16.         {
  17.             $uploadfilename=$upload->filename;    
  18.  
  19.                 /*-------------------------file conversion code starts here-------------------------------------*/
  20.  
  21.                 //getting image name and storing in a variable
  22.                 $ori_img="image_store/".$uploadfilename;
  23.                 $ori_img = "$uploadfilename";
  24.                 $uploadfilenamebw="b_".$ori_img;
  25.                 $destinationfile="image_store/b_".$ori_img;
  26.  
  27.                 //storing original file name and converted file in a variable
  28.                 $originalFileName    = "$ori_img";
  29.                 $destinationFileName = "$destinationfile";
  30.  
  31.                 // checking file extension
  32.                 $fullPath = explode(".",$originalFileName);
  33.                 $lastIndex = sizeof($fullPath) - 1;
  34.                 $extension = $fullPath[$lastIndex];
  35.                 if (preg_match("/jpg|jpeg|JPG|JPEG/", $extension))
  36.                 {
  37.                     $sourceImage = imagecreatefromjpeg($originalFileName);
  38.  
  39.  
  40.                 }
  41.                 elseif (preg_match("/png|PNG/", $extension))
  42.                 {
  43.                     $sourceImage = imagecreatefrompng($originalFileName);
  44.                 }
  45.  
  46.                 // get image dimensions
  47.                 $img_width  = imageSX($sourceImage);
  48.                 $img_height = imageSY($sourceImage);
  49.  
  50.                 // convert to grayscale
  51.                 for ($y = 0; $y <$img_height; $y++) 
  52.                 { 
  53.                     for ($x = 0; $x <$img_width; $x++) 
  54.                     { 
  55.                         $rgb = imagecolorat($sourceImage, $x, $y);
  56.                         $red   = ($rgb >> 16) & 0xFF;
  57.                         $green = ($rgb >> 8)  & 0xFF;
  58.                         $blue  = $rgb & 0xFF;
  59.  
  60.                         $gray = round(.299*$red + .587*$green + .114*$blue);
  61.  
  62.                         // shift gray level to the left
  63.                         $grayR = $gray << 16;   // R: red
  64.                         $grayG = $gray << 8;    // G: green
  65.                         $grayB = $gray;         // B: blue
  66.  
  67.                         // OR operation to compute gray value
  68.                         $grayColor = $grayR | $grayG | $grayB;
  69.  
  70.                         // set the pixel color
  71.                         imagesetpixel ($sourceImage, $x, $y, $grayColor);
  72.                         imagecolorallocate ($sourceImage, $gray, $gray, $gray);
  73.                     }
  74.                 }
  75.  
  76.                 // copy pixel values to new file buffer
  77.                 $destinationImage = ImageCreateTrueColor($img_width, $img_height);
  78.                 imagecopy($destinationImage, $sourceImage, 0, 0, 0, 0, $img_width, $img_height);
  79.  
  80.  
  81.                 // create file on disk
  82.                 imagejpeg($destinationImage, $destinationFileName);
  83.                 imagepng($destinationImage, $destinationFileName);
  84.  
  85.                 // destroy temp image buffers
  86.                 imagedestroy($destinationImage);    
  87.                 imagedestroy($sourceImage); 
  88.             /*-----------------------------------conversion ends here----------------------------------------*/
  89.  
  90.  
  91.             /*------------------------------inserting in DB----------------------------------------------*/
  92.             $query = "INSERT INTO wedding_image (img_name_orig,img_name_bw) VALUES ('$uploadfilename','$uploadfilenamebw')";
  93.             mysql_query($query);
  94.             /*--------------------------------------------------------------------------------------------*/
  95.  
  96.  
  97.         }
  98. ?>
  99.  
  100.  
  101. <form name="myform" method="post" action="" enctype="multipart/form-data">
  102. <input type="file" name="fleImage" id="fleImage" />
  103. <br /><br />
  104. <input type="submit" name="submit" value="Upload" />
  105. </form>
  106. </body>
  107. </html>
Nov 19 '07 #4

MarkoKlacar
Expert 100+
P: 296
Hi Muddasir,

did you know you can insert a image directly in the database, I'm not quite sure you're doing that here. Just put it in as any other value.

I assume you are using MySQL, are you using the BLOB to store the image?

Now, what are the conversions you are talking about?

Cheers
Nov 20 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.