views:

64

answers:

4

In my site, users can upload photos. I currently compress and resize the photo to make sure they aren't huge files. But this creates photos that are of varying dimensions... which makes the site a bit "ugly" in my opinion.

I'd like to ensure the thumbnails are square images, but not by using padding. It's ok if there is some loss of the photo in the thumbnail. I'd like to keep the fidelity of the photo high, even if it means some cropping needs to occur.

A: 

Rather than cropping, I would make the div or whatever you put them in a fixed square size. Scale the image to fit inside that square.

How would you decide to crop it? From the top-left? Bottom-right? Center?

BioBuckyBall
I don't want to do on-client scaling - as it's a performance issue. I plan to crop from top left using the shortest dimension as the size, square. Then as Carson6300 mentioned, if it's wider than taller, crop equal from each side. If it's taller than wide, crop mostly from the bottom.
Chad
The problem with this is if you are taking a large image and just scaling it down the browser still loads the large image. Multiply that by a few images and your website load speeds tank.
antonlavey
@Chad, @antonlavy - Where in my answer did I say client side scaling?
BioBuckyBall
@Lucas, from the sound of the answer, imo, you were referring to setting the size on the div and letting that determine the size of the image.
Chad
+1  A: 

I would imagine you need to take the shortest dimension (either w or h), and use that as your dimension for creating the cropped image, essentially you can crop and then scale the image. Check out this article as an example for cropping an image. Also check out this Stack Overflow question regarding image quality.

Matthew Abbott
That's a great point, use the shortest dimension for the crop settings. Thanks.
Chad
A: 

To make a rectangle into a square you need to either pad, resize without preserving aspect ratio or crop (or combinations).

Here's some code for cropping

http://snippets.dzone.com/posts/show/1484

(I work for Atalasoft -- in our DotImage Photo SDK), it's

AtalaImage img = new AtalaImage("filename.jpg");
AtalaImage img2 = new CropCommand( /*point and size of crop */).Apply(img).Image;
img2.Save("filename", new JpegEncoder(quality), null);
Lou Franco
+2  A: 

I wrote some code to do this exact thing. I chose to crop it because resizing without preserving aspect ratio looks pretty horrible. I do a crop then a resize to create a thumnail image:

  public Bitmap CreateThumbnail(Bitmap RawImage)
    {
        int width = RawImage.Width;
        int height = RawImage.Height;

        Size ThumbnailDimensions = new Size();
        ThumbnailDimensions.Width = 100;
        ThumbnailDimensions.Height = 100;

        Rectangle cropArea = new Rectangle();
        if (width > height)
        {               
            cropArea.Width = height;
            cropArea.Height = height;
            cropArea.X = 0;
            cropArea.Y = 0;                
        }
        else if (width < height)
        {
            cropArea.Width = width;
            cropArea.Height = width;
            cropArea.X = 0;
            cropArea.Y = 0;
        }
        if(width != height) Bitmap thumbnail = CropImage(RawImage, cropArea);

        thumbnail = ResizeImage(thumbnail, ThumbnailDimensions);
        return thumbnail;
    }

This just crops from the top left corner then resizes it to my thumbnail dimensions.

antonlavey
That's pretty much exactly what I was looking for, thanks a ton!
Chad
no problem have fun.
antonlavey