Add html attributes to a tag via PHP

I was wondering if there was a way to do this rather than make my own function, I have a tag like

<img src="image.jpg">

and I need to add attributes to it, sometimes more than one, like height=x and width=x sometimes, but not others.

So a tag in that format is stored in my database column, and I need to modify them on the fly, any ideas?

Does this work for you?

See domdocument library and its examples in user notes.

Is that much necessary? I’m not looking to create a new document or anything, simply take an image tag and add a few attributes, thought it seemed more like string manipulation.

On second thought, I just decided to delete all records and change up how I store the image url, storing it as a plain url so I can later format it however I want it.