Display a random image from an image field - drupal

I have an image field on my content type that can accept 5 images to be uploaded. I would like for just one of them (at random) to be displayed on my page. Is there a module that can create a display formatter that will do this? Or is there a way I can accomplish this in my page.tpl.php?

Figured it out..
$imgCount = count($node->field_header_image[$node->language]) - 1;
$randomNum = rand(0,$imgCount); echo $randomNum;
echo file_create_url($node->field_header_image[$node->language][$randomNum]['uri']);

You could give Single Image Formatter a try, from its drupal project page it allows you to define which image to display. Providing an index which will never exist (6 if you can upload up to 5 images) it will choose a random image. Sounds like it could do the job.

Related

GravityForm preview thumbnails for the multifile upload field

I want to display the preview thumbnail of the multifile upload field. I had followed this thread but I found that {fileName} had some random string. It’s between {input id} and {file.target_name}.
Ex: 4a017277_input_5_zfKuGrwCd91CyiHy_o_1esroacel59uais19ev1dqa405q.png.
What is {zfKuGrwCd91CyiHy}?
tmp image's name
I had found a solution for this. It's not a beautiful way but it worked like a champ.
The random string above was rendered by a function random_str(). My solution is to remove or customizing the function.
File located at path: ../wp-content/plugins/gravityforms/includes/upload.php
enter image description here

using Views_get_view_result to return imagefield contents, but how do I get the path?

how do I grab images from a node using php? I"m using views_get_view_result to retrieve the array from Views and for the image field I get a serialized string:
a:2:{s:3:"alt";s:20:"south pacific poster";s:5:"title";s:0:"";}
it contains no reference as to where the image is located or the name of the image. In Views the image displays properly in the preview area.
Thanks
ah! Views has an option for URL to File
Its hard to tell exactly what you are doing, but you can get the filepath from the fid with something like:
<?php
$r = db_result(db_query('select filepath from files where fid=%d',$fid));

Add HTML to node title in Drupal module, not in theme layer

I want to add some functionality to my Drupal 6 module: I want to insert an image next to certain node titles (it involves a span and an img tag). I thought I could do this by just implementing mymodule_preprocess_node() and modifying the title. However, Drupal strips out all tags to avoid XSS attacks.
I've seen many solutions that involve the theme layer (most commonly http://drupal.org/node/28537), but I want to do this in my module, not in the theme. I don't want to modify any .tpl.php files or template.php. Can anyone give me tips on how to do this?
You mention that you've tried preprocess_node(), and are correct that, if you are storing the img tag as part of the node title, Drupal will indeed strip that out, as it runs $node->title through check_plain in template_preprocess_node().
My suggestion would be to store the actual image as an image field (using some combination of the imagefield module and imagecache for sizing), set the display of that field to be hidden on the CCK display tab for the given content type, and then attach the image to be part of the $title variable in your module's preprocess function. This solution would also allow you to display that image next to the title in any views you may need to create.
By 'certain node titles' - do you mean all nodes titles from certain node types?
If so, you can likely style the node using only CSS. By default all nodes will have a class that corresponds to the node type. Using CSS background images, you can add an image to the node title.
Your module can call drupal_add_css and add in any required CSS into the page. This way, it is theme independent.
I think the easier way is with javascript / Jquery.
You create a Jquery script which is called only in certain types of nodes and pass the number of views from drupal to the jscript.
You can call drupal_add_js() inside your module_preprocess_node and pass a variable which contains the number of views or the image link to the script. Something like this:
<?php
drupal_add_js("var mymodule_imagelink = " . drupal_to_js($imagelink) . ";", 'inline');
drupal_add_js("my_js_file.js");
?>
then in my_js_file.js just change the text. There are several ways to acomplish this. For instance, you can do a search in the document and change the title to something else or you can use a determined ID, etc...
Find text string using jQuery?
http://api.jquery.com/replaceWith/

Imagecache for non image CCKs?

I'm creating a view with images, videos, audio and documents (books) and I like to shown a picture of each of them in a carousel.
Everything works nicely with images and videos as far as we added a image-thumbnail (image filefield) to their CCK but we like to show a default image for audio and documents without changing the original CCK. Is it possible with imagecache (may be with imagecache_custom_code or views_custom_php) or we need to look for a different approach?
Thanks for your help,
m.
You are able to do this from two places.
First, you can go into Display Fields part of the CCK content type settings and change its default display in the teaser and node view. You will be given all the current imagecache presets as sizing options for display.
Second, you can also change the output as part of Views. Adding the image as a Field to show will give you the same set of preset options.
I'm going to say something very similar to Kevin above but I think it's the solution your looking for. To understand the problem:
you have 3 content types, two have thumbnails that you set individually
for the third you would like to have a default image displayed but not have to set it
I think you should still use a cck field.
Add a cck field for the image
Set a default image for that cck field (http://screencast.com/t/YzA0OWY4Mz)
Set your imagecache from the display tab
Hide the cck field from node edit for using http://drupal.org/project/formfilter, http://drupal.org/project/cck_field_perms, or hook_form
I might be mis-understanding your issue though.
Thanks for your both for your help.
I think you should still use a cck field.
You took a good picture of the scenario, but this is exactly the point of what I was trying to avoid. :-)
I don't want to use a cck field for books and I don't want to hide it with hook_form_alter.
I think I finally found a solution that didn't require adding extra CCK-fields to my "non-image" content types (audio and text).
There is a module called "Views custom field" (drupal.org/project/views_customfield) that allows you to add a PHP-processed-field to your view, so you can filter by content type and apply imagecache or imageapi code
$node=node_load($data->nid);
$img_path=drupal_get_path("module", "gt_medias") . "/images";
switch ($node->type) {
case "gt_video":
$img_path = $node->field_gt_thumbnail[0][filepath];
break;
case "book":
$img_path .= "/bookDefault.jpg";
break;
case "gt_audio":
$img_path .="/audioDefault.png";
break;
case "pingv_image":
$img_path = $node->field_pingv_image[0][filepath];
break;
}
$imgcache_url = imagecache_create_url('gt_medias_video_346', $img_path);
$output .= '<img src="'. $imgcache_url .'" ' />';
return ($output);
Where "gt_medias_video_346" is the name of my imagecache preset.
Once again, thanks you both for your help,
m.

How would you recommend adding an image as a custom field in WordPress?

I need to add an image to each page in WordPress.
I don't wish to insert it using the WYSIWYG editor, I just need the url as a custom field, which I later use in the template.
I tried using the CFI plugin (Custom Field Images), and I hacked my way into getting it to work with the rest of my plugins, but then I moved the site to the production server and CFI just didn't work for some reason.
I can't seem to find any other plugin that just lets you pick an image from the library and add it as a custom field.
I've downgraded to the point where I'm willing to manually enter all the URLs into each and every page. but before I do, I thought I'd check here.
Can anyone tell me what's the easiest, best way to add images as custom fields in WordPress (2.7.1 if it matters)?
In our WordPress template, each post generally only has one image 'attached', which is displayed outside the posts' (textual) content.
I simply upload the file using the edit posts' media uploader, never insert it into the post like JoshJordan above, then retrieve the image using a bit of code in the right place in my template file.
This would also work if you're using more than one image in your post, eg. in your post content. As long as you keep the image used as the 'main' post image as the first image (remember you can re-order images in your posts' image library by dragging them up and down), you're easily able to call it anywhere in your template file by using something like this:
<?php
$img_size = 'thumbnail'; // use thumbnail, medium, large, original
$img_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts where post_parent= $post->ID and (post_mime_type = 'image/jpeg' OR post_mime_type = 'image/gif') and post_type = 'attachment'");
$img_array = wp_get_attachment_image_src($img_id,$img_size,false);
echo '<img src="'.$img_array[0].'"' title="'.get_the_title().'" />';
?>
No need for copying and pasting image urls.
The template I have uses a manually-entered custom field for the splash image of each post. When I'm done writing my article, I upload an image, copy its URL from the upload tool, never insert it into my post, and then paste that URL into the "Image" custom field. Simple as pie and takes only seconds. Insignificant compared to the amount of time it takes me to write an article.
You can use the custom key value fields on posts as well. let's say you always give your images the key 'thumb'. you can then use this code to output them in your post as a thumbnail:
<?php
$values = get_post_custom_values("thumb");
echo “<img src=\”$values[0]\” class=\”thumb\”></a>”; ?>
Consider using Flutter it's a bit tricky to figure out at first, and has many really useful featured, including EIP (edit in place), and image handling.
After installing the plugin create a new "Write Panel", you'll figure it out from there. The plugin provides you with a rather intuitive GUI, which includes an image uploader. The template tags are very easy to use, I believe it's something like
<?php echo get_image('name_of_field'); ?>
I just had to build a site for a client that needed the same feature, I ended up using Flutter.

Resources