Yes it can, if you know the focal length as well.
Assume that the focal length is $f$ and the distance from lens to object is $d$.
To get the object in focus, you need the distance from the lens to the sensor to be
$$f' = \frac{f * d}{f + d}$$
Once you have the distance from the optical center to the sensor, you know the magnification:
$$M = \frac{d}{f'}$$
which in most practical situations will be a "minification" (i.e. $d \gt f'$).
Finally you divide the size of the image on the sensor by the magnification to get the object size. If the image is $m\times n$ pixels of size $p$ each, then the object size is
$$width = \frac{m \cdot p\cdot d }{f'} = \frac{m \cdot p \;(f+d)}{f}$$
and similarly for the height.
Now the size of the pixels can usually be derived from the sensor type. It is not quite enough to know you have a "12 Mpix" camera, as sensors come in (many) different sizes. Typically the best cameras have the largest sensors (and large lenses). To complicate matters further, some camera manufacturers will quote the focal length of the lens in "equivalent for 35 mm" (especially for super zoom cameras with "1200 mm" telephoto capability). But that is going a bit off topic perhaps. See https://mattsassamatt.files.wordpress.com/2011/09/sensor-size7.png for a chart (somewhat out of date) of different sensor sizes, from which you can calculate the parameter $p$ I used above.

Let me know if you have enough information here.