How to perform scale to fit / fill? - blitline

I want to scale an image to fit or fill certain width while keeping the aspect ratio.
For example, when the server receives 1200x800 image without knowing the image size and I want to scale it down to 300x200 by specifying only width=300.
Note that I would specify either width or height, not both.
Also, I wouldn't want to scale up to make it bigger size, if the original image is small.

resize_to_fit does what I want.
Even though, doc says both width and height are required, you can actually perform scale_to_fit effect by providing either only width or only height to resize_to_fit function.

Related

Resize proportionally and crop image in any aspect like top right,top left..... on html5canvas [duplicate]

I'm trying to upload images and have them fit into different sized boxes....To give you an idea of what the application does: People upload images and have them printed onto posters.
For example, we'd have a poster size of 8" x 10"(live area) and the full size is 9.5" x 11.5", since the minimum DPI is 100, we typically multiple the 8x10 by 100 = 800x1000.
Here's an image explaining that I have an original image(http://i.imgur.com/Uds9rcZ.jpg) and need it to fit accordingly to the different sizes.
I may need to clarify this a bit, so ask questions if needed.
Thanks.
Canvas's context.drawImage has a version which allows you to scale an image while you are drawing it to the canvas.
If you resize disproportionally (like you do in your example) some of your resized image will fall off the canvas. Then your kitty will look distorted (in your example: stretched vertically)
This sample code resizes proportionally by using only the width. This way your kitty is not stretched.
// calculate how much to scale the resulting image
var originalWidth=16;
var originalHeight=20;
var desiredWidth=20;
var scalingFactor = desiredWidth/originalWidth;
// scale the original size proportionally
var newWidth=originalWidth*scalingFactor;
var newHeight=originalHeight*scalingFactor;
// resize the canvas to fit the desired image size
// Note: canvas is a reference to your html canvas element
canvas.width=newWidth;
canvas.height=newHeight;
// Draw the image to the canvas
// This version of drawImage allows you to scale the original image
// while you are drawing it to the canvas.
context.drawImage(
originalImage,
0,0,originalWidth,originalHeight,
0,0,newWidth,newHeight);
I would like to suggest you use the easyCanvas library to do this. The reason for this is that the built-in drawImage method of context do not scale the image proportionally for you, and it involves a small dose of math to get it right, especially in cases where destination shape differs from original and you want it to cover the whole area.
I made a method in this library to handle situations such as these allowing you to draw the original image proportionally into any shape even if the shape doesn't correspond with the original.
See this demo for an live example.
In essence what you do is to draw your uploaded image into the canvas with this method:
ez.drawImageProp(image, x, y, width, height);
where width and height would be that of destination.
It also has offset parameters so you can move the image around within that shape where an offset of 0.5 is center, 0 is from left and 1 is from right:
ez.drawImageProp(image, x, y, width, height, offsetX, offsetY);
Assuming image is already available all you need to do is:
var ez = new easyCanvas('canvas'); /// provide ID of existing canvas
ez.drawImageProp(image, 0, 0, ez.width, ez.height);
Disclaimer: I am the author of easyCanvas. easyCanvas is open-source (GPL3.0 license).

How to change the position of a label based on how big an image is

My image changes size and I want to know how to keep a label in the same spot on the image no matter the size. I want to keep the label inside of a speech bubble, is there a way to put constraints on it to do this, or is there another way? I'm working in Xcode.
Instead of giving constant position values, derive the required position using height and width of the image using mathematical calculation.
Example:
Let's say the image size is 400x400 and you want a label at bottom right
You can derive it as
X=width-width*0.75
Y=height-height*0.8
Now even if the height and width varies, the label will be in same location

how to calculate the right resolution for an image overlay in openlayers

I have an image I use as overlay in a vector layer in my openlayer web application. The idea is the same as showed in this example
What I do not understand is how to set a valid resolution to my image so that every time the user changes the zoom of the map, the image width and height is adjusted to cover its real geographical area.
In the example above some images are used and placed at some specific locations using a specific and hard-coded resolution factor.
Assume I have an image 400x400 pixel which represents an area of 400x400 kilometers, I need to recalculate the width and height of my image every time the zoom factor changes using a formula like this:
imageWidth = imageWidthInPixel * theResolution / map.getResolution();
imageHeight = imageHeightInPixel * theResolution / map.getResolution();
where 'theResolution' is the value I need to calculate some way I don't know. I guess this is a value that depends on the area expressed in kilometers or meters the image covers, but I am not able to find out a relation that has a sense. As explained above, in the example I reported, these values are hard-coded and depends on the image but there is no way to understand how these values are calculated.
Please help me understand this.
If you know the projection of your image, it would be easiest to simply define the image as a layer defined in OpenLayers with the appropriate projection. At that point, OpenLayers will handle the zooming and panning for you.

Image percentWidth 100 height stays original image height (margin)

What happens with the code below is that image width is scaled to 100% as expected and the height also scales as expected keeping the aspect ratio correct. Issue is that there is a margin at the bottom and that seems to be the height of the original contentHeight of the image. How can I get rid of that?
I am using percentages so that it scales when device orientation changes.
backdrop.source = "http://cf2.imgobject.com/t/p/" + "w342" + data.backdrop;
backdrop.scaleMode = "letterbox";
backdrop.horizontalAlign = "left";
backdrop.verticalAlign = "top";
backdrop.smooth = true;
backdrop.percentWidth = 100;
The answer to your question is don't use the letterbox setting. That is going to preserve the aspect ratio and make the black area, hence the name letterbox :)
Try setting scaleMode to zoom instead. As the documentation states, zoom will result in one axis being clipped. This should scale the image, preserve the aspect ratio, but clip some edges of the image to avoid having the black area.
Other solutions to this problem are:
modify the original image outside of Flash
use a mask to achieve similar results that the zoom setting will provide. In this approach you make the image bigger, but then apply a square mask to the image. The mask reveals only the square portion ... clipping what is outside the mask.
(undesirable in most cases) use the scaleMode setting of strectch (and specify both width/height) so that the area is filled, this will not preserve the aspect ratio
PS: There is no way to avoid the black area if the image's aspect ratio is not square. Even with HTML/CSS. This is just math/geometry. The same thing happens in HTML -- the image is either stretched, clipped, or will not fill both dimensions.
[Edit]
PPS: One other idea, if you know the original aspect ratio of the image, is to calculate a new width that will be closest to the desired width, but naturally preserves the width to height aspect ratio.
For example, the width:height ratio is 4:3. Your desired width is 500 pixels. Using cross products you get this:
4 500
- = -
3 x
Using cross products you get the equation:
4x = 3*500
Now solve for x:
x = 3*500/4 = 375
Therefore, if the original aspect ratio is 4:3, you can set a width of 500 and a height of 375 to scale the image and not have any black areas. You can even write code that dynamically calculates the aspect ratio, and applies this logic to scale something nicely. The point is that you have to the respect aspect ratio when scaling the image to avoid the "black" areas.

How to decrease image size(height, width) without losing its resolution and quality

I want to decrease height and width of a image without losing its resolution, so afterwards if i want to make image height and width as original, it looks same.
I dont know how this can be achieved. Will just changing image's height and width and later increasing will result in loss of resolution to image?
Got to the Image > Image Size > Resample Image and select Bicubic Sharper from the dropdown menu and you can get the output

Resources