Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: longblob wyciąganie obrazka i resize
Forum PHP.pl > Forum > Przedszkole
vahooz
Witam.

Posiadam bazę z obrazkami (wszystkie o rozdzielczości 800x600), teraz chciałbym je wyświetlić jako miniaturki. Jak dokonać takiej konwersji? Plik wyświetlający obrazek:
  1. <?php
  2. if(!isset($Id)) die("Need 'Id' parameter");
  3. else $Id=addslashes($Id);
  4. include("common.php");
  5. //set up SQL connection
  6. $link = mysql_connect ($server, $user, $password);
  7. if (! $link) die ("Couldn't connect to mySQL server");
  8. if (!mysql_select_db ($db, $link) ) die ("Coldn't open $db: ".mysql_error() );
  9. $query = "SELECT filetype, bin_data FROM tbl_Files WHERE id_files='$Id';";
  10. $result = mysql_query($query);
  11. $query_data = mysql_fetch_array($result);
  12. $bin_data = $query_data[bin_data];
  13. $filetype = $query_data[filetype];
  14. Header("Content-type: $filetype");
  15. echo $bin_data;
  16. ?>


Pozdrawiam.
vahooz
Tej funkcji nie mogę zastosować do obrazka władowanego do bazy, przecież nie mam uchwytu do obrazka, tylko jego binarną zawartość. Proszę poprawcie mnie jak jestem w błędzie.

Pozdrawiam.
SongoQ
Mozesz odczytac zrodlo z bazy i stworzyc obiekt. Jest w manualu.
vahooz
Czy mógłbyś mi pomóc bo naprawdę nie wiem jak to zrobić, szukam, czytam i się zastanawiam:
  1. <?php
  2.  
  3. $Id = "3";
  4. if(!isset($Id)) die("Need 'Id' parameter");
  5. else $Id=addslashes($Id);
  6. include("common.php");
  7.  
  8.  
  9. //set up SQL connection
  10. $link = mysql_connect ($server, $user, $password);
  11. if (! $link) die ("Couldn't connect to mySQL server");
  12. if (!mysql_select_db ($db, $link) ) die ("Coldn't open $db: ".mysql_error() );
  13.  
  14.  
  15. $query = "SELECT filetype, bin_data FROM tbl_Files WHERE id_files='$Id';";
  16. //$query = 'SELECT filetype, bin_data FROM tbl_Files WHERE id_files="1"';
  17. $result = mysql_query($query);
  18.  
  19.  
  20. $query_data = mysql_fetch_array($result);
  21. $bin_data = $query_data[bin_data];
  22. $filetype = $query_data[filetype];
  23.  
  24. Header("Content-type: $filetype");
  25.  
  26.  
  27. $filename = $bin_data;
  28.  
  29. $percent = 0.5;
  30.  
  31. header('Content-type: image/jpeg');
  32.  
  33. list($width, $height) = getimagesize($filename);
  34. echo "x".$new_width = $width * $percent;
  35. echo "y".$new_height = $height * $percent;
  36.  
  37. $image_p = imagecreatetruecolor($new_width, $new_height);
  38. $image = imagecreatefromjpeg($filename);
  39. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  40.  
  41.  
  42. echo imagejpeg($image_p, null, 100);
  43.  
  44. ?>


Pozdrawiam.
SongoQ
  1. <?php
  2. $rPhoto = imagecreatefromstring('zrodlo obrazka pobrane z bazy');
  3. ?>


W $rPhoto masz obiekt obrazka i dalej robisz tak jak Ci podawalem na samym poczatku.
vahooz
  1. <?php
  2. $Id = "3";
  3. if(!isset($Id)) die("Need 'Id' parameter");
  4. else $Id=addslashes($Id);
  5. include("common.php");
  6. $link = mysql_connect ($server, $user, $password);
  7. if (! $link) die ("Couldn't connect to mySQL server");
  8. if (!mysql_select_db ($db, $link) ) die ("Coldn't open $db: ".mysql_error() );
  9. $query = "SELECT filetype, bin_data FROM tbl_Files WHERE id_files='$Id';";
  10. $result = mysql_query($query);
  11. $query_data = mysql_fetch_array($result);
  12. $bin_data = $query_data[bin_data];
  13. $filetype = $query_data[filetype];
  14. Header("Content-type: $filetype");
  15. //echo $bin_data; //TU JEST OK WYSWIETLA OBRAZEM
  16. $filename = imagecreatefromstring($bin_data);
  17. $percent = 0.5;
  18. list($width, $height) = getimagesize($filename);
  19. $new_width = $width * $percent;
  20. $new_height = $height * $percent;
  21. $image_p = imagecreatetruecolor($new_width, $new_height);
  22. $image = imagecreatefromjpeg($filename);
  23. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  24. imagejpeg($image_p, null, 100);
  25. imagedestroy($image_p);
  26. ?>


Niestety nic nie wyświetla, podstawiając pod $filename nazwę pliku na serwerze to działa. Jednak tworząc obraz za pomocą imagecreatefromstring($bin_data) nie działa. Z manuala dorwałem gotowca, który tez nie działa!!
  1. <?php
  2. require("dbconfig.inc");
  3.  
  4. $id = $_GET['id'];
  5.  
  6. if($id) {
  7.  
  8.  $link = @mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
  9.  @mysql_select_db($dbname, $link);
  10.  
  11.  $query = "select filetype, image from pictures where id = $id";
  12.  $result = @mysql_query($query);
  13.  
  14.  $data = @mysql_result($result,0,"image");
  15.  $type = @mysql_result($result,0,"filetype");
  16.  
  17.  Header( "Content-type: $type");  
  18.  
  19.  $size = 150; // new image width
  20.  $src = imagecreatefromstring($data);
  21.  $width = imagesx($src);
  22.  $height = imagesy($src);
  23.  $aspect_ratio = $height/$width;
  24.  
  25.  if ($width <= $size) {
  26.  $new_w = $width;
  27.  $new_h = $height;
  28.  } else {
  29.  $new_w = $size;
  30.  $new_h = abs($new_w * $aspect_ratio);
  31.  }
  32.  
  33.  $img = imagecreatetruecolor($new_w,$new_h);
  34.  imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$width,$height);
  35.  
  36.  // determine image type and send it to the client  
  37.  if ($type == "image/pjpeg") {  
  38.  imagejpeg($img);
  39.  } else if ($type == "image/x-png") {
  40.  imagepng($img);
  41.  } else if ($type == "image/gif") {
  42.  imagegif($img);
  43.  }
  44.  imagedestroy($img);
  45.  mysql_close($link);
  46. };
  47. ?>

Czy to możliwe, że mam jakieś ograniczenia po stronie serwera?
SongoQ
Calkiem mozliwe, jakies bledy wyrzuca? Wlacz raportowanie bledow i pokaz co CI pokazalo.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.