/    Sign up×
Community /Pin to ProfileBookmark

Dynamically resize images

This question has been asked a hundred times, but I haven’t been able to find the perfect solution, -yet.

I have a page where i load pictures dynamically when user clicks <- or -> buttons, with some simple jQuery.

[CODE]
$.post(‘/v2/_ajax/loadImage.php’, {
[INDENT] event_id: event_id, image_id: image_id,[/INDENT]
}, function(data) {
[INDENT] target.html(data);[/INDENT]
});
[/CODE]

What i need, is a way for the picture to be resized, if it exceeds 900px in width, OR if the height is bigger than the browser – 60px.

I have tried the jQuery plugin MaxImage ([url]http://www.aaronvanderzwan.com/maximage/[/url]) but it has some very strange behaviour, and it only resizes when you resize the browser, not when the image is loaded.

Is there any good solution?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@savvykmsAug 04.2009 — You could check each image element on the page and simply set the CSS/HTML properties for width/height via Javascript.

[code=html]var img = document.getElementById('someimage');
if (img.width > 900) { document.getElementById('someimage').width = 900; }[/code]
×

Success!

Help @b3nj4m1n spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...