Gah, need a javascript image resizing script.

This is a discussion on "Gah, need a javascript image resizing script." within the JavaScript Forum section. This forum, and the thread "Gah, need a javascript image resizing script. are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 21st, 2007, 09:08
Junior Member
Join Date: Apr 2007
Location: London, England
Age: 22
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to JasonStanley Send a message via Skype™ to JasonStanley
Gah, need a javascript image resizing script.

I swear JavaScript is the next thing on my list to learn. It doesn't look to hard, just a case of finding the time.

For once Google has failed me.

I need the following script. If someone could convert this into javascript I would be enternally greatful. Should be pretty easy for someone with basic knowledge of JavaScript


imgx = get image width
imgy = get image height

// Desired width and hight

desimgx = 160px
desimgy = 240px

*if imgx > desimgx*
difference = imgx - desimgx
decimalpercent = difference / desimgx

imagewidth = round down(imgx * decimalpercent)
imageheight = round down(imgy * decimalpercent)

*end if and repeat with height*

Return imagewidth and height.


That should work, if someone could write this up or has a better way of doing it then please help me out. Thanks alot.
Reply With Quote

  #2 (permalink)  
Old Oct 21st, 2007, 12:47
marSoul's Avatar
Moderator
Join Date: Sep 2007
Location: Tehran - Iran
Age: 28
Posts: 411
Blog Entries: 2
Thanks: 4
Thanked 4 Times in 4 Posts
Send a message via MSN to marSoul Send a message via Yahoo to marSoul
Re: Gah, need a javascript image resizing script.

i hope i understand what u want, here is a code :

HTML: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function changeSize() {
    // get current image sizes
    imgx = document.getElementById("pic").offsetHeight;
    imgy = document.getElementById("pic").offsetWidth;
    desimgx = 160;
    desimgy = 240;
    if (imgx > desimgx) {
        difference = imgx - desimgx;
        decimalpercent = difference / desimgx;
        imagewidth = Math.round(imgx * decimalpercent);
        imageheight = Math.round(imgy * decimalpercent);
        document.getElementById("pic").style.height = imageheight + "px";
    } else {
        document.getElementById("pic").style.height = desimgx + "px";
        }
    if (imgy > desimgy) {
        difference = imgy - desimgy;
        decimalpercent = difference / desimgy;
        imagewidth = Math.round(imgx * decimalpercent);
        imageheight = Math.round(imgy * decimalpercent);
        document.getElementById("pic").style.width = imagewidth + "px";
    } else {
        document.getElementById("pic").style.width = desimgy + "px";
        }
}
</script>
</head>
<body>
<img id="pic" src="image.jpg" width="160" height="200"  />
<label>
<input type="submit" onclick="changeSize()" id="button" value="Change" />
</label>
</body>
</html>
__________________
Designing For Communicating
Website : http://www.datisdesign.com
Weblog : http://blog.datisdesign.com

Last Blog Entry: Throughout IRAN (Dec 10th, 2007)
Reply With Quote
  #3 (permalink)  
Old Oct 21st, 2007, 13:27
Junior Member
Join Date: Apr 2007
Location: London, England
Age: 22
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to JasonStanley Send a message via Skype™ to JasonStanley
Re: Gah, need a javascript image resizing script.

Thanks a lot, when I am through with what I am doing I will give it a run out.
Reply With Quote
  #4 (permalink)  
Old Nov 6th, 2007, 18:51
Reputable Member
Join Date: Nov 2007
Location: India
Posts: 150
Blog Entries: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Gah, need a javascript image resizing script.

I have showcased my image resizing script at http://javascript.biologyformhtcet.c...ize_image.html
check if its the one you are looking for!
Last Blog Entry: Cross browser nuisance (Feb 11th, 2008)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Awesome Image Resizing! Jack Franklin Webforumz Cafe 3 Jan 19th, 2008 10:06
image not resizing in Safari cyberseed Other Programming Languages 0 Jul 30th, 2007 16:39
Firefox image resizing problem with CSS paishin Web Page Design 2 Jun 15th, 2007 13:29
Resizing an image using HTML Powderhound Web Page Design 12 Sep 4th, 2006 02:31


All times are GMT. The time now is 04:53.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43