Page 1 of 1

image from db problem

Posted: Mon Apr 12, 2004 9:26 am
by lostboy
Hi All,

Kinda stuck here. I am trying to pull an image out of the db and resize it, then stick it back into the db. (Its a bastard solution to an ASP app problem that I have to deal with. This is the only way to do it for now).

Anyway, the code below is to take the image out, and does work if I print the image to the screen. But I am having problems saving this to a file so that I can resize it. The code does indicate success, but the file produced is 0Kb in size. I can't figure out what I am doing wrong

I my approach valid, or is there something else I should do?






Code: Select all

$sql = "select evidence from evidence where filename = '$image_name' and evidence_number = 'EV8000000'"; 

$results = mysql_query($sql) or die ("Can't connect to DB because ".mysql_error()); 
//check for results 
if (($results)&&(mysql_num_rows($results)==1)){ 
   
  //we have the image 
  while ($row=mysql_fetch_array($results)){ 
    $img =  imagecreatefromjpeg($row['evidence']); 
  }//end while 

}//end if 

//print $img; 
//save the photo temporarily to the disk - delete when finished 
/*$temp_image = fopen($my_image,"wb"); 
if (is_writable($my_image)) { 
  fwrite ($temp_image, $img); 
}else{ 
  echo "Cannot write to file ($filename)"; 
} 
fclose($temp_image); */ 

// Let's make sure the file exists and is writable first. 
if (is_writable($my_image)) { 
 
   if (!$handle = fopen($my_image, 'wb')) { 
         echo "Cannot open file ($my_image)"; 
         exit; 
   } 
   // Write $somecontent to our opened file. 
   if (fwrite($handle, $img) === FALSE) { 
       echo "Cannot write to file ($my_image)"; 
       exit; 
   } 
   echo "Success, wrote (img) to file ($my_image)"; 
   fclose($handle); 
} else { 
   echo "The file $my_image is not writable"; 
}



TIA

Posted: Mon Apr 12, 2004 3:03 pm
by lostboy
solved

Code: Select all

if (($results)&&(mysql_num_rows($results)==1)){
  
  //we have the image 
  while ($row=mysql_fetch_array($results)){
    //echo "img retreived";
    $db_img = $row['Evidence'];
    //header("Content-type: image/jpeg");
    
    $src = imagecreatefromstring($db_img);
    $size = 110;  // new image width
    $width = imagesx($src);
    $height = imagesy($src);
    $aspect_ratio = $height/$width;
    //calc the reduction in size
    if ($width <= $size) {
     $new_w = $width;
     $new_h = $height;
    }else {
     $new_w = $size;
     $new_h = abs($new_w * $aspect_ratio);
    }
    //create the thumbnail image
    $img = imagecreatetruecolor($new_w,$new_h); 
    imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$width,$height);
    //save the image		
		imagejpeg($img, $my_thumb);
	  ImageDestroy($img);
    ImageDestroy($src);
  
  }//end while
}//end if