Drupal: .htaccess Access Deny
August 21, 2014 – 7:59 am | No Comment

In this article I will tell how to forbid access to certain resources for some clients. The instructions will include descriptions of different directives.

Read the full story »
CSS Templates

Contain reviews and news about CSS Templates.

Freebies

Contain freebies such as icons, graphics, headers and images for your websites.

Fun Stuff

Contains other fun stuff for entertainment or interesting site showcase.

How-To

Contain technical elaborations on some specific workarounds or common tweak.

Joomla Templates

Contains reviews and news about Joomla templates.

Home » How-To

How To Generate Image With Drupal

Submitted by on June 20, 2011 – 8:25 amOne Comment

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.

Good luck!

One Comment »

  • Andy says:

    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);

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.