Avatar billede mhv Praktikant
16. februar 2004 - 12:04 Der er 1 kommentar

Visning af billeder fra mysql database

Jeg får godtnok hentet data i databasen, men hvordan får jeg det vist som et billede?

min kode:

<?php
    $SQLSTR = "SELECT * FROM Test";
    $result = mysql_db_query($db, $SQLSTR, $connection);
    while($row = mysql_fetch_array($result))
        {
?>
        <img src="<?php echo $row["Billede"]; ?>">
<?php
        }
?>
Avatar billede mhv Praktikant
16. februar 2004 - 13:35 #1
fandt selv svaret i manuelos........

Some say that BLOBS are unefficient or even stupid to use, but the truth is that dinamic content works much better while having the capability of reading information "attached" in some way to the image.

imagecreatefromstring gives you the posibility to create great thumbnails, see the examples given in the comments above.

If you must create an image in the middle of a sea of html, it's always possible using the output buffering options (see function "ob_start();"), so you don´t have to deal with annoying "headers already sent..." problems. Some still say that it is not possible, well, they have to read a little.

If you still cannot get rid of "ÿØÿà", try using your script for creating thumbnails in a separate file, just call it from your main page, here´s an example:

MY MAIN PAGE CALLS THE THUMBNAILING SCRIPT IN ORDER TO OUTPUT AN IMAGE TO THE BROWSER:
<?php
echo "<img src=\"view.php?se={$row_Recordset1['id']}\";></img>";
?>

MY THUMBNAILING SCRIPT WORKS IN A SEPARATE FILE "view.php" wich resizes and gives output:

//validate connection

<?php require_once('Connections/promo.php'); ?>

//get "se" variable passed by my MAIN PAGE

<?php
$colname_Recordset1 = "1";
if (isset($HTTP_GET_VARS['se'])) {
  $colname_Recordset1 = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['se'] : addslashes($HTTP_GET_VARS['se']);
}
mysql_select_db($database_promo, $promo);

//select info from DBASE, creatign logic to get the rigth info

$query_Recordset1 = sprintf("SELECT * FROM files WHERE id = %s", $colname_Recordset1);
$Recordset1 = mysql_query($query_Recordset1, $promo) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

//Tell browser kind of image to expect

  header('Content-Type: image/jpeg');
  $data = $row_Recordset1["fileContents"];

//set new height

  $size = 115;  // new image height script adapted by Raul Rodriguez Haro for mueblesexcell.com.mx
  $src = imagecreatefromstring ($data);
  $width = imagesx($src);
  $height = imagesy($src);
  $aspect_ratio = $height/$width; 

//start resizing
 
  if ($height <= $size) {
    $new_w = $width;
    $new_h = $height;
  } else {
    $new_h = $size;
    $new_w = abs($new_h / $aspect_ratio);
  }

  $img = imagecreatetruecolor ($new_w,$new_h);

//output image

imagecopyresampled ($img,$src,0,0,0,0,$new_w,$new_h,$width,$height);

// determine image type and send it to the brow

imagejpeg($img, '', 90);
imagedestroy($img);
mysql_free_result($Recordset1);
?>

READY!!!!!
You have a fully functional thumbnails gallery.
not given
03-Aug-2003 08:14
A note to the previous question (if you still don't know it :))...

GIF's are 256 colors (or 8 bit), and the resample function needs true color I guess... that's why it works with JPG's and not with GIF's.

Next thing... you take a string, write it to file, open the file (imagecreatefromgif), and delete the file again.

if you do imagecreatefromstring($string) you can skip the temporary file part.
shiehj at yahoo dot com
12-Apr-2003 03:44
Here is the code I did to create a thumbnail image from the database blob field. The trick is to use "imagecreatefromstring()" to create an image file.

Jack Shieh

<?php
require("dbconfig.inc");

$id = $_GET['id'];

if($id) {

  $link = @mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
  @mysql_select_db($dbname, $link);

  $query = "select filetype, image from pictures where id = $id";
  $result = @mysql_query($query);

  $data = @mysql_result($result,0,"image");
  $type = @mysql_result($result,0,"filetype");
 
  Header( "Content-type: $type");   
 
  $size = 150;  // new image width
  $src = imagecreatefromstring($data);
  $width = imagesx($src);
  $height = imagesy($src);
  $aspect_ratio = $height/$width;

  if ($width <= $size) {
    $new_w = $width;
    $new_h = $height;
  } else {
    $new_w = $size;
    $new_h = abs($new_w * $aspect_ratio);
  }

  $img = imagecreatetruecolor($new_w,$new_h);
  imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$width,$height);

  // determine image type and send it to the client   
  if ($type == "image/pjpeg") {   
    imagejpeg($img);
  } else if ($type == "image/x-png") {
    imagepng($img);
  } else if ($type == "image/gif") {
    imagegif($img);
  }
  imagedestroy($img);
  mysql_close($link);
};
?>
adrian_schmidt at yahoo dot com
05-Mar-2003 10:04
I'm trying to get the imagecreatefromstring to work with GIFs. Of course, it won't.
I've read the tips but can't get them to work either.
The following is what I tried, based on above tips:

---

  header('Content-Type: image/gif');
  header('Content-Disposition: inline; filename=file.gif');

  $temp = tmpfile();
  fwrite($temp, $line['image']);
  $src_img = imagecreatefromgif($temp);
  fclose($temp); // this removes the file
  $dst_img = imagecreatetruecolor(100, 100);
  imagecopyresampled($dst_img, $src_img, 0,0,0,0, 100,100, imagesx($src_img), imagesy($src_img));
 
  imagegif($dst_img);

---

where $line['image'] is the gif as taken from my MySQL database...

If anyone that has been able to make something like this work could give me a working piece of code I'd be really greatful!

I would be great if the tempfile could be excluded too...

Below is a working piece of code for jpeg:

---

  header('Content-Type: image/jpeg');
  header('Content-Disposition: inline; filename=file.jpg');
 
  $src_img = imagecreatefromstring($line['image']);
  $dst_img = imagecreatetruecolor(100, 100);
  imagecopyresampled($dst_img, $src_img, 0,0,0,0, 100,100, imagesx($src_img), imagesy($src_img));
 
  imagejpeg($dst_img);
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester