How To Generate Image With Drupal
This article will tell you how to generate an image on your site with the help of PHP-script. You will learn two different methods to do this.
Separate File
Create image.php file in the site root. Use this code to output the image with a number of published articles:
<?php
/*
Include Drupal and download it.
All Drupal functions and system variables are available when drupal_bootstrap function is called.
*/
require_once ‘./includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
//Send content type header to a browser: PNG image.
drupal_set_header(“Content-type: image/png”);
//Create an image
$image = imagecreate(88, 31);
//Define color of text and background.
$background_color = imagecolorallocate($image, 2, 122, 198);
$text_color = imagecolorallocate($image, 255, 255, 255);
//Fill the image background with color.
imagefill($image, 0, 0, $background_color);
//Request a number of articles from the node table.
$query = “SELECT COUNT(`nid`) FROM `{node}`”;
$result = db_result(db_query ($query));
//Output the result to an image
imagestring($image, 5, 5, 7, $result , $text_color);
//Send the image to a browser.
imagepng($image);
//Clear server RAM resources.
imagedestroy($image);
Drupal Module
Let’s name our module testimagepng.
Add hook menu to testimagepng.module to create a path for an image on the site.
function testimagepng_menu() {
$items[‘testimage.png’] = array(
‘type’ => MENU_CALLBACK,
‘page callback’ => ‘testimagepng_image’,
‘access arguments’ => array(‘access content’), );
return $items;
}
Insert testimagepng_image function to the same file. The function will generate an image. Use this code:
function testimagepng_image() {
drupal_set_header(“Content-type: image/png”);
$image = imagecreate(88, 31);
$background_color = imagecolorallocate($image, 2, 122, 198);
$text_color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $background_color);
$query = “SELECT COUNT(`nid`) FROM `{node}`”;
$result = db_result(db_query ($query));
imagestring($image, 5, 5, 7, $result , $text_color);
imagepng($image);
imagedestroy($image);
//Stop executing of the scripts to disable sending Drupal’s outputs at the end of the image.
exit();
}
An advantage of this method is that an image is inserted to an HTML page with a code similar to this one (inserting of PNG-image to a file):
<img src=”http://mysite.ru/testimage.png”>
An image looks like a static file but you can generate it using any method inside testimagepng_image() function.
Thanks for the great info. I am using this method to create an image on my site. I want to have the data that I use for the image tied to the current node id. I cannot seem to pass the node id value into the image create function where I select the data. Do you have any suggestions? Here is my SELECT statement:
function dangerrose_image() {
if (arg(0) == ‘node’ && is_numeric(arg(1))) $nodeid = arg(1);
$query = “SELECT node.nid AS nid,
node_data_field_abeast.field_abeast_value AS abeast,
node.type AS node_type,
node.vid AS node_vid,
node_data_field_abeast.field_abnorth_value AS abnorth,
node_data_field_abeast.field_abnortheast_value AS abnortheast,
node_data_field_abeast.field_abnorthwest_value AS abnorthwest,
node_data_field_abeast.field_absouth_value AS absouth,
node_data_field_abeast.field_absoutheast_value AS absoutheast,
node_data_field_abeast.field_absouthwest_value AS absouthwest,
node_data_field_abeast.field_abwest_value AS abwest,
DATE_FORMAT((FROM_UNIXTIME(node.created) + INTERVAL -28800 SECOND), ‘%Y%m%d%H%i’) AS node_created_minute
FROM node node
LEFT JOIN content_type_forecast node_data_field_abeast ON node.vid = node_data_field_abeast.vid
WHERE (node.status = 1) AND (node.type in (‘forecast’)) AND (node.nid = $nodeid)”;
$avyforecast = db_query($query);
$avyinfo = db_fetch_array($avyforecast);