I know very little about TeX and LaTeX, but I often encounter strings containing markup described as LaTeX, because various tools, including MathJax, SymPy and Apple's Grapher, use LaTeX as a lingua franca for math. A user can often right-click on a nicely-formatted equation in a GUI and get a representation of it "as LaTeX" or call a method in a script and get a string described as containing "LaTeX markup". (I don't know if I'm using the correct terminology.)
I'm trying to take such a string and use automation to turn it into an image of a specific size (625 x 465 pixels as an example) with the equation expanded or shrunk to fit the image, or, failing that, at least centered.
I'm programmatically creating .tex files, using latex to compile to DVI, then using dvipng to convert to PNG. I have tried over 30 permutations of the various suggestions I've read on Stack Exchange. I've tried document classes including standalone, article, report and minimal. I've tried packages including graphicx, preview and geometry. I've tried commands including resizebox, vspace, topskip, centering and noindent. I've tried dvipng options -T and -D.
The results I get are never quite right:
Correct size, but oddly aligned:

Others are the correct size but blank (especially using standalone with options other than preview).
How can I produce images of the specified size that look right? Does anyone know a combination of a TeX document template into which I can insert the math markup, and a dvipng command that will convert it correctly?
I am running on Debian. latex --version reports pdfTeX 3.14159265-2.6-1.40.15 (TeX Live 2015/dev/Debian). dvipng --version reports dvipng 1.14.
an example latex command:
latex -halt-on-error -interaction batchmode -output-directory /data/notebooks/2016-09-13T17:41:22.181658_typesetting /data/notebooks/2016-09-13T17:41:22.181658_typesetting/foo21.tex
an example dvipng command:
dvipng -T 6.25in,4.65in -z 9 -bg gray 0.80 -o /data/notebooks/2016-09-13T17:41:22.181658_typesetting/foo21.png /data/notebooks/2016-09-13T17:41:22.181658_typesetting/foo21.dvi
I've been keeping a catalog of attempts in a Jupyter notebook. If necessary, maybe I could put all 31 (and counting) attempts on a Gist or something, with the .tex files, latex commands, dvipng commands, and result images.

-T="tight"or something like that withdvipng(and page dimensions -- and margins etc -- set by package geometry) ? – Sep 15 '16 at 18:11