For compatibility with http://drupal.org/project/fckeditor and http://drupal.org/project/imagebrowser

I have modified image_caption.js to include the following lines - this transfers the 'class' attribute from the image to the containing div as in the case of imagebrowser, the alignment is done by it altering the 'class' rather than the 'align' attribute.

var imgclass = $(this).attr('class');
$(this).removeClass(imgclass);
$(this).wrap("

");

Hope this is of help to someone - not sure how compatible it is with other editors & image inserters.

Peter

Comments

anrikun’s picture

I've modified it this way, so that the CSS 'float' property of the image is transfered to the div:

$(document).ready(function(){
  $("img.caption").each(function(i) {
    var imgwidth = $(this).width();
    var imgheight = $(this).height();
    var captiontext = $(this).attr('title');
    var alignment = $(this).attr('align');
    if (alignment == undefined || alignment == '') {
      alignment = $(this).css('float');
    }
    $(this).removeAttr('align');
    $(this).css('float','');
    $(this).wrap("<div class=\"image-caption-container\" style=\"float:" + alignment + "\"></div>");
    $(this).parent().width(imgwidth);
    $(this).parent().append("<div class=\"image-caption\">" + captiontext + "</div>");
  });
});
jannalexx’s picture

this one works with CKeditor module inserted images, otherwise captions are overlaying content, (as far as I can guess it has to do with height css property). Some css tweaks may be needed if style properties used in ckeditor.

panhead490’s picture

Can you help me understand where you are making this change? And then is there a way to translate this to the input filter shipped with image_caption so there's a JS free way of doing this?

panhead490’s picture

Definitely can't read since you clearly stated: I have modified image_caption.js

The second question if there's a way to do this sans JS/through the input filter still stands.

Thanks for your help.

fabianx’s picture

Version: 6.x-2.3 » 6.x-2.5

Image Caption JS code for 2.5:

/*$Id: image_caption.js,v 1.2.2.3 2010/02/03 07:50:25 davidwhthomas Exp $*/
$(document).ready(function(){
  $("img.caption").each(function(i) {
    var imgwidth = $(this).width();
    var imgheight = $(this).height();
    var captiontext = $(this).attr('title');

    var alignment = $(this).attr('align');
    if (alignment == undefined || alignment == '') {
      alignment = $(this).css('float');
    }
    $(this).css('float', '');

    var style = $(this).attr('style');
    //Clear image styles to prevent conflicts with parent div
    $(this).attr({align:""});
    $(this).attr({style:""});
    $(this).wrap("<span class=\"image-caption-container\" style=\"display:block;" + style + "; float: " + alignment + "\"></span>");
    $(this).parent().addClass('image-caption-container-' + alignment);
    if(imgwidth != 'undefined' && imgwidth != 0){
      $(this).parent().width(imgwidth);
    }
    $(this).parent().append("<span style=\"display:block;\" class=\"image-caption\">" + captiontext + "</span>");
  });
});

Patch:

diff --git a/sites/all/modules/content/custom/image_caption/image_caption.js b/sites/all/modules/content/custom/image_caption/image_caption.js
index 9dabc15..4c9a01d 100644
--- a/sites/all/modules/content/custom/image_caption/image_caption.js
+++ b/sites/all/modules/content/custom/image_caption/image_caption.js
@@ -4,8 +4,14 @@ $(document).ready(function(){
     var imgwidth = $(this).width();
     var imgheight = $(this).height();
     var captiontext = $(this).attr('title');
-    var style = $(this).attr('style');
+
     var alignment = $(this).attr('align');
+    if (alignment == undefined || alignment == '') {
+      alignment = $(this).css('float');
+    }
+    $(this).css('float', '');
+
+    var style = $(this).attr('style');
     //Clear image styles to prevent conflicts with parent div
     $(this).attr({align:""});
     $(this).attr({style:""});
fabianx’s picture

Title: Compatibility with FCKEditor and Image Browser » Support float: right|left; Compatibility with CKEditor, FCKEditor and Image Browser
Version: 6.x-2.5 » 6.x-2.x-dev
Status: Active » Needs review
StatusFileSize
new2.03 KB

Hi,

I think this can be safely commited and will enable Image Caption to work with more Editors, so I add a real patch and change status to "needs review".

Best Wishes,

Fabian (LionsAd)

davidwhthomas’s picture

Thanks, I will commit suitable patches that have been reviewed and tested by the community.
DT

essbee’s picture

Further to help support this should the container take its width from the style rather than form the image width= property.

This would allow for scenarios where the img tag does not use width property but rather uses style="width: 100px"

Line 66 would need changing to search for width: rather than width=

IE
//replaced preg_match ('/width=\"(.+?)\"/i', $imgText, $matches);
preg_match('/width: (.+?);/i', $imgText, $matches);

I guess the ideal scenario would be to assess both so that either was supported.

Sam

ngstigator’s picture

subscribing

vood002’s picture

I tested updated JS in #5 and it worked better for me.

I unset the style & align though rather than setting them to "" as seen in #1

    $(this).removeAttr('align');
    $(this).removeAttr('style');
fabianx’s picture

StatusFileSize
new1.64 KB

Here is a short update code and patch:

Code:

/*$Id: image_caption.js,v 1.2.2.3 2010/02/03 07:50:25 davidwhthomas Exp $*/
$(document).ready(function(){
  $("img.caption").each(function(i) {
    var imgwidth = $(this).width();
    var imgheight = $(this).height();
    var captiontext = $(this).attr('title');
    if (captiontext == undefined || captiontext == '') {
      captiontext = $(this).attr('alt');
    }

    var alignment = $(this).attr('align');
    if (alignment == undefined || alignment == '') {
      alignment = $(this).css('float');
    }
    $(this).css('float', '');
    $(this).css('display', '');

    var style = $(this).attr('style');
    //Clear image styles to prevent conflicts with parent div
    $(this).removeAttr('align');
    $(this).removeAttr('style');
    $(this).wrap("<span class=\"image-caption-container\" style=\"" + style + "; float: " + alignment + "; display: inline;\"></span>");
    $(this).parent().addClass('image-caption-container-' + alignment);
    if(imgwidth != 'undefined' && imgwidth != 0){
      $(this).parent().width(imgwidth);
    }
    $(this).parent().append("<span style=\"display:block;\" class=\"image-caption\">" + captiontext + "</span>");
  });
});

Things new:

* use removeAttr instead of setting to "".
* allow usage of alt tag as caption.
* use inline for the container instead of block to fix IE issue.

New patch attached. Please test.

Best Wishes,

Fabian (LionsAd)

bryancasler’s picture

This needs to get comitted

DrDeezee’s picture

Version: 6.x-2.x-dev » 6.x-2.5
Category: feature » support

Accidentally posted in the wrong thread, please disregard / delete this.

topdillon’s picture

Version: 6.x-2.x-dev » 6.x-2.5
Category: feature » support
Issue tags: -patch

Doesn't seem to work for me. Only works for image that is not floated left or right and that is width of container --i.e. for image of 200px in content that is 600px, caption is 600px wide.

fabianx’s picture

Version: 6.x-2.5 » 6.x-2.x-dev
Category: support » feature
Issue tags: +patch

@#14: Okay, need to test this ...

ydga’s picture

Version: 6.x-2.5 » 6.x-2.x-dev
Category: support » feature
Issue tags: +patch

Very technical for me (designer) Im using Image Caption + CKEditor the left/right float is not transfered to the style of the caption container (image-caption-container-XXX) Im reading all your solutions, but I dont know where to start.

bryancasler’s picture

NenadP’s picture

I did this fusion of original code and Anrikun suggestion from #1, because text near the image was going through the caption text, with original code.
But the Anrikun solution was not completely good for me, because i use colorbox (lightbox plugin). When i was back from colorbox popup, the code was appending another caption text.

This was originally prevented by adding .caption-processed.

So i did quick fusion of two codes working for me. Someone maybe could find it useful:

(function($) {
  
Drupal.behaviors.image_caption = {
  attach: function (context, settings) {
    $("img.caption:not(.caption-processed)").each(function(i) {
      var imgwidth = $(this).width() ? $(this).width() : false;
      var imgheight = $(this).height() ? $(this).height() : false;
      
      // Get caption from title attribute
      var captiontext = $(this).attr('title');
      
      // Get image alignment and style to apply to container
      if($(this).attr('align')){
        var alignment = $(this).attr('align');
        $(this).css({'float':alignment}); // add to css float
        $(this).removeAttr('align');
      }else if($(this).css('float')){
        var alignment = $(this).css('float');
      }else{
        var alignment = 'normal';
      }
      var style = $(this).attr('style') ? $(this).attr('style') : '';

      // Reset img styles as are added to container instead      
      $(this).removeAttr('width');
      $(this).removeAttr('height');
      $(this).css('width', '');
      $(this).css('height', '');     
      $(this).removeAttr('align');
      $(this).removeAttr('style');
      
      //Display inline block so it doesn't break any text aligns on the parent contatiner
      $(this).wrap("<span class=\"image-caption-container\" style=\"display:inline-block;" + style + "\"></span>");
      $(this).parent().addClass('image-caption-container-' + alignment);
      
      // Add dimensions, if available
      if(imgwidth){
        $(this).width(imgwidth);
        $(this).parent().width(imgwidth);
      }
      if(imgheight){
        $(this).height(imgheight);
      }
      // Append caption
      $(this).parent().append("<span style=\"display:block;\" class=\"image-caption\">" + captiontext + "</span>");
      
      // Add class to prevent duplicate caption adding
      $(this).addClass('caption-processed');
    });
  }
};

})(jQuery);


avpaderno’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)
Issue tags: -patch

I am closing this issue, which is for a not-supported Drupal version.