to save it, all you have to do is this:
$fp = fopen("filename.jpg", "w+");
fwrite($fp, $xyz);
fclose($fp);
what you should do is come up with a way to name the image or somehow get the attachment name via imap functions, and then this also defeats the purpose of a standalone script to show the image, now you can integrate it to another script (if you have one) that reads the mail.