Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

image from Db problems

Status
Not open for further replies.

bastienk

Programmer
Joined
Mar 3, 2004
Messages
326
Location
CA
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:
$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


Bastien

Cat, the other other white meat
 
I think you operate on the false assumption that you can create images from a variable. The PHP documentation says:

imagecreatefromjpeg -- Create a new image from file or URL
Description
resource imagecreatefromjpeg ( string filename)

imagecreatefromjpeg() returns an image identifier representing the image obtained from the given filename.

imagecreatefromjpeg() returns an empty string on failure.


This is what I belive that's going on.
 
According to the PHP online manual page on imagecreatefromjpeg(), that function expects a filename as input.


If your database contains the filesystem filename of the image, why are you trying to write the data to the filesystem again?

If your database contains the image data, you should be using imagecreatefromstring().

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleipnir214

I need to save the thumbnail to the db again. ASP (which is used for the application) has no image manipulation capabalities and we don't want to send a 5meg image down to the client if we don't have to (some of our clients have certain bandwidth restrictions).

So my task is to get the image out, shrink it, and stuff it into the db.

imagecreatefromsring() looks promising. The errors I get now is:

Code:
Notice: Undefined variable: img in E:\Inetpub\[URL unfurl="true"]wwwroot\dev210\bastien210\includes\thumbnailer.php[/URL] on line 80

Warning: imagecreatefromstring(): Passed data is not in 'WBMP' format in E:\Inetpub\[URL unfurl="true"]wwwroot\apis\dev210\bastien210\includes\thumbnailer.php[/URL] on line 80

Warning: imagecreatefromstring(): Couldn't create GD Image Stream out of Data in E:\Inetpub\[URL unfurl="true"]wwwroot\dev210\bastien210\includes\thumbnailer.php[/URL] on line 80
Success, wrote (img) to file (E:\html\scripts\BigKitty.jpg)


Bastien

Cat, the other other white meat
 
Jeez what messy code!
I think you need to do a little tidying up of your code and try and get some consistancy to your references:
ie $my_image and $temp_image.
you need to decide what you want for these refs.
you appear not to have opened the file for writing. you appear to have commented out a lot of the code so i'm not surprised this does not work. what does line 80 in thumbnailer.php actually say?
 
bastienk:
I would guess that if you were to solve the code problem indicated by the first error message, the other two will go away.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
solved guys, thanks for all the help and pointers

Code:
while ($row=mysql_fetch_array($results)){
    //echo "img retreived";
    $db_img = $row['Evidence'];
    //start the image manipulation routine
    $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


Bastien

Cat, the other other white meat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top