1

I have to scale down images which match the following selectors:

img.math
div.math img

The problem is that I have to scale images down twice their own size — that is relative to their own size. It seems that it is only possible with javascript:

  • css tricks with width scale images relative to the current line witdth
  • css tricks with transform leave empty space

I found a js function which scales an image down here:

function Resize(imgId)
{
  var img = document.getElementById(imgId);
  var w = img.width, h = img.height;
  w /= 2; h /= 2;
  img.width = w; img.height = h;
}

the problem now is to apply that function to image matching the above mentioned selectors.

I use jQuery 1.11 and something like this is due:

// resize all math images:
$(document).ready(function() {
    $("img.math").each(function() {
        // ???
    });
    $("div.math img").each(function() {
        // ???
    });
});

(this is a part of the larger (function ($) { }(window.$jqTheme || window.jQuery));)

Edit:

img.math is just an inline image. While div.math img is a numbered math equation: it also contains the equation number, which is span floated to the right.

Edit 2: Complete html section sample

The html involved is pretty basic:

<div id="math" class="section">
  <span id="id1"></span><h1>Math</h1>
  <p>In-line equation: <img alt="a^2+b^2=c^2" src="_images/math/ee657daf07808119b97144fc459a5dc8839eb9c9.png" class="math">.</p>
  <p>Numbered equation:</p>
  <div id="equation-pythag" class="math">
    <p><span class="eqno">(1)</span><img alt="a^2 + b^2 = c^2" src="_images/math/53158707cc080d32d0d2f081e4bf5c6988f437ca.png"></p>
  </div><p>Link to equation: <a href="#equation-pythag">(1)</a>.</p>
</div>

All images staically positioned: img.math is just an inline image, while div.math img is horizontally centered in a div.

Community
  • 1
  • 1
Adobe
  • 12,967
  • 10
  • 85
  • 126

1 Answers1

2

This is not enough information to post in a comment, but it isn't enough for an answer.

Still, I will answer it.

Your code has to be rewritten to the following:

// resize all math images:
$(function() {
    $("img.math, div.math img").each(function() {
        this.width/=2;
        this.style.height='auto'; //ensures proportion
        this.style.verticalAlign='middle';
    });
});

All that boilerplate code can be safely eliminated.

I've removed the line this.height/=2; because the O.P. stated that it was causing his images to deform.

I've added the line this.style.height='auto'; as suggested by @MorganFeeney.
It helps to ensure proportion to the image resize, in case an height it set.

Also, as the O.P. pointed out, it was needed to set this.style.verticalAlign='middle';.
If you are having problems with alignments, this might help. Or you can try the values inherit or top.

This method can be a little inaccurate in some situations.
For that, I recommend reading How do I get natural dimensions of an image using javascript or jquery? to get the original width and height of your image.
Also, you can read this interesting comment for a cross-browser solution.


As a side note:

$(function(){});

Is equivalent to:

$(document).ready(function(){});
Community
  • 1
  • 1
Ismael Miguel
  • 4,185
  • 1
  • 31
  • 42
  • Turns out `this.height/=2;` line is not needed. Your code actually solves the problem. – Adobe Jan 15 '15 at 09:23
  • @Adobe That also depends on the case, but sometimes it's beneficial to have it there. I would recommend that you don't vote on an accepted answer so soon, since someone can come up with a better solution. I've posted this without intention of getting any reputation, but because it is too much for a comment. I'm glad it worked for you, but you should remove the mark as accepted answer, wait a few hours and then you choose the best one. – Ismael Miguel Jan 15 '15 at 09:29
  • `this.height/=2;` actually deforms the image vertically. So I guess when I did `this.width/=2;` the aspect ratio has been preserved. So the `this.height/=2;` line is not just not needed, but harmful in my case. – Adobe Jan 15 '15 at 09:45
  • @Adobe That may vary. But I think I can explain the behavior: When you set the `this.width/=2`, probably this triggers a re-design of the image. Then, applying `this.height/=2;` will reduce the height again. That may be the reason. I have updated the answer. – Ismael Miguel Jan 15 '15 at 09:48
  • Perhaps just have the width attribute and set height: auto in css? – Morgan Feeney Jan 15 '15 at 09:58
  • @MorganFeeney Now THAT is a great idea! But, for some edge cases, it will bring some harm. I will update the answer. – Ismael Miguel Jan 15 '15 at 10:00
  • @MorganFeeney: Seems like it is default in browser or twitter-bootstrap -- although I don't see such rule in firebug css of these images. – Adobe Jan 15 '15 at 10:02
  • @Adobe Browsers internally assume a set of styles. `height:auto;` is the default for the images. If no height is set, the image will have the original height and will scale according to the width. – Ismael Miguel Jan 15 '15 at 10:03
  • @IsmaelMiguel's updated answer uses it for consistency. When I scale images in the browser, whilst retaining their aspect ratio I always set the width value, and give the height a value of auto. I have never seen an adverse effect. In cases where there are problems it usually relates to a height attribute on the img element. So just get rid. – Morgan Feeney Jan 15 '15 at 10:19
  • @IsmaelMiguel: I also have to re-inforce vertical-align: `this.style.verticalAlign = "middle";` – Adobe Jan 15 '15 at 11:59
  • @Adobe That is more layout-specific, but I have added that to the answer. – Ismael Miguel Jan 15 '15 at 12:17