Resize images above max width - javascript

JavaScript would probably do it, I have searched but found nothing to do with maxWidth. Only resizing images out of the blue.
What I need is a piece of JavaScript code to resize an image if it's too big(defined in a variable.)

Is there some reason why you can't use the max-width CSS property to do what you want, like:
<img src="myImage.png" style="max-width: 600px;">
...or even better:
<style>
.widthConstrained {
max-width: 600px;
}
</style>
...
<img src="myImage.png" class="widthConstrained">
Edit:
If you must use JavaScript for compatibility reasons, I suggest that you use something like the code specified in this question to check the total image width after it loads, and clamp it down if needed, roughly like:
var img = $("img")[0]; // Get my img elem
$("<img/>") // Make in memory copy of image to avoid css issues
.attr("src", $(img).attr("src"))
.load(function() {
if (this.width > maxWidth) {
$(img).width(maxWidth);
}
});

I take it you have a <img /> tag in your webpage.
To shrink images that are too large, simply insert the width and height attribute, along with your image source and a alt, along with a ID.
<head>
<script type="text/javascript" />
function pie()
{
var pathToImage = "bla/bla/Image.jpg";
document.GetElementByID(Image1).setAttribute("src",pathToImage);
}
</script>
</head>
<body onload=Pie() >
<img ID="Image1" alt="random Image" width="300px" Height="200px" />
</body>

Related

Chrome: Changing img src repeatedly causes chrome to request image again?

Same title. I'm not sure if this is a browser bug or behavior, see below example:
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
var srcs = [
'https://i.stack.imgur.com/sIH1O.jpg?s=48&g=1',
'https://www.gravatar.com/avatar/9aa2aa68144b2781ac025a29c83bbdd4?s=32&d=identicon&r=PG',
'https://www.gravatar.com/avatar/c0377fc1b08293d3362611978913a32b?s=32&d=identicon&r=PG',
'https://www.gravatar.com/avatar/07f36acea2b942f61102221f92f62f6e?s=32&d=identicon&r=PG',
//
// 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALgAAAA8CAYAAADVEnAJAAAOvklEQVR42u1dCZBcRRluyM4Sbg9QuUTFYAhy7Zs3G2Nw5r2ZTWKMWBCXQ5QzIncEFIqjGGtnZpdwaEUOIYcFlBwVRBA5wh7hUIIQCF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dTxOCfCU6RnolmYM+Z3u/nu/eKq/EW0d2o9NHpRre18Nk1JqDViChW33s0sq0FirZJllpy0skvjEjx+fCJ5WVWJb9OvIJvMsEj3h4UxmFNPL+YkAHoevprGUu0tV/U9cXIeYVlTLdm+IlRceaUEB5Mfbc2Wx+/RFgLfwSG7+l9If4pVmxaeoly9z1LBy2zm7R72atm0hqWd6hvibfVTtLEg94vbPLfnGMDHBU/qvI9U0vMSctiSD7Bol8P42ihqUTJ4JQlKgw18SkqJD72b0EcXrujPU2/4h6X4eW2VMVXHW05IC1G75VA9wNuc/tIAfv1BVhigHToRodffSC6Vx5LX+HPGVYOYLS+UkG1j4kgh+rO2geFzvLqeC52EpQO9VpO1PUxQVMcpkioWnmjql27jtjCP8rsgXPBbIBPSSJE1SWVYpa30JhzGcUw5E3mSEgy5mYGYkO5cLSFf2glPVjdwI1lrzXXcn8Pz029uu5dHqurtWoe4tXJDWWK8xblqJoYmnlNRAxMdXC8QjMdQjLIPiIyjlSubJJTMOVQvGazU8pRMo+Fy3lt9GjPPq3/ABI9Uety4aRGrbDbtL2vRLNN/TrTUVMsNXVz4SaZlmzhwgESjkLluTuPTM6mVlOSLjBMSp5M/mVLjyGw28z5NaEnsTdqmw6wl+KGGaojfMbcgOAQCfuR0SYUBLl8sQP11UuonKnrDEk+20OGuqXw/FRSxybYmLHCE4IPBH89VQjxEW06Xlm0a1HVpJNLWrJJOUj2bmck8Ae2OMY/QddHQI7eUiDQ1DLDTPGs8ADo8ZIJIwwYEc/fOf1x1zwrwPul5uXcCGi0zfbjd7zYNJUslo07RVtRJPHYqUSrPJSwhvpTyMrryTtKrnaigIwGkI8JU+nJaDVgpGE0lJQu4jSQHdgoHDbvzDc2Bk8Y/bpY6HztxbIf9fQvVNLBFVQRujbDtV43wxP8MP8Azqxw5fiIhuYNI//Z',
];
var $x = $('#x');
var i = -1;
setInterval(function() {
++i;
if(i >= 4) {
i = 0;
}
$x.attr('src', srcs[i]);
}, 100);
});
</script>
</head>
<body>
<img id="x" width="50" height="50">
</body>
</html>
Every few random seconds, the images are requested again, although fetched from disk cache, but request is happening. If you resize window, the requests may happen even more frequently.
Check JS Fiddle
I tried using base64 images instead of URLs, still same thing. This is an issue because the visible image may glitch/flicker when request occurs.
EDIT
How to prevent chrome from requesting the same image again?
This is the right behavior as "Memory Cache" stores and loads resources to and from Memory (RAM). So this is much faster but it is non-persistent. Content is available until you close the Browser.
"Disk Cache" is persistent. Cached resources are stored and loaded to and from disk.
For more info on how Chrome caches works (memory and disk), please refer to this link.
Placing multiple imgs, hide all and show only 1 at a time, prevents Chrome from requesting same images. With this approach, each image is requested only once.
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
var i = -1;
setInterval(function() {
++i;
if(i >= 4) {
i = 0;
}
$('.img').hide();
$('#img' + i).show();
}, 100);
});
</script>
</head>
<body>
<!-- using multiple images with display: none, src is fixed in HTML (not changed by JS) -->
<img id="img0" class="img" width="50" height="50" style="display: none;" src="https://i.stack.imgur.com/sIH1O.jpg?s=48&g=1">
<img id="img1" class="img" width="50" height="50" style="display: none;" src="https://www.gravatar.com/avatar/9aa2aa68144b2781ac025a29c83bbdd4?s=32&d=identicon&r=PG">
<img id="img2" class="img" width="50" height="50" style="display: none;" src="https://www.gravatar.com/avatar/c0377fc1b08293d3362611978913a32b?s=32&d=identicon&r=PG">
<img id="img3" class="img" width="50" height="50" style="display: none;" src="https://www.gravatar.com/avatar/07f36acea2b942f61102221f92f62f6e?s=32&d=identicon&r=PG">
</body>
</html>
JS Fiddle

call on function when all images are clicken on

I want to call a function that opens a new link. I want to do this after all my images have been clicked on. How can i achieve this?
HTML
<!DOCTYPE HTML>
<html>
<head>
<meta charset="windows-1252">
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"> </script>
<![endif]-->
<title>Date med mig - version b
</title>
<script type="text/javascript">
function newWindow()
{
window.location = window.location = "http://www.tv3.se/datemedmig"
}
</script>
</head>
<body>
<h1>Del 2: Date med mig</h1>
<img src="1_c.jpg" id="image1" onclick="this.src='1_o.jpg';" alt="image" />
<img src="2_c.jpg" id="image1" onclick="this.src='2_o.jpg';" alt="image" />
<img src="3_c.jpg" id="image1" onclick="this.src='3_o.jpg';" alt="image" />
<img src="4_c.jpg" id="image1" onclick="this.src='4_o.jpg';" alt="image" />
</body>
</html>
If you are trying to just load a new window as soon as any image is clicked, I think the way that #runfaj said to do it is probably the best, It is my understanding that you want to do it only after ALL images have been clicked on?
If this is the case, here's one option (which is probably not the most efficient)
//Assuming there are 4 images
var hasBeenClicked = [false,false,false,false]
//To be called when each image is clicked
function imageClicked(whichImage){
this.src = "'"+whichImage+"_o.jpg'"
//set this position in the array equal to true
hasBeenClicked[whichImage] = true
// if the array is completely true (every image has been clicked)
// call the newWindow function
if(hasBeenClicked == [true,true,true,true]){
newWindow();
}
}
Then when you create an image:
<img src="1_c.jpg" id="image1" onclick="imageClicked(1);" alt="image" />
you could do it three ways:
jquery:
$('img').click(newWindow);
inline:
<img src=..... onclick="newWindow()" />
regular javascript:
get all the tags with:
var imgs = document.getElementByTagName('img');
then add an event listener to each one (google it for lots of different examples)
Have an onclick function that triggers for each image which sets a new attribute on the image tag. Then run a loop over all images to check that they all have the tag. If so, open new windows. For example:
$('img').click(function() {
$(this).data('was-clicked', true);
var all_clicked = true;
$('img').each(function() {
if($(this).data('was-clicked') != true) {
all_clicked = false;
break;
}
});
if(all_clicked == true) newWindow();
});
Note, this code is not tested and may have syntax/semantic errors. Also, you may not really want the user to click ALL image tags (e.g. banners, backgrounds, etc), so you should modify the jquery selectors to filter only the 'img' tags you're interested in, e.g. $('#my-gallery img')...

Why the difference in displaying my webcam image across browsers?

I have an IP webcam and upload an image to a weather service. I then capture that image to display in my webpage. In order to do that I searched for some code that would refresh the image without refreshing the page. I found some code (sorry, I don't know who to acknowledge) that I adapted ……..
<script>
var reimg
window.onload=function () {
reimg=document.getElementById('re')
setInterval(function () {
reimg.src=reimg.src.replace(/\?.*/,function () {
return '?'+new Date()
})
},60000)
}
</script>
The next bit is, to a great extent, probably overkill but I have been trying to get it to work (how I want it to look) on all browsers...
<iframe frameborder=0 marginwidth=0 marginheight=0 border=0 width="360" height="270" style="overflow:
hidden;border:0;margin:0;width:360px;height:270px;max-width:100%;max-height:100%"
src="http://icons.wunderground.com/webcamramdisk/g/l/Gluepack/1/current.jpg?" id="re" scrolling="no"
allowtransparency="true">If you can see this, your browser doesn't understand IFRAME. However, we'll
still link
you to the file.</iframe>
It works fine in Firefox, reducing the 640 x 480 original image to fit. However, in IE and Chrome only the centre top part of the image is shown (i.e. the image is not reduced to fit). Is there any additional code required to ensure it displays correctly (i.e. how I want it, reduced to fit) in IE and Chrome or is it not possible?
How images are displayed is really up to the browser, there is not standard for that. The easiest solution would be to create an HTML file that contains the javascript and a <img> tag for the image instead of loading the image directly into the iframe:
<!DOCTYPE HTML>
<html><head>
<script>
window.onload = function() {
var reimg = document.getElementsByTagName('img')[0];
setInterval(function () {
reimg.src = reimg.src.replace(/\?.*/, function () {
return '?'+new Date()
});
}, 60000);
};
</script>
<style type="text/css">
* { margin: 0; padding: 0; }
img { width: 360px; height: 270px; }
</head><body>
<img src="http://icons.wunderground.com/webcamramdisk/g/l/Gluepack/1/current.jpg?">
</body></html>
Save this as "webcam.html" or whatever:
<iframe frameborder=0 marginwidth=0 marginheight=0 border=0
width="360" height="270" style="overflow: hidden; border:0;
margin:0; width:360px; height:270px; max-width:100%; max-height:100%"
src="webcam.html" id="re" scrolling="no"
allowtransparency="true">...</iframe>

Why don't auto height work on images?

What I am trying to accomplish is that, when you put an image on 100% it nicely scales the height accordingly. I like to catch that height and process it.
<div id="view" style="width:950px;">
<img src="1.png" />
</div>
The image is 950x500pixels. However when I ask the view $( '#view' ).height() what the height is, it returns 16pixels. Does anyone know why it does this? Why doesn't it return 500pixels as that's the size of the image.
You need image to be loaded first. Try this:
<!DOCTYPE HTML>
<html>
<style>
div { width: 950px; }
img { width: 100%; }
</style>
<body>
<div>
<img src="1.png">
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
$('img').load(function() {
var height = $('div').height();
console.log(height);
});
</script>
</body>
</html>
I have test to alert the size of <Div> It's return the valid value, that return the size of image.
But from your code $( 'view' ).height() I have change to $( '#view' ).height();
Here is my code it's return correctly.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function loaded() {
var height = $( '#view' ).height();
alert(height);
}
</script>
</head>
<body onload="loaded();">
<div id="view" style="width:950px;">
<img src="Desert.jpg" />
</div>
</body>
</html>
Please do not use local image, you can use an image with URL, like "http://www.veryued.org/wp-content/uploads/2012/02/less-online.png".
Please read jQuery API carefully:
Caveats of the load event when used with images:
It doesn't work consistently nor reliably cross-browser
It doesn't fire correctly in WebKit if the image src is set to the same src as before
It doesn't correctly bubble up the DOM tree
Can cease to fire for images that already live in the browser's cache

On site load, how do you wrap all images in a link dynamically using Javascript?

I have barely any experience working with DOM and using Javascript, and I have a very specific task that I'm trying to accomplish.
Let's say I have an image in my HTML:
<img src="foo.jpg" />
When the site loads, I want to take that image (all images in the document, actually), and wrap them in a link:
<img src="foo.jpg" />
What could I use to accomplish this? Google hasn't turned up much for me with regards to this specific task. On load, I can access and iterate all the images in the document... but I'm not sure where to go from there in order to wrap the image in a link.
You could try something among the lines:
window.onload = function() {
var images = document.getElementsByTagName('img');
for (var i = 0, image = images[i]; i < images.length; i++) {
var wrapper = document.createElement('a');
wrapper.setAttribute('href', 'http://www.foobar.com');
wrapper.appendChild(image.cloneNode(true));
image.parentNode.replaceChild(wrapper, image);
}
};
I would recommend you using the excellent jQuery library to manipulate the DOM:
$(function() {
$('img').wrap('<a href="http://foo.com">');
});
In the following example, all images become wrapped in a link to their source. So if an image has the src of //example.com, it will be wrapped with an anchor(link) to //example.com.
Of course you don't want some of the images linked so you can add the data attribute nolink, like so:
<img src="//lorempixel.com/400/200 data-nolink />
<!DOCTYPE html>
<html lang="en-GB">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <!-- Include jQuery 1.11.1 -->
<script>
$(function() { //Run once DOM is ready
$('img:not([data-nolink])').each(function() { //Iterate through each img element
$(this).wrap('<a href="' + $(this).attr('src') + '">'); //Wrap with link to own src
});
});
</script>
<style>
h3 {
text-align: justify;
}
img {
width: 200px;
margin: 24px;
}
</style>
</head>
<body>
<h3>These images are generated randomly by loremPixel; So you will be presented by a different image to what you see here, when you click on any of the links </h3>
<hr />
<br />
<img src="//lorempixel.com/400/200?samp1" />
<img src="//lorempixel.com/400/200?samp2" />
<img src="//lorempixel.com/400/200?samp3" data-nolink/> <!-- Add data-nolink to prevent auto-linking -->
<img src="//lorempixel.com/400/200?samp4" />
<img src="//lorempixel.com/400/200?samp5" />
<img src="//lorempixel.com/400/200?samp6" />
</body>
</html>

Categories

Resources