Drupal 6 – “Best Practice” for adding images to content.

"Best Practice" in this instance is open to interpretation - this is my first effort at writing for Drupal. Best Practice for Placing Images depends on your individual site, but I think most people seem to like using CCK with Filefield and ImageField (and perhaps Insert module with Lightbox2). You'll also probably need to use ImageCache with this.

Install CCK (checking Content, Content Permissions, and File Field) and then install ImageField and ImageCache (Image Cache UI, Image Api and GD2) and lastly Insert and Lightbox2.

Now go to Image Cache and set up some imagecache presets - such as scale images to 150px wide, 200px wide, 250px wide, scale and crop images to “150px x 100px”, etc.

Adding Thumbnails linked to node
Go to Content and click on "Edit" beside the content type you wish to add images to, and add a new field (using "File" for type of field and "Image" for Widget type). I usually call the field that I am going to use as a thumbnail "add_image" and place it above the body field ((some css later on will tell the imagecache present that will populate this to float left); if I wish to have an image that will float right, I add a second field called "add_image_right" (and add some css to the theme to tell it to float right). Click save.

On the configure page (that you are automatically brought to), you can select which presets you wish to attach to this field. I usually only add information for the “Alt Text” and “Title” settings and enable the “Description” field. Click save.

Now click on “Display Fields”. On the “Label” field, set this to “Hidden” (so that there is no text saying “Add Image” above the image.

For the “Teaser” field choose an imagecache preset that will be used here – I choose “teaser-left linked to node” (having already set up an imagecache preset called “teaser-left” (and setting this to “Scale and Crop” 150px wide by 100px high). This means that if you click on the image when viewing a list of teasers, it will open up the page/node.

For the “Full Node”, choose an imagecache preset that will be used here – I choose something like “Lightbox2 – full-node to add_image_500px” (having set up imagecache presets called “full node” (image scaled to 250px wide, for example) and “add_image_500px” (image scaled to 500px wide). This means that if you click on the image when viewing the page it will open up in a Lightbox2 window at 500 pixels wide, but display on the page at 250px wide.

Repeat the above steps for “teaser-right” and “full-node-right”.

If you use the above, you’ll notice that your images are displayed above your content, rather than inline with it. To get them to display inline, you need to add the following css to your theme’s css file (probably style.css):

/* BEGIN imagecache classes */

.imagecache-teaser
{
 float: left;
} 

.imagecache-teaser-right
{
 float: right;
} 

.imagecache-full-node
{
 float: left;
}

.imagecache-full-node-right
{
 float: right;
}

To add identical fields to other content types, you simply click on “Edit” beside the content type and then scroll down to “Add existing field” and choose the field(s) you wish to add, ensuring to “drag and drop” the field(s) above the “body” field.

Adding extra images to nodes
Once I have the above done, I add another field called “Extra Images” and place it below the “Body” field. On the “configure” page, I set the maximium resolution to 640x480, set the “Alt Text” and “Title Text” settings (ensuring to check the box to allow custom text), and then turn on “Insert” and choose the imagecache presets that I wish to allow here (usually scale image to 200px, 250px, 300px, 350px and 500px and also the relevant settings that will allow each of these be displayed in a Lightbox2 window at 500px). (When you click on “Insert” when adding images to nodes, you will also have to click on the “image” icon in TinyMCE (or CKEditor or similar) or use html to float the image left/right and set margins etc. (unless you have it all set up in the css).)

Under “Global Settings” there is an option to limit the number of images, I usually set this to “unlimited” and also enable the “Description” field (this is the field that is used to give a description of the image in the Lightbox2 window).

Click on “Save” and on the “Display Fields” page, set everything for this field to “hidden” so labels do not show above your field and your images do not show up twice (once with this preset and once because you will have used the “Insert” button when creating nodes).

To make sure that the Lightbox2 settings work correctly, go to “Input Formats” and click on “configure” beside the Input Format you wish to edit (usually “Full HTML”), then check “Lightbox Filter”.

Setting Permissions
Lastly, we have to go to our “User” settings and “Permissions” and allow whatever roles you need to to view the necessary fields under “content_permissions module”. Click “Save”.

Comments

odisey’s picture

This tutorial looks good except for explanations on text wrapping, additional images and formatting.

First, text wrapping: When adding CSS to float main image thumb left (see also: http://drupal.org/node/153465), the image will float left as anticipated. However, text will not wrap. Text align will follow right margin of image for height of image, and instead of wrapping left under image, text continues to follow same path, as though an extended margin has been added on bottom of image. The only way to get text to wrap is to hit "space bar/return carriage" when adding text in body field at the appropriate place to break text into two WYSIWYG paragraphs.

Is there a fix for this? It has to do with how Drupal writes the HTML based on php output fields. I imagine I will look at creating a .tpl for the custom content to fix this issue.

Second, additional images and format: When adding unlimited images they all print in a column -- floated left or right depending on your CSS class override -- and they do not explode from that column array into rows of images. How do you use CSS to align additional thumbs in a grid instead of a column?

Thanks!

markconroy’s picture

Hi Odisey,

For me the wrapping is fine. I just put in .imagecache-teaser-left (etc) and the text wraps around it. On full node view it's the same.

You would need to create more image cache presets that don't have any css attached to them to lay them out in a grid. You could do so by inserting the images using the insert module. And then using your WYSIWYG you can align them left/right/center.

============

Drupal Core Maintainer for "Out of the Box" Initiative
Annertech - Web Agency of the Year.

odisey’s picture

Mark, I appreciate your quick reply after creating your post a while back. Your method for formatting images looks great - especially the main content image. I want to use this method. I still have not quite wrapped my mind around how you are getting text wrapping to work though. And, I have a really simple, and clean CSS solution for the grid which I will explain in detail in a moment. This method for grid preserves the Lightbox slideshow - grouping all images that are additional into the show.

First on text wrapping: I have a field for the main image on my content type node that is named field_news_main_image. I can target this image in CSS through targeting this class .field-field-news-main-image (see: http://drupal.org/node/153465). As previously described I can float left or right, add margins, etc ... but, text will not wrap the image. The image cache preset I have on the full node for this image is named main_news_item_image. If I understand your method correctly, I would target the image through CSS using the imagecache preset name as the class I am wanting to manipulate - and this method will allow text to wrap image if it is floated. So - I will read your explanation again and try to get it to work again. I need to figure out how you used an imagecache preset name as a class target.

On grid and floating additional images; After a little thought I realized this was an easy clean fix in CSS. Instead of targeting the global field class .field-field-news-image I realized I could explode the instances of the field class array with the finer specificity of .field-field-news-image img. This targets each instance printed in the HTML. I floated the images left, they aligned perfectly. I added a 5px margin and they spread out on the page in a grid. They remained in the same array therefore they are grouped in a slideshow in the Lightbox when anyone of them (thumbs) is clicked.

Thanks again,
Odisey

odisey’s picture

OK Mark - I targeted the image through doing a view source on target page, then locating the classes wrapping the target image. Strangely I could not use conventional CSS markup substituting dashes for underlines, I had to use underlines in the style sheet. So, the imagecache preset I have named main_new_item_image can be targeted using .imagecache-field_news_main_image-main_news_item_image (cut out of source file). Even though I can float image, etc... using this alternate class text still refuses to wrap. Don't know why. This is CSS and I am using a template theme, not a Drupal core theme. Maybe something in my theme is causing text to behave this way. So, I created CSS rules for text on this node

Text I want to wrap

. Now everything you described above works in my case, with this additional rule on text.

Thanks for the tutorial. Perhaps our log of trouble shooting will help another.

Marc

markconroy’s picture

I used to call my imagecache presets with underscores (eg my_image_preset), but to get the CSS to work, I had to use my-image-preset in the CSS, so I now use dashes instead of underscores.

It's quite annoying in drupal that some modules MUST use hyphens and some you MUST use underscores. It'd be better to have them all standardised.

Glad it has worked out for you.

============

Drupal Core Maintainer for "Out of the Box" Initiative
Annertech - Web Agency of the Year.