Generally, when I retrieve image from a MySQL blob field with php, I just specify the header type and echo the string. That suffices to show the image properly.

What is the way to achieve that in Drupal?

For example:

function show_premise_plan($pid=null) {

$query = "SELECT plan FROM {premises} WHERE pid=%d";
$result = db_query($query, $pid);
$row = db_fetch_object($result);

header('Content-type: image/jpeg');
echo $row->plan;
}

This function shows the image I want but NOT in the content area of Drupal. It simply show only the image on a new page that has nothing to do with Drupal. How do I change the lines

header('Content-type: image/jpeg');
echo $row->plan;

with

return STUFF

where STUFF is a proper html string containing the image.

Thank you!

Comments

yelvington’s picture

Sometimes when you get too close to a problem you forget really elementary stuff, like how HTML works.

You can not do what you describe. You don't embed binary data in an HTML page. You point to it with an IMG tag.

From your description, it sounds like you need to create a module that registers multiple menu callbacks, both potentially containing a pid argument. One path would render a page containing (among many other things) an img tag; another would render only a binary image along with a content-type header as you describe.

ixeos’s picture

yelvington, thank you for your reply! This is what I had just done before I read you post. It took me 2 hours to figure it out... Basically, I registered in mymodule_menu a callback to a page that echoes the blob field. Below is part of the code in case someone bumps into a similar problem.

function premises_menu() {
 
  $items = array();

  $items[] = array(
    'path' => 'editpremises',
    'title' => t('Edit premises information'),
    'description' => t('You can insert, delete, edit premises here.'),
    'callback' => 'show_edit_premises_page',
    'callback arguments' => '',
    'access' => user_access('edit premises content'),
    'type' => MENU_NORMAL_ITEM,
   );
   $items[] = array(
    'path' => 'showpremiseplan',
    'callback' => 'show_premise_plan',
    'access' => user_access('access premises content'),
    'type' => MENU_CALLBACK,
   );
   $items[] = array(
    'path' => 'retrievepremiseplan',
    'callback' => 'retrieve_premise_plan',
    'access' => user_access('access premises content'),
    'type' => MENU_CALLBACK,
   );
  return $items;
}  


function show_premise_plan($pid=null) {

   return "<img src=\"?q=retrievepremiseplan/".$pid."\" >";

}

function retrieve_premise_plan($pid=null) {
    $query = "SELECT plan FROM {premises} WHERE pid=%d";

    $result = db_query($query, $pid);
    $row = db_fetch_object($result);
	
     header('Content-type: image/*');
     echo $row->plan;
}