Simplifying variable
Duplicated code removed
A better function from trim()
Part of the code was commented assuming that get too small images
htmlentities not need anymore