473,804 Members | 3,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEl ementById('righ t').height >=
document.getEle mentById('right ').width)
{
var newwidth = (( document.getEle mentById('right ').width * 672) /
document.getEle mentById('right ').height);
document.getEle mentById('right ').height = 672;
document.getEle mentById('right ').width = newwidth;
}
else
{
var newheight = (( document.getEle mentById('right ').height * 672) /
document.getEle mentById('right ').width);
document.getEle mentById('right ').width = 672;
document.getEle mentById('right ').height = newheight;
}
}

function center()
{
if ( document.getEle mentById('right ').height > 672)
{
document.getEle mentById('right ').style.top = '-' +
(Math.abs(docum ent.getElementB yId('right').he ight - 672)/2) + 'px';
}
else
{
document.getEle mentById('right ').style.top =
(Math.abs(docum ent.getElementB yId('right').he ight - 672)/2) + 'px';
}

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

document.getEle mentById('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.getEle mentById('right ').src.height?

Help?

-CJL

Jul 23 '05 #1
2 2147

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='SOME FILEHERE.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['MYHOLDERORSOME THING'].style.width=my img.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.c om> 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.getEl ementById('righ t').height >=
document.getEle mentById('right ').width)
{
var newwidth = (( document.getEle mentById('right ').width * 672) /
document.getEle mentById('right ').height);
document.getEle mentById('right ').height = 672;
document.getEle mentById('right ').width = newwidth;
}
else
{
var newheight = (( document.getEle mentById('right ').height * 672) /
document.getEle mentById('right ').width);
document.getEle mentById('right ').width = 672;
document.getEle mentById('right ').height = newheight;
}
}

function center()
{
if ( document.getEle mentById('right ').height > 672)
{
document.getEle mentById('right ').style.top = '-' +
(Math.abs(docum ent.getElementB yId('right').he ight - 672)/2) + 'px';
}
else
{
document.getEle mentById('right ').style.top =
(Math.abs(docum ent.getElementB yId('right').he ight - 672)/2) + 'px';
}

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

document.getEle mentById('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.getEle mentById('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(divnam e, imagesrc) {
var output = "<img src='" + imagesrc + "'>";
if (document.all)
document.all(di vname).innerHTM L = output;
else if (document.layer s) {
document.layers[divname].document.open( );
document.layers[divname].document.write ln(output);
document.layers[divname].document.close ();
}
else if (!document.all && document.getEle mentById)
document.getEle mentById(divnam e).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.c om> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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.getEl ementById('righ t').height >=
document.getEle mentById('right ').width)
{
var newwidth = (( document.getEle mentById('right ').width * 672) /
document.getEle mentById('right ').height);
document.getEle mentById('right ').height = 672;
document.getEle mentById('right ').width = newwidth;
}
else
{
var newheight = (( document.getEle mentById('right ').height * 672) /
document.getEle mentById('right ').width);
document.getEle mentById('right ').width = 672;
document.getEle mentById('right ').height = newheight;
}
}

function center()
{
if ( document.getEle mentById('right ').height > 672)
{
document.getEle mentById('right ').style.top = '-' +
(Math.abs(docum ent.getElementB yId('right').he ight - 672)/2) + 'px';
}
else
{
document.getEle mentById('right ').style.top =
(Math.abs(docum ent.getElementB yId('right').he ight - 672)/2) + 'px';
}

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

document.getEle mentById('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.getEle mentById('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
2244
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 http://www.ryanwsims.com/sd.php everything's fine (i.e. centered)
1
1636
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 the way. I have a transaction log of 1792 kb...
1
2564
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 description text box which is to the left of the image is more then one line of text(can grow = true), the report section increases in size and leaves a white space between the next record below. If there is an image to display with only one line of...
6
2325
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 OR have the text appear on top of the image. But in the second case, the image and text aren't centered properly. Any help is appreciated... Jerry
8
2348
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 look here... http://www.cardman.com/switches.html In MIE this page looks perfect. Unfortunately both Netscape and Opera show these item photos in the wrong place. For some *unknown* reason all the photos drop down to align with the green...
3
4214
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 top level <divthen I can place it with CSS attribute 'left:', but this is a fixed offset. Is it possible to have a <divcentered in the browser window? 2. Verticle centering in <div>
2
3068
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 and the number of them can vary greatly. They are all contained within a parent table cell, which is centred on the page. But I am unable to find a way of centering these table cells within the parent cell.
2
2234
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). Please help! I would be ever so greatful. The page is located here: http://www.cpiequipment.com/test_site/index.html Here is the code: <html> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
16
4942
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 "background" or "background-image", the positioning only works with specifying pixels. If I specify the vertical position in pixels, the image gets cut-off at the bottom. I don't know what to do and would appreciate anyone's help. Specifically the code...
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9582
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9157
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7621
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.