in reply to if statement doesnt work

Yes because you are doing it in a foreach loop. Put last; after your print $image if you don't want that to happen.

/J\