{"record":{"id":"d4f8212ee51ee66b","repo":"Yalantis/uCrop","slug":"save-graphicsmagick-external-failed-to-save-fil","errorCode":null,"errorMessage":"save_graphicsmagick_external(): Failed to save file '%s' with external command 'gm'.","messagePattern":"save_graphicsmagick_external\\(\\): Failed to save file '(.+?)' with external command 'gm'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":63747,"sourceCode":"#endif\n      CImg<charT> command(1024), filename_tmp(256);\n      do {\n        cimg_snprintf(filename_tmp,filename_tmp._width,\"%s%c%s.%s\",\n                      cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),\n                      _spectrum==1?_cimg_sge_extension1:_cimg_sge_extension2);\n      } while (cimg::path_exists(filename_tmp));\n\n#ifdef cimg_use_png\n      save_png(filename_tmp);\n#else\n      save_pnm(filename_tmp);\n#endif\n      cimg_snprintf(command,command._width,\"\\\"%s\\\" convert -quality %u \\\"%s\\\" \\\"%s\\\"\",\n                    cimg::graphicsmagick_path(),quality,\n                    CImg<charT>::string(filename_tmp)._system_strescape().data(),\n                    CImg<charT>::string(filename)._system_strescape().data());\n      if (cimg::system(command,cimg::graphicsmagick_path())!=0)\n        throw CImgIOException(_cimg_instance\n                              \"save_graphicsmagick_external(): Failed to save file '%s' with external command 'gm'.\",\n                              cimg_instance,\n                              filename);\n      if (!cimg::path_exists(filename))\n        throw CImgIOException(_cimg_instance\n                              \"save_graphicsmagick_external(): Failed to save file '%s' with external command 'gm'.\",\n                              cimg_instance,\n                              filename);\n\n      std::remove(filename_tmp);\n      return *this;\n    }\n\n    //! Save image using ImageMagick's external binary.\n    /**\n       \\param filename Filename, as a C-string.\n       \\param quality Image quality (expressed in percent), when the file format supports it.\n       \\note This method uses \\c convert, an external executable binary provided by","sourceCodeStart":63729,"sourceCodeEnd":63765,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L63729-L63765","documentation":"CImg::save_graphicsmagick_external() shells out to the GraphicsMagick 'gm convert' executable to write the image to a format CImg does not natively support. This error is thrown when either the 'gm' system command returns a non-zero exit status, or the command 'succeeds' but the expected output file does not exist afterwards. It means the external GraphicsMagick invocation failed to produce the target file.","triggerScenarios":"Calling save_graphicsmagick_external(filename, quality) when: the 'gm' binary is not installed or cimg::graphicsmagick_path() points to a missing/wrong path; the command fails due to bad permissions, unsupported format, or a bad quality value; or 'gm' exits 0 but fails to write the requested output filename (e.g. unwritable directory).","commonSituations":"Deploying to an environment (e.g. Android NDK builds, Docker containers) where GraphicsMagick is not installed; graphicsmagick_path env var (CIMG_GRAPHICS MAGICK_PATH) misconfigured; output directory lacks write permission; requested format extension not understood by the installed gm version.","solutions":["Install GraphicsMagick or verify cimg::graphicsmagick_path() points to a valid 'gm' executable (test by running the command manually).","Check that the output directory exists and is writable by the process.","Simplify: run the constructed 'gm convert ...' command by hand to see the real error message.","Use CImg's native save() for formats it supports, or save to a temporary supported format first.","Verify the target format/extension is supported by the installed GraphicsMagick version."],"exampleFix":"// before\nimg.save_graphicsmagick_external(\"out.png\", 100);\n// after\nif (cimg::path_exists(cimg::graphicsmagick_path()) || !std::system(\"gm version > /dev/null 2>&1\"))\n  img.save_graphicsmagick_external(\"out.png\", 100);\nelse\n  img.save_png(\"out.png\"); // fallback to native save","handlingStrategy":"try-catch","validationCode":"bool gmAvailable() { return std::system(\"gm version > /dev/null 2>&1\") == 0; }\nif (!gmAvailable()) { /* fallback to native save */ }\nif (!cimg::path_exists(outputDir)) { /* create or error out early */ }","typeGuard":null,"tryCatchPattern":"try {\n  img.save_graphicsmagick_external(filename, quality);\n} catch (CImgIOException& e) {\n  std::fprintf(stderr, \"gm save failed: %s\", e.what());\n  img.save_png(fallbackName); // native fallback\n}","preventionTips":["Install GraphicsMagick in every deployment environment and verify with 'gm version'.","Set/verify the GraphicsMagick path configuration before saving.","Check output directory writability before calling save.","Prefer CImg native save() for supported formats to avoid the external dependency."],"tags":["io","external-command","graphicsmagick","image-save"],"backgroundTag":"command-not-found","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}