How to make gameobject match the source texture's size?

I have a gameobject with the standard mesh renderer/quad/collider etc and it’s using a texture to display an image. Is there a way for the object to match the source texture’s resolution or ratio at least?

Or is there an easier way to do this? I tried using a image component and sprite texture instead but it won’t show up.

Get the MeshRenderer and use the materials texture to find the height and width of the texture.
Once you have those you can divide height / width. That will tell you how much larger the texture height is compared to the width. For example a 256 by 512 texture will return 2, so you have a ratio of 2 : 1 (height : width). A 512 by 256 texture will return 0.5 which is 1 : 2. then you can just do something like y = x * ratio.

Transform quad; //assigned however you want

MeshRenderer mr = quad.GetComponent<MeshRenderer>();
Texture tex = mr.material.mainTexture;
float ratio = tex.height / tex.width;
float x = 1; //this will be the width of the quad so set it to whatever...
float y = x * ratio;
quad.localScale = new Vector3(x, y, 1);

I just wrote the code here and i didn’t check it so it could have errors.