What you are looking for is imagemagick. Usually it is packaged with PHP. Just check your PHP info. http://php.net/manual/en/book.imagick.php Code (markup):
GD may be possible as well. It has less features than imagemagic but is more appropriate for simple conversions like this.