What have you got so far? What you describe sounds pretty close: loop through all the files in the directory; for each file, if it has a .jpg or .gif extension (I'm presuming you're ensuring that these are the extensions they're saved with), unlink it.
foreach(file in the directory)
{
if the file has a .jpg or .gif extension
unlink it.
}