473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Shrinking and Centering an image of unknown size

cjl
Hey all:

I have a div of a known size, 672px X 672px. In it I will display
images that are of unknown size. I have two simple functions that
shrink and center the image:

function shrink()
{
if (document.getElementById('right').height >=
document.getElementById('right').width)
{
var newwidth = (( document.getElementById('right').width * 672) /
document.getElementById('right').height);
document.getElementById('right').height = 672;
document.getElementById('right').width = newwidth;
}
else
{
var newheight = (( document.getElementById('right').height * 672) /
document.getElementById('right').width);
document.getElementById('right').width = 672;
document.getElementById('right').height = newheight;
}
}

function center()
{
if ( document.getElementById('right').height > 672)
{
document.getElementById('right').style.top = '-' +
(Math.abs(document.getElementById('right').height - 672)/2) + 'px';
}
else
{
document.getElementById('right').style.top =
(Math.abs(document.getElementById('right').height - 672)/2) + 'px';
}

if ( document.getElementById('right').width > 672)
{
document.getElementById('right').style.left = '-' +
(Math.abs(document.getElementById('right').width - 672)/2) + 'px';
}
else
{
document.getElementById('right').style.left =
(Math.abs(document.getElementById('right').width - 672)/2) + 'px';
}
}
So, when I want to display my first image, I simply:

document.getElementById('right').src = cr_images[0][0].src;
shrink();
center();

This works. However, when I want to change the image to another image
with a different size, it uses the previous images height and width. In
the html of the page I have an img tag which sources a 1px gif, without
specifying the height or width.

Is there some way to 'reset' the height and width, or read it from
something like document.getElementById('right').src.height?

Help?

-CJL

Jul 23 '05 #1
2 2127

Yikes, you don't need js for most of that, just CSS, absolutely
positioned the IMG in the div, make the div position: relative; so it
becomes the parent and the child IMG element uses its dimensions as
references for its properties, and then just center the IMG per
vertically/horizontally by using its height/width, position the IMG say,
left by 50%, and top 50%, so it starts in the middle of the div, then give
it negative margin-left and margin-top of HALF the IMG width/height
respectively. Now, you don't know the IMG dimensions, for that, you can
use js:

var myimg=new Image(); myimg.src='SOMEFILEHERE.png'; //loads the file to
the .src and at the same time, gets a myimg.width and myimg.height from
the file itself, and from there, you stick it to the positioned IMG style,
by document.images['MYHOLDERORSOMETHING'].style.width=myimg.width+'px';
//yes, you need to specify a unit, so you need 'px'.
Danny

On Wed, 29 Jun 2005 10:59:50 -0700, cjl <cj****@gmail.com> wrote:
Hey all:

I have a div of a known size, 672px X 672px. In it I will display
images that are of unknown size. I have two simple functions that
shrink and center the image:

function shrink()
{
if (document.getElementById('right').height >=
document.getElementById('right').width)
{
var newwidth = (( document.getElementById('right').width * 672) /
document.getElementById('right').height);
document.getElementById('right').height = 672;
document.getElementById('right').width = newwidth;
}
else
{
var newheight = (( document.getElementById('right').height * 672) /
document.getElementById('right').width);
document.getElementById('right').width = 672;
document.getElementById('right').height = newheight;
}
}

function center()
{
if ( document.getElementById('right').height > 672)
{
document.getElementById('right').style.top = '-' +
(Math.abs(document.getElementById('right').height - 672)/2) + 'px';
}
else
{
document.getElementById('right').style.top =
(Math.abs(document.getElementById('right').height - 672)/2) + 'px';
}

if ( document.getElementById('right').width > 672)
{
document.getElementById('right').style.left = '-' +
(Math.abs(document.getElementById('right').width - 672)/2) + 'px';
}
else
{
document.getElementById('right').style.left =
(Math.abs(document.getElementById('right').width - 672)/2) + 'px';
}
}
So, when I want to display my first image, I simply:

document.getElementById('right').src = cr_images[0][0].src;
shrink();
center();

This works. However, when I want to change the image to another image
with a different size, it uses the previous images height and width. In
the html of the page I have an img tag which sources a 1px gif, without
specifying the height or width.

Is there some way to 'reset' the height and width, or read it from
something like document.getElementById('right').src.height?

Help?

-CJL


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #2
Are you just setting the .src of the image to change images? I would try
using some dynamic html to render the new image instead. That way it should
be a new object.

function newImage(divname, imagesrc) {
var output = "<img src='" + imagesrc + "'>";
if (document.all)
document.all(divname).innerHTML = output;
else if (document.layers) {
document.layers[divname].document.open();
document.layers[divname].document.writeln(output);
document.layers[divname].document.close();
}
else if (!document.all && document.getElementById)
document.getElementById(divname).innerHTML = output;
}

You will probably have to modify your other two functions a bit to be able
to target the new image more dynamically. I'm not sure if you can keep
naming it "right" without causing a problem. Perhaps referencing the image
using document.images[x] (where x is the index of the targeted image) could
simplify things.

Hope this helps,
Dominic
"cjl" <cj****@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hey all:

I have a div of a known size, 672px X 672px. In it I will display
images that are of unknown size. I have two simple functions that
shrink and center the image:

function shrink()
{
if (document.getElementById('right').height >=
document.getElementById('right').width)
{
var newwidth = (( document.getElementById('right').width * 672) /
document.getElementById('right').height);
document.getElementById('right').height = 672;
document.getElementById('right').width = newwidth;
}
else
{
var newheight = (( document.getElementById('right').height * 672) /
document.getElementById('right').width);
document.getElementById('right').width = 672;
document.getElementById('right').height = newheight;
}
}

function center()
{
if ( document.getElementById('right').height > 672)
{
document.getElementById('right').style.top = '-' +
(Math.abs(document.getElementById('right').height - 672)/2) + 'px';
}
else
{
document.getElementById('right').style.top =
(Math.abs(document.getElementById('right').height - 672)/2) + 'px';
}

if ( document.getElementById('right').width > 672)
{
document.getElementById('right').style.left = '-' +
(Math.abs(document.getElementById('right').width - 672)/2) + 'px';
}
else
{
document.getElementById('right').style.left =
(Math.abs(document.getElementById('right').width - 672)/2) + 'px';
}
}
So, when I want to display my first image, I simply:

document.getElementById('right').src = cr_images[0][0].src;
shrink();
center();

This works. However, when I want to change the image to another image
with a different size, it uses the previous images height and width. In
the html of the page I have an img tag which sources a 1px gif, without
specifying the height or width.

Is there some way to 'reset' the height and width, or read it from
something like document.getElementById('right').src.height?

Help?

-CJL

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ryan W Sims | last post by:
I'm having trouble with centering in IE... http://www.ryanwsims.com/koh/ The image should center over the text. It does in Firebird, but not in IE for some reason. If you look at ...
1
by: jeffreyv | last post by:
Hi! I'm studying to have my MCSE 70-228 certification and I'm trying some things with backing up transaction logs and shrinking it. Here's what I do: There is no activity in the database by...
1
by: Mark | last post by:
Hi all, I have a report that prints quotations with images of the stock item if there is an image. images are externally stored. My problem is that when there is an image to display and the...
6
by: Jerry Camel | last post by:
I want to center an image on the screen and have text centered over the image. What's the best way to do this? I've been playing with style tags and I can get the stuff to center automatically...
8
by: Cardman | last post by:
I am hopeful that someone can quickly solve my image alignment issue when things are just not going right and where my attempts to solve this alignment issue have all failed. First of all take a...
3
by: John Pote | last post by:
1. Horizontal centering a <divin browser window. The current trend seems to be to place page content in a fixed width area in the middle of the browser window. How is this achieved? If I use a...
2
by: rudicheow | last post by:
SHORT VERSION ============= I have a bunch of identical fixed-size single-celled tables that rest against each other horizontally thanks to "float:left". These tables are dynamically generated...
2
by: tonsi | last post by:
I have a page but it wont center the div. Can anyone take a look at it and help. When I try to center in the body using text-align: center; it doesn't work with the main container (called Table_01)....
16
by: stevedude | last post by:
CSS newbie again. I have a problem trying to get coffee mug images within anchor tags to center with my link text for a vertical list menu. If I use the horizontal/vertical properties of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.