Yii2 - Imagine - creating a thumbnail and keeping transparent background - yii2

I am trying to upload an image and scale it to maximum 200px width or 200px height depends which is bigger, using Imagine.
I read this link: Imagine where it says:
If one of thumbnail dimensions is set to null, another one is calculated automatically based on aspect ratio of original image.
I Tried this code to change the width:
Image::thumbnail($target, 200, null)->save($target_to);
got an error:
Exception Imagine\Exception\InvalidArgumentException with message Length side cannot be 0 or negative.
I am also trying to upload png files with transparent background.
Looking at the same link it says:
If thumbnail mode is ImageInterface::THUMBNAIL_INSET, the original image is scaled down so it is fully contained within the thumbnail dimensions. The rest is filled with background that could be configured via yii\imagine\Image::$thumbnailBackgroundColor and yii\imagine\Image::$thumbnailBackgroundAlpha.
Looking at the links they give, it says it will fill:
$thumbnailBackgroundAlpha public static property (available since version 2.0.4)
Background alpha (transparency) to use when creating thumbnails in ImageInterface::THUMBNAIL_INSET mode with both width and height specified. Default is solid.
public static string $thumbnailBackgroundAlpha = 100
$thumbnailBackgroundColor public static property (available since version 2.0.4) Background color to use when creating thumbnails in ImageInterface::THUMBNAIL_INSET mode with both width and height specified. Default is white.
public static string $thumbnailBackgroundColor = 'FFF'
I have 2 questions:
Will this keep the transparent background?
public static string $thumbnailBackgroundAlpha = 100
$thumbnailBackgroundColor public static property (available since version 2.0.4)
I don't want to give width and height, only one dimension so the image will be resized accordingly. How do I do that?

As in latest releases, passing null as an argument is making the respective null argument value as zero and hence exception is thrown. You can solve it using as shown in the image. As you can see in the code, I have passed only width parameter i.e 400 and calculated height using the same and passed to the thumbnail method.

Related

autosizing movieclips to match text size (AS3)

I'm looking to add a specialized border around some dynamic text.
A special type of border that filters just can not produce
So I need this border to match the length of the dynamic text.
Unfortunately this code is not working
thistext.autoSize = TextFieldAutoSize.LEFT;
border.width = thistext.width;
What happens is, the border width is set to the initial starting width of the text and is not changed as the width of the text changes
Any ideas on how I can make this work?
You should rather listen only to text changes : flash.events.Event.CHANGE, instead of checking every frame.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html#event:change
Besides, you may use textWidth attribute to get actual text width, no matter the value of autosize attribute (Width must be set to the maximum width). I'm not a huge fan of auto size feature which hides the maximum width, although it exists internally ( equal to the width attribute of the textfield, BEFORE setting autosize..). I'd rather have a less "magical" but more clear behavior, but it's debatable, especially if your text has to interact with the mouse (click,hover..), then you can take advantage of the bounds being automatically updated
I actually caught my mistake.
I need to add the code into an event listener that checks every frame AFTER the dynamic change so this code works
border.width = thistext.width;

How get a string width in Libgdx?

I wonder know how to get the width of my string in pixels
BitmapFont API < 1.5.6
To mesure the width of a String you use your Font and get the bounds of the String, you are going to draw.
BitmapFont.getBounds(String str).width
BitmapFont API
You can get the height to for the right offset for drawing too. Just replace width with height.
In addition for multiline texts use getMultiLineBounds(someString).width to get the bounds.
BitmapFont API >= 1.5.6
The BitmapFont API changed in 1.5.7 so there is a different way to get the bounds now:
BitmapFont.TextBounds and getBounds are done. Instead, give the string to GlyphLayout and get the bounds using its width and height fields. You can then draw the text by passing the same GlyphLayout to BitmapFont, which means the glyphs don’t have to be laid out twice like they used to.
Source (Web archive)
Example:
GlyphLayout layout = new GlyphLayout(); //dont do this every frame! Store it as member
layout.setText("meow");
float width = layout.width;// contains the width of the current set text
float height = layout.height; // contains the height of the current set text
According to #Nates answer: https://stackoverflow.com/a/20759876/619673 calling method
BitmapFont.getBounds(String str).width
not always returns proper width! Especially when you are reusing font.
If you want draw text for example in the center of part of view port, you can avoid this problem by using another method
BitmapFont.drawWrapped(...)
Example code:
font.drawWrapped(spriteBatch, "text", x_pos, y_pos, your_area_for_text, BitmapFont.HAlignment.CENTER);
If you use skins in UI it is a hassle to find out the correct font to feed into the GlyphLayout.
In this case I use a throw away instance of Label to figure everything out for me then ask the Label for the width.
Skin skin = getMySkin();
Label cellExample = new Label("888.88888", skin);
cellExample.layout();
float cellWidth = cellExample.getWidth();
Table table = new Table(skin);
table.defaults().width(cellWidth);
// fill table width cells ...
This is not the answer if you want to position the text yourself, but it is useful to make a UI layout stable and less dependent on the actual content of the cells.

Getting image size through template?

I have a template on my wiki that I use to put in image side by side with an image of a location. The issue that I am having is that if the image (which is often taken from a movie screenshot) has a different ratio (say it is a wide screen 16:9) then there is a bunch of white space above and below the image. Is there a way to either
Set the Google Maps widget to obtain its height and width based off of the dimensions of the image being supplied or...
Obtain the aspect ratio of the image? If I can get that, then I can run a series of "#if" statements to set the widgets height & width.
Basically, I would like the map and image to have the same height when displayed on the page. The code for the template is below. Thanks in advance.
{{{!}}style="border: 1px solid darkgray;"
{{!}}-
{{!}}{{#widget:Google Maps
|height=270
|width=480
|lat={{{lat|}}}
|lng= {{{lng|}}}
|zoom={{{zoom|}}}
|}}
{{!}}|[[image:{{{image|}}}|none|480x270px]]}}
{{!}}-
{{!}}}
There is an extension for this (that I haven't tried myself): ImageSizeInfoFunctions. It will give you two parser functions for your purpose (and will work until the day one of your users try to put a file that a not a picture in there...)

How to show a two color area?

Since now, in the design of one of the websites I work, I've been using a graphic to decorate the header section that consists in a diagonal division white in the lower side and transparent in the upper side. The result is this:
If I change upper color, as the image is transparent in its upper area the effect seems perfect:
Now, I need to allow users to change page background and that's the problem:
Background changes to red, but the image I used to decorate the header doesn't change.
Is there any way to allow users to change the background without ruin the header decoration?
Note that store a copies of the decoration imagen in different colors is not an option due I allow users to choose any 24-bit color. Also, to change the image in real time like explained here isn't an option due multiple users may access the same file.
You could try changing the image in realtime using data URIs: https://developer.mozilla.org/en/data_URIs
With a data URI, you can do something like the following: <img src="data:image/png;base64,SGVsbG8sIFdvcmxkIQ%3D%3D" />. The image can be changed dynamically in JS by generating a new image and setting the src attribute to the new data URI.
You will need to find a suitable format for generating images in JS though. I have used pnglib.js before, and it works, but it might be slower than you'd prefer. You might need to test some different libraries and image formats to see which can be generated quickly. Also, make the image as small as possible - should be only the area with the diagonal split, the area to the right can be done with a div instead.
Alternatively, you could generate a unique image server-side via a script. Make a script that takes a GET parameter for the background color and generates the appropriate image (for PHP, you can use GD or IMagick). Advantage is that the server may generate the image and send it to the client quicker than the client could generate it in JS.
Add the following to the div with the background image:
position: absolute;
top: 0px;
right: 0px;
The problem currently is that the background cannot overlap your div with the background image. Adding the position: absolute gives your div some kind of "ghost box" model, thus allowing the background of the body to overlap it.
P.S.: you can also play with z-index if you want to.

What are the units for the crop attributes in the imagedata element of WordML?

I am trying to extract images from a document saved as WordML. Some of the images are cropped from a single resource using attributes on the imagedata element eg:
<v:imagedata r:id="rId8" o:title="" cropbottom="32429f" cropright="44328f"/>
What are the units for the cropbottom and cropright attributes?
I have done some web searching and according to http://msdn.microsoft.com/en-us/library/bb229565%28v=vs.85%29.aspx the units should be percentages but don't seem to be.
In the example above the resulting image should come from the top left of the original and be a third of the width and half the height.
I'll answer my own question in the hope that it is useful to someone else.
According to ImageData Class - Office 2010
CropBottom ... specifies the how much to crop the image from the bottom
up as a fraction of picture size. Default is 0. This numeric value can
also be specified in 1/65536-ths if a trailing "f" is supplied. For
example, a value of "52429f" represents 52429/65536 or 0.8.