image | Eric's Drupal Blog

Content tagged with: image

Eric's picture

Adding a jQuery Image Carousel to your Node View

In this tutorial I'll show you how you can add jQuery image carousel functionality to your CCK node. This tutorial requires you to install the following modules:

cck
filefield
imageapi
imagecache
imagefield

NOTE: The next few steps assume you have not already setup a content type and ImageCache preset. You can ignore them if you already have.

Once you installed the above modules and set permission accordingly, you'll need to define an ImageCache preset (/admin/build/imagecache/add). I called mine "thumbnail" for this example. I added a "Add Scale" action to set the width to 100 (pixels). This will ensure when you create an image, a thumbnail will be created automatically.

Create a new content type (/admin/content/types/add). I called mine "Carousel" for this example. I then clicked on "Manage Fields" (/admin/content/node-type/carousel/fields) to setup a new Image field. I called my new field "field_images", selected "File" for field type, and selected "Image" for the form element.

On the next screen, scroll down to the Global fieldset region and enable the "Required" checkbox, and set the "Number of Values" to "Unlimited". This will allow the user to upload numerous image files to the same CCK field.

Next, you'll want to set which image preset it shown when viewing the node, by clicking on "Display fields" (/admin/content/node-type/carousel/display). I chose the "thumbnail image" preset for both the teaser and full node displays.

I then created a new carousel node (/node/add/carousel). I used the "Add another item" button to upload three images to this node.

If you view the node, the images will be stacked vertically.

This is where the fun starts. You'll need to download the jQuery Carousel library (http://sorgalla.com/projects/jcarousel/). I downloaded the jcarousel.zip file and unpacked it. Copy the entire unpacked jcarousel folder into your theme directory.

Now, you'll need to add a preprocess_node function to your template.php file:

<?php
function YOURTHEME_preprocess_node(&$variables) {
 
 
// test for carousel node type
 
if ($variables['type'] == 'carousel') {

   
// include jCarousel javascript
   
drupal_add_js(path_to_theme() . '/jcarousel/lib/jquery.jcarousel.js');
   
   
// add jquery in enable jQuery carousel on <ul>
   
$js = "
      $(document).ready(function(){
        $('#mycarousel').jcarousel();
      });
    "
;
   
drupal_add_js($js, 'inline');
   
   
// include jCarousel css
   
drupal_add_css(path_to_theme() . '/jcarousel/lib/jquery.jcarousel.css');
   
drupal_add_css(path_to_theme() . '/jcarousel/skins/tango/skin.css');
   
   
// loop through images and create an item list
   
$items = array();
    foreach (
$variables['field_images'] as $key => $value) {
     
$items[] = $value['view'];
    }
   
   
// ensure images exist
   
if (count($items)) {
     
// add jQuery carousel html to $content variable
     
$variables['content'] .= theme('item_list', $items, NULL, 'ul', array('id' => 'mycarousel', 'class' => 'jcarousel-skin-tango'));
    }
   
  }
 
}
?>

The previous code snippet should result in the following:

If you'd like you can hide the old image html by adding a single line of CSS (or by adding more elaborate code in your template preprocess function).

Example:

div.field-field-images {
  display: none;
}

Adding the previous CSS will result in this example:

gradient spacer
Eric's picture

Creating a rotating image block using Views and ImageCache

A rotating image block can easily be created by using modules that you make already have installed. If you have not already download, install, and enable the following modules:

Views
CCK
FileField
ImageField
ImageAPI
ImageCache

After you've installed these modules, you'll have to adjust the user permissions (admin/user/permissions) to ensure the correct roles have permission to view and administer these modules.

Create a new ImageCache preset (admin/build/imagecache). In my example, I named it "home_rotating". I also added a Scale action to ensure all the images are the same width. It's probably a good thing to check the region width in your theme and adjust the image width accordingly.

Define a new content type (admin/content/types/add). In my example, I named it "image_home_rotating". I left off the body field label because I am only concerned with displaying the image (no other text). Add a new field to the new content type, and choose Image for Field type and Widget. I named mine "field_image_home_rotating" and labeled it "Image". After adding the field, click on the Display Fields tab. For teaser and Full node, choose the ImageField preset you just created. This will ensure the right image size is used when viewing the node.

Create a new view (admin/build/views/add). Make sure you choose Node for View Type on the first form screen. Add two filters: one for published nodes; and one for node type, and choose the newly created node type. To get a random selection of nodes, add Sort Criteria and choose "Global: Random". Limit the Items to Display to 1. You can leave the Style set to Unformatted and the Row style to Fields. Add a new field and select the newly created image field. When adding the field, the Format setting allows you to change the ImageField preset. If it's not already set, choose your new preset. I also choose to hide the Field label on this step. Add the Block display to the view and save it.

Lastly, assign this block view to the region in your theme and add a few image nodes. As you refresh the page, the image should change.

gradient spacer
Eric's picture

Resolving the page title issues caused by the menu_breadcrumb & admin_menu modules

If your site uses the admin_menu & menu_breadcrumb modules, you might have noticed something wrong with your page titles on the /user page, similar to this:

# / # <img src="/sites/all/modules/admin_menu/images/icon_users.png" width="16" height="15" alt="Current anonymous / authenticated users" title="Current anonymous / authenticated users" />

The incorrect HTML is being output from the admin_menu in admin_menu.module on line 286, but it's caused from the menu_breadcrumb module.

As a quick solution, I added some code to my theme template.php file in a preprocess_page function to replace the titles.

<?php
function MYTHEME_preprocess_page(&$variables) {
  if (
stripos($variables['head_title'],'icon_users.png')) {
   
$variables['head_title'] = 'User account | ' . variable_get('site_name', '');
  }
  if (
stripos($variables['title'],'icon_users.png')) {
   
$variables['title'] = 'User account';
  }
}
?>

gradient spacer
Eric's picture

Replacing a submit button with an image using CSS

I just created a new image to replace the generic submit button appearance, when I remembered the submit buttons are type submit, not type image. I decided to use some quick CSS instead. The trick is adding a background image to the submit button, and then *hiding* the button text by adding padding-top CSS that is greater than the height of the image, and adding "overflow: hidden" to ensure the text does not get pushed outside the button...

form#MYFORMID #edit-submit {
  height: 22px;
  width: 73px;
  background: url('images/button_submit.jpg') no-repeat;
  border: none;
  overfow: hidden;
  padding-top: 25px;
}

gradient spacer
Eric's picture

Dynamically resize image dimensions using jQuery

I'm working on a piece of code that outputs a variable number of node fields into a table that has one row. I decided to use a table and tds to solve the following: 1) maintaining a true MVC by keeping my CSS (view) separate from my code (model); and 2) implement a variable number column solution that does not require a hard-coded width. Anyway... After implementing the table, I realized large images in my content would skew the overall width of each td. I decided to write some jQuery to resize images in my content automatically. This code figures out the overall width of my page (parent div of the table) and the padding on each td, and adjusts as necessary...

<?php
$(document).ready(function(){
   
 
// get width of main column
 
var mainWidth = $('#MYPARENTDIV').innerWidth();

 
// get number of columns
 
tdCount = $('td', 'table.MYTABLECLASS tr').length;
   
 
// tally td padding
 
var tdPadding = 0;
  $(
'td', 'table.MYTABLECLASS tr').each(function(){
   
tdPadding += $(this).innerWidth() - $(this).width();
  });
   
 
// calculate new max image width
 
var maxWidth = (mainWidth - tdPadding) / tdCount;
   
 
// adjust image dimensions
 
$('img', 'table.MYTABLECLASS tr td').each(function(i){
   
// check the width of the image
   
if ($(this).width() > maxWidth) {
     
// calculate new image dimensions
     
newWidth = maxWidth;
     
newHeight = $(this).height() / ( $(this).width() / maxWidth );
           
     
// set new image dimensions
     
$(this).height(newHeight).width(newWidth);
    }
  });   
});
?>

gradient spacer Syndicate content