I can give you some pointers, but what you're looking to do is really custom coding.
You would want to use the require_once function to include the file:
function.require-once.php
To make it a function, you would basically want to have $img be a parameter instead of hardcoded. Take a look at this guide on how to write functions:
language.functions.php