Dynamic Thumbnails from BLOB Fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
_undead_zzz
Forum Newbie
Posts: 3
Joined: Fri Mar 17, 2006 5:08 pm

Dynamic Thumbnails from BLOB Fields

Post by _undead_zzz »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi !

Hope Someone can help me here.

What I'm trying to do is obtain data from an Image BLOB field in MySQL
and create a thumbnail from that info using PHP GD Toolkit.

Ok, so here's how I'm doin it :

I have the class maneja_img that goes something like this :

Code: Select all

<?php
include "xml_feed.php" //Class that parses data from an XML File
?>
<?php

       $xml_feed = &New xml_feed;

       $datosxml = $xml_feed->leeyobt(); //Parse Now

       $thumbnailmi = $datosxml['thumbnailmi']; //Value is 100
       $thumbnailme = $datosxml['thumbnailme']; //Value is 400
       define("ALTURA", $thumbnailmi);
       define("ALTURA2", $thumbnailme);
       define("NAMETHUMB", "/tmp/thumbtemp"); //Unused Here
       define("NAMETHUMB2", "/tmp/thumbtemp2"); //Unused Here
?>
<?php
class maneja_img{

      //this array defines Image mime Types
       var $mimetypes = array("image/jpeg", "image/pjpeg", "image/gif", "image/png");

      // Here we go !
       function thumdbd($idfoto){

             //SQL Query
               $sql = "SELECT foto, mime FROM fotografias WHERE idfoto = $idfoto";
               $link = mysql_connect("localhost:8889", "root", "root") or
die(mysql_error($link));;
               mysql_select_db("filestorage", $link) or die(mysql_error($link));
               $conn = mysql_query($sql, $link) or die(mysql_error($link));
               $datos = mysql_fetch_array($conn);
               $imagen = $datos['foto']; //Image BLOB Data
               $mime = $datos['mime']; //Image mime type


               //First I write BLOB data into a temporary file in /tmp dir

             $t_img_name = tempnam("/tmp", "timg");
               $t_img = fopen($t_img_name, "w");


               //write data into file
               fwrite($t_img, $imagen);
               fclose($t_img);



               //check for mime type
               switch($mime) {
                       case $mimetypes[0]:
                       case $mimetypes[1]:
                       $img = imagecreatefromjpeg($t_img_name);
                       break;
                       case $mimetypes[2]:
                       $img = imagecreatefromgif($t_img_name);
                       break;
                       case $mimetypes[3]:
                       $img = imagecreatefrompng($t_img_name);
                       break;
               }

                       //thumb attributes
                      $datos = getimagesize($t_img_name);
                       $ratio = ($datos[1]/ALTURA);
                       $ancho = round($datos[0]/$ratio);
                       $thumb = imagecreatetruecolor($ancho, ALTURA);
                       imagecopyresized($thumb, $img, 0, 0, 0, 0, $ancho, ALTURA,
$datos[0], $datos[1]);

                       switch($mime) {
                               case $mimetypes[0]:
                               case $mimetypes[1]:
                               imagejpeg($thumb, $t_img_name);
                               break;
                               case $mimetypes[2]:
                               imagegif($thumb, $t_img_name);
                               break;
                               case $mimetypes[3]:
                               imagepng($thumb, $t_img_name);
                                       break;
                       }


                       $fp = fopen($t_img_name, "rb");
                       $tthumb = fread($fp, filesize($t_img_name));
                       $tthumb = addslashes($tthumb);
                       fclose($fp);

                       unlink($t_img_name);

                   //Specify a header and print the new Image
                       header("Content-type: $mime");
                       echo $tthumb;
       }
}
Then I call this class and it's method from within another PHP that
goes like this

Code: Select all

<html>
<body>
<?php
       include "maneja_img.php"
?>
<?php
       $maneja_img = &New maneja_img;
       $id = 37; //index of the image in the DataBase
       $maneja_img->thumdbd($id);
?>
</body>
</html>
What I get in the end is something similar to echoing the raw BLOB data

What I'm I doing wrong ??

Thanks in advance !!


Jose


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

perhaps you need to tell the browser what to do with the string... a content-type header maybe?

(i see you have one, is it outputting the correct mime type?)

Also why not store the data on the filesystem. Having a larger table will slow down your queries
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

images are not sent in the same connection stream as page code. Your page will have to make a standard image reference for the thumbnail. On a side note, imagecreatefromstring() would likely be easier than writing the buffer out and reading it back in..
_undead_zzz
Forum Newbie
Posts: 3
Joined: Fri Mar 17, 2006 5:08 pm

Post by _undead_zzz »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Well, the client want's everything in the DB, so, I don't think I'll be able to manage the pictures from the system folders.

I think the mime type is correct, I have this php that queries the same db and shows the pic correctly

Code: Select all

<?php
       $idfoto = (isset($_GET["idfoto"])) ? $_GET["idfoto"] : exit();
       $tam = (isset($_GET["tam"])) ? $_GET["tam"] : 1;
       switch($tam) {
               case "1":
                       $campo = "foto";break;;
               case "2":
                       $campo = "thumb";break;;
               case "3":
                       $campo = "thumb2";break;;
               default:
                       $campo = "foto";break;;
       }
       $sql = "SELECT $campo, mime FROM fotografias WHERE idfoto = $idfoto";
       $link = mysql_connect("localhost:8889", "root", "root") or
die(mysql_error($link));;
       mysql_select_db("filestorage", $link) or die(mysql_error($link));
       $conn = mysql_query($sql, $link) or die(mysql_error($link));
       $datos = mysql_fetch_array($conn);
       $imagen = $datos[0];
       $mime = $datos[1];
       header("Content-Type: $mime");
       echo $imagen;
?>
Perhaps this method is wrong, since it's creating an image from the BLOB data, and not from an image file

Code: Select all

switch($mime) { 
                       case $mimetypes[0]: 
                       case $mimetypes[1]: 
                       $img = imagecreatefromjpeg($t_img_name); 
                       break; 
                       case $mimetypes[2]: 
                       $img = imagecreatefromgif($t_img_name); 
                       break; 
                       case $mimetypes[3]: 
                       $img = imagecreatefrompng($t_img_name); 
                       break; 
               }
If I eliminate that method... what should I use in the $img here ?

Code: Select all

imagecopyresized($thumb, $img, 0, 0, 0, 0, $ancho, ALTURA, 
$datos[0], $datos[1]);

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
_undead_zzz
Forum Newbie
Posts: 3
Joined: Fri Mar 17, 2006 5:08 pm

Post by _undead_zzz »

feyd wrote:images are not sent in the same connection stream as page code. Your page will have to make a standard image reference for the thumbnail. On a side note, imagecreatefromstring() would likely be easier than writing the buffer out and reading it back in..
Oh, Let me try that !!

then maybe returning the final var, like "return $tthumb" at the end of the method and assigning it into another var in a page like this

Code: Select all

<?php 
       $idfoto = (isset($_GET["idfoto"])) ? $_GET["idfoto"] : exit(); 
       $tam = (isset($_GET["tam"])) ? $_GET["tam"] : 1; 
       switch($tam) { 
               case "1": 
                       $campo = "foto";break;; 
               case "2": 
                       $campo = "thumb";break;; 
               case "3": 
                       $campo = "thumb2";break;; 
               default: 
                       $campo = "foto";break;; 
       } 
       $sql = "SELECT $campo, mime FROM fotografias WHERE idfoto = $idfoto"; 
       $link = mysql_connect("localhost:8889", "root", "root") or 
die(mysql_error($link));; 
       mysql_select_db("filestorage", $link) or die(mysql_error($link)); 
       $conn = mysql_query($sql, $link) or die(mysql_error($link)); 
       $datos = mysql_fetch_array($conn); 
       $imagen = $datos[0]; 
       $mime = $datos[1]; 
       header("Content-Type: $mime"); 
       echo $imagen; 
?>
Post Reply