From 75599e85690cf25b6ccdddd7337274980d4885f2 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 24 Mar 2018 14:16:30 +0100 Subject: [PATCH] Support all image formats offered by libgd --- Image.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Image.cpp b/Image.cpp index 0e76474..b5f0f02 100644 --- a/Image.cpp +++ b/Image.cpp @@ -103,12 +103,9 @@ void Image::drawCircle(int x, int y, int diameter, const Color &c) void Image::save(const std::string &filename) { - FILE *f = fopen(filename.c_str(), "wb"); - if(!f) { - std::ostringstream oss; - oss << "Error writing image file: " << std::strerror(errno); - throw std::runtime_error(oss.str()); - } - gdImagePng(m_image, f); // other formats? - fclose(f); + const char *f = filename.c_str(); + if (gdSupportsFileType(f, 1) == GD_FALSE) + throw std::runtime_error("Image format not supported by gd"); + if (gdImageFile(m_image, f) == GD_FALSE) + throw std::runtime_error("Error saving image"); }