Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

display image but do not resize

Posted on 2011-02-25
11
234 Views
Last Modified: 2012-05-11
many different image sizes  some have alot of width, some have alot of height


want image to be shown as max 100px width and max 100px height

without resizing image
0
Comment
Question by:rgb192
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34982899
<script >
function fixSize(what)
{
	var maxwh = (what.width > what.height) ? what.width : what.height;
	if (maxwh > 100 )
	{
		var scaleby = maxwh / 100;
		what.width /= scaleby;
		what.height /= scaleby;
	}
}
</script>
<img src=myimage.jpg onload="fixSize(this)">

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34983068
Do you mean to crop the image to 100x100 ?
0
 
LVL 7

Expert Comment

by:DiscoNova
ID: 34984770
Disregarding the fact that you appear to be contradicting yourself in the question ;) you could try something like this:

<div style="background-image:url(image.png);max-width:100px;max-height:100px"></div>

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:onemadeye
ID: 34988230
... want image to be shown as max 100px width and max 100px height without resizing image.

How about defining the width and height straight to the <img> tag like this :
<img src="picture.jpg" width="100" height="100" alt="" />

Open in new window

0
 

Author Comment

by:rgb192
ID: 34997930
<div style="background-image:url(image.png);max-width:100px;max-height:100px"></div>

is there a way to do this without a div tag

maybe just use an image tag

because I cant put the div in the correct
0
 
LVL 7

Expert Comment

by:DiscoNova
ID: 34998046
You can set style for <img>-tag also, but I must say that I'm not entirely certain if it will work (if you use <div> with background-image, it essentially "clips" the overflow, with <img> it might be a bit different). However, if you wish to achieve the same as with <div>, you could try something like:

<img src="transparent.png" width="100" height="100" style="background-image:url(image.png);max-width:100px;max-height:100px">

Open in new window


Without understanding exactly what it is you're trying to achieve it's a bit of guesswork.

However, using the max-width/height in style actually does "cropping" (which I think is what you might be trying to achieve). The code provided above works by taking a "dummy" transparent image and showing it as 100x100 pixels and putting the "actual" image into background. But as said, I'm not sure if this is what you're trying to do...
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34998116
If I understand the question right, I believe my Javascript is still the only solution that satisfies all these requirements:

(1) Don't resize images that are already smaller than 100px in their largest dimension

(2) Resize larger images proportionally

(3) Resize based on the largest dimension, so that both width and height are <= 100px.

(3) Don't crop - resize
0
 
LVL 7

Expert Comment

by:DiscoNova
ID: 34998189
As you say "(3) Don't crop - resize" whereas the question stated "without resizing image" ... as far as I can fathom, the only way to limit size of image without resizing it would be to crop it :)

However, I just noticed that we are on PHP-area, so perhaps we should be providing solutions in PHP rather than client-side scripting? However - doing that would also require a bit further understanding of what the original author wants to do; crop or fit the whole image within a 100x100 pixels area (I think he may actually wish to resize the image without distorting it's dimensions - for which your JavaScript solution could quite easily be adapted for use with PHP/GD instead).
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34998270
It seems pretty clear to me.

"without resizing image" means that they do not want to have to mess about with the image files that are already stored on their web server, so the resize has to be done by the client's browser.

The question was probably asked in the PHP zone because they're using PHP to generate the web page, so the answer describes some of the HTML that should be spit out by their PHP program.
0
 
LVL 7

Expert Comment

by:DiscoNova
ID: 34998398
I generally try to avoid assumptions (it's good when they are correct, but if the assumptions are wrong, they may lead one to entirely the wrong track) ... that is why I would like the author to clarify his needs.
0
 

Author Closing Comment

by:rgb192
ID: 35031826
yes you are correct.  I didnt understand the javascript at first.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question