Image submit buttons on Drupal 5.10
I follow this useful howto by Tom Constant on how to add image submit buttons on Drupal forms. Since I got a JavaScript error on drupal.js (line 31: button is undefined), I rewrite Tom’s implementation, and now it works fine on Drupal 5.10:
function phptemplate_button($element) {
// Make sure not to overwrite classes.
if (isset($element['#attributes']['class'])) {
$element['#attributes']['class'] = 'form-'. $element['#button_type'] .' '.
$element['#attributes']['class'];
}
else {
$element['#attributes']['class'] = 'form-'. $element['#button_type'];
}
// here the novelty begins: check if #button_type is normal submit
// button or image button
switch($element['#button_type']) {
case 'image': $button_type = 'image';
break;
default: $button_type = 'submit';
break;
}
return '<input id="'. $element['#id'].'" name="'. $element['#name'] .'"
type="' . $button_type . '" value="'. check_plain($element['#value']) .'" />
PDF cover thumbnails for attached files
This howto has been superseded by http://drupal.org/node/815816 (patch for Upload preview module).
Tested on:
- Drupal 5.x
- Content Templates module
- ImageMagick 6.3.7
- GhostScript 8.15.3
Using ImageMagick + GhostScript you can convert the first page of a PDF into a thumbnail image linking to file. You can cut and paste this function on your theme main script: you have to manually create the thumb directory and grant write permission by scripts. Check also the path to ImageMagick convert command.
function pdf_thumb_attachments(){
$allowed_mime = array("application/pdf");
foreach($files as $file){
if(in_array(strtolower($file->filemime),$allowed_mime) && strstr(strtolower($file->description),"classifica")){
$title = $file->description;
if(substr($title,-4)==".pdf"){
# create link title from file name (Transliteration module suggested)
$title = str_replace("_"," ",substr($title,0,strlen($title)-4));
}
$img = "";
$local_src_path = getcwd() . "/" . $file->filepath;
$destfilename = substr($file->filename,0,strlen($file->filename)-4) . ".gif";
# YOUR-FILES-DIRECTORY/pdfgen will hosts the generated thumbnails
$local_dest_path = getcwd() . "/" . file_directory_path() . "/pdfgen/" . $destfilename;
$imageurl = base_path() . file_directory_path() . "/pdfgen/" . $destfilename;
/* create thumbnail on node reading if file doesn't exist */
if(!file_exists($local_dest_path)){
# choose the first page of the PDF, scale to 90x90px and convert to GIF
$exec = '/usr/bin/convert -scale 90x90 "'.$local_src_path.'"[0] "'.$local_dest_path . '"';
shell_exec($exec);
/* delete this line if you cannot read the generated file
$exec = "chmod 777 ".$local_src_path;
shell_exec($exec);
//*/
}
$img = '<img src="'.$imageurl.'" alt="'.$title.'" />';
$output .= "<li>". l($img." ".$title,$file->filepath, $attributes = array("title"=>$title), $query = NULL, $fragment = NULL, $absolute = TRUE, $html = TRUE) . "</li>";
}
}
if(!empty($output)){
$output = '<div class="my-attachments"><ul>' . $output . "</ul></div>";
}
return $output;
}
Update
You can alternatively use imagemagick function provided by image.module (the image.imagemagick.inc file on drupal/includes):
function file_preview_path($filename, $filepath = 'filepreview') {
/** generate jpg thumbnails **/
return $filepath . "/" . substr($filename,0,strlen($filename)-4) . ".jpg";
}
function file_preview(&$file, $pages = Array(1)) {
$allowed_mime = array("application/pdf");
if(in_array(strtolower($file->filemime),$allowed_mime)){
$local_src_path = getcwd() . "/" . $file->filepath;
$local_dest_path = getcwd() . "/" . file_directory_path() . "/" . file_preview_path($file->filename);
# create thumbnail only when needed
$imagemagick = getcwd() . '/includes/' . 'image.imagemagick.inc';
if(!file_exists($local_dest_path) && file_exists($imagemagick)){
include_once($imagemagick);
_image_imagemagick_convert($local_src_path.'[0]',
$local_dest_path, array('-colorspace RGB'));
# all can read thumbnail files
if(file_exists($local_dest_path))
chmod($local_dest_path, 0777);
}
return file_preview_path($file->filename);
}
else{
return FALSE;
}
}
Image rollover on primary links Drupal howto
Tested on:
- Drupal 5.x
- Zen theme
- Requirements: JQuery Update
Do you remember my Customize links menu in Drupal mini-howto? You can use it to change text links in image links renaming and customizing theme_links function (from theme.inc) and adding it to your template.php.
On your template.php file, you have something like this:
$imglink_path = path_to_theme() . “/my_zen_subtheme/menu/” . str_replace(” “,”_”,strtolower($link['title']) . $imgsuffix . “.gif”);
$imglink = theme_image($imglink_path, $alt = $link['title'], $title = $link['title'], $attributes = NULL, $getsize = FALSE);
if(empty($imglink))
$imglink = $link['title'];
- Search for images in a defined folder by link title: if link title is “My Nice Link”, a “my_nice_link.gif” image is used.
- Generate HTML code for “my_nice_link.gif”. Note: getsize is set to FALSE to avoid errors on rollover
- If Drupal cannot generate HTML code for image (e.g. file not found) link is rendered as plain text
If you want to add a rollover effect on primary links images, you have to add this JQuery javascript code on page.tpl.php:
<script type="text/javascript">// <![CDATA[
$(document).ready(function()
{
$("#primary a").mouseover(function ()
{
$rollsrc = $(this).children("img").attr("src");
$matches = $rollsrc.match(/_active.gif$/);
if (!$matches) {
$rollON = $rollsrc.replace(/.gif$/,"_active.gif");
$(this).children("img").attr("src", $rollON);
}
});
$("#primary a").mouseout(function ()
{
$(this).children("img").attr("src", $rollsrc);
});
});
// ]]></script>
Where "_active" is the rollover image.
See also:
- Easy, Reusable Image Rollovers with jQuery (JQuery rollover tips used for this mini-howto)
Update:
- A module for Drupal 6.x named Signwriter allows to generate text from images even for menus.



Recent Comments