For the PHP part, your best bet is a nice search on google for specialized website about PHP.
For the "import" of the modified image, you should take a look into the c2 plugins section of this forum, to the "Load image at runtime" plugin, this might be what you are looking for.
At the moment, there is no "easy"/straightforward way of doing what you are trying to achieve. It's pretty tricky anyway.