Resizing photos as thumbnails such as Flickr and Facebook photostreams?

What is the best way to resize photos and create thumbnails of the same size for photo streams? Flickr and Facebook do this, and we would like to emulate this functionality.

Here is an example: http://www.flickr.com/photos/alphageek/233472093/

If you are viewing the photo stream on the right side (middle page), all thumbnails are the same size, even if the source images vary in size, and most importantly, the thumbnails look reasonably cropped.

Flickr can automatically crop and resize the image around the main focus, as opposed to simply resizing the image to the thumbnail size (for example, by setting the width and height of the image), which will cause the image to look curled and lose focus on the image.

+3
source share
3 answers

For such problems, I always use: http://www.imagemagick.org/script/convert.php .

Imagemagick provides many functions for image processing. I take images in a loop and create a larger version and a thumbnail.

0
source

they do resize.

imagemagick , # , "40x40 #" "90x90 #", , , .

, , :

1) , , . 2) , .

+1

. :

CGFloat horizontalRatio = newSize.width/oldSize.width;
CGFloat verticalRatio = newSize.height/oldSize.height;
CGFloat ratio = MAX(horizontalRatio, verticalRatio);
CGSize newSize = CGSizeMake(oldSize.width *ratio, newSize.height*ratio)

( ):

UIGraphicsBeginImageContextWithOptions(newSize, YES, 0);
    [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

:

        CGRect cropRect = CGRectMake ((newImage.size.width - width) / 2, (newImage.size.height - height) / 2, width, height)
        UIGraphicsBeginImageContextWithOptions(cropRect.size, YES, 0); 
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGRect drawRect = CGRectMake(-cropRect.origin.x, -cropRect.origin.y, self.size.width, self.size.height);
        CGContextClipToRect(context, CGRectMake(0, 0, cropRect.size.width, cropRect.size.height));
        [self drawInRect:drawRect];
        UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
return croppedImage; 
+1

Source: https://habr.com/ru/post/1789341/


All Articles