How to set fancybox height and width identical?

How can I resize an image to a set width and height while maintaining its aspect ratio?

  • For a real estate website am developing, the user uploads an image of any dimension and then I have to resize it to a fixed width and height in order to display it on the website. I am looking for algorithm ideas on how I can resize the images while maintaining its aspect ratio

  • Answer:

    Instead of trying to guess and doing a bad job, ask the user. As soon as user uploads the photo, ask them how they want the image cropped. Show them the same photo, superimposed with a cropping border of the correct aspect ratio. Allow them to resize the cropping border (without changing the aspect ratio). Once they confirm, use that for cropping. This can be done easily using javascript. ( suggests using Jcrop: http://deepliquid.com/content/Jcrop.html) (Updates to answer based on comments below) This approach can be combined with any of the other approaches suggested for autocropping heuristics - so you guess a crop, and then show it to the user for confirmation. In case you have multiple photos you can show an intermediate page with all the photos and the guessed crops, and allowing them to change any that have a bad crop. The hope would be that in most cases they just glance over the page and click OK because the guessed crops would be fine in most cases. Once in a while they might modify one or two of the crops. (Except for a few finicky users who will adjust the crop for each and every photo; and these users will love this feature).

Navin Kabra at Quora Visit the source

Was this solution helpful to you?

Other answers

There is likely a much more elegant way to do this, but what I've always done is something along these lines: 1) Let's assume that availableWidth and availableHeight define the target (fixed) area that you're resizing against. 2) Let's assume that imageWidth and imageHeight represent the CURRENT width and height of your uploaded image at any given point in time. 3) First, do an aspect-ratio-constrained resize locked to the value of availableWidth. This is done by adjusting the height of the uploaded image first, then setting the width: imageHeight = imageHeight x (availableWidth / imageWidth) imageWidth = availableWidth 4) The previous step assumes that the image is landscape-oriented (hence using width as the locking value) but it may not be, so we make a quick check to see if the height of the image exceeds our target height: if(imageHeight > availableHeight) 5) If this is the case, we apply step 3 again, but this time we use height as the locking value: imageWidth = imageWidth x (availableHeight / imageHeight) imageHeight = availableheight 6) At this point, we know we have an image that fits in the bounding area that we've defined, so it's just a matter of centering it vertically and horizontally within that area, which is accomplished via simple division and subtraction. I've always been under the impression that there's some sort of formula or algorithm that combines both the width and height steps above, but every time I've found myself having to do this, I've been too time-constrained to give it proper research : )

Andy LeMay

1. Upload picture 2. Get current width / height. 3. Calculate aspect ratio. 4. Set to new width / height (if one is fixed because of the front-end set only one and calculate the other through the aspect ratio) 5. Save image. Maybe this'll help (not mine, googled): http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php

Philipp M. W. Hoffmann

Use something like this [code]$max = 1000; $tw  = <original width>; $th  = <original height>;         if ( <original height> > <original width>  && $max < <original width>) {             $th = $max / <original width> *  <original height>;             $tw = $max; } elseif ( <original height> > <original width> && $max < <original height>) {             $tw = $max /<original height> * <original width>;             $th = $max; } elseif ($max < $w) {             $tw = $th = $max; }[/code]

Chethan N

You can try this, http://code.google.com/p/timthumb/ coded in php, you can use it as a standalone script in any project. I have used it in two of my projects.

Habib Ullah Bahar

if you want to specify both height and width, then you can calculate the new height and width based on original dimensions using the aspect ration. In general, you can fix the width and let the library decided the height. For example,  http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/. The fixed width could correspond to what you need to show on your user interface.

Vishal K. Singh

I would think you could use CSS to set either the height or the width and then use background positioning within a div (overflow:hidden) to approximate "cropping". The position is something that could be set/adjusted by the person doing the uploading. This was a quickie off the top of my head so I might be mixing my Ps with my Qs a bit. None the less, I believe the basic idea is sound and ideally simpler.

Mark Simchock

I was struck by this problem recently. I had to reduce the size of some images for a customer’s website, but I had to keep the same aspect ratio so that the images look natural. The most simple and convenient solution that I found was http://www.ninjaunits.com/aspect-ratio-calculator/ and keep my proportions according to a new desired size: length or width.

Octavian Serafim

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.