{"record":{"id":"47ae191a1452860c","repo":"Yalantis/uCrop","slug":"load-imagemagick-external-failed-to-load-file","errorCode":null,"errorMessage":"load_imagemagick_external(): Failed to load file '%s' with external command 'magick/convert'.","messagePattern":"load_imagemagick_external\\(\\): Failed to load file '(.+?)' with external command 'magick/convert'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":59800,"sourceCode":"                      \"png\"\n#else\n                      \"pnm\"\n#endif\n                      );\n        std::FILE *file = popen(command,\"r\");\n        if (file) {\n          const unsigned int omode = cimg::exception_mode();\n          cimg::exception_mode(0);\n          try {\n#ifdef cimg_use_png\n            load_png(file);\n#else\n            load_pnm(file);\n#endif\n          } catch (...) {\n            pclose(file);\n            cimg::exception_mode(omode);\n            throw CImgIOException(_cimg_instance\n                                  \"load_imagemagick_external(): Failed to load file '%s' with \"\n                                  \"external command 'magick/convert'.\",\n                                  cimg_instance,\n                                  filename);\n          }\n          pclose(file);\n          return *this;\n        }\n      }\n#endif\n      do {\n        cimg_snprintf(filename_tmp,filename_tmp._width,\"%s%c%s.%s\",\n                      cimg::temporary_path(),\n                      cimg_file_separator,\n                      cimg::filenamerand(),\n#ifdef cimg_use_png\n                      \"png\"\n#else","sourceCodeStart":59782,"sourceCodeEnd":59818,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L59782-L59818","documentation":"CImg's load_imagemagick_external() shells out to ImageMagick ('magick'/'convert') to convert an unsupported format (e.g. TIFF, GIF, PDF) to PNM/PNG, then parses the temp file. This CImgIOException is thrown when the conversion command succeeded and produced a temp file, but parsing that temp file with load_pnm/load_other failed (the catch block around the load).","triggerScenarios":"Calling CImg<T>::load_imagemagick_external(filename) (or a load() that routes to it) where 'magick convert <tmp> <file>' runs without a nonzero exit but the generated temporary image cannot be decoded by load_pnm/load_other — e.g. magick produced an empty or zero-byte temp file, or wrote a format the built-in parsers cannot read.","commonSituations":"ImageMagick delegates failing silently (e.g. broken ghostscript delegate for PDFs), disk-full so the temp file is truncated, security policies (policy.xml) blocking a coder so magick exits 0 but writes nothing, or a converted file with a corrupted/unsupported intermediate format.","solutions":["Run 'magick convert <input> out.pnm' manually with the same input to see the real ImageMagick error and fix the delegate/policy it reports.","Verify the temp directory (TMPDIR/TMP) is writable and has free space so the intermediate file is complete.","Check ImageMagick's policy.xml and delegate configuration are intact and allow the target format.","Pre-convert the file yourself with magick to a supported format (PNG/PNM) and load that directly with CImg<T>::load().","Confirm the 'magick'/'convert' binary is a real ImageMagick 7 (or matching 6) install, not a shim that returns success spuriously."],"exampleFix":"// before: blind external load\nimg.load_imagemagick_external(\"scan.pdf\");\n// after: check file decodes first / pre-convert\nif (!cimg::is_file(\"scan.pdf\")) throw ...\nCImg<unsigned char> img;\ntry { img.load_imagemagick_external(\"scan.pdf\"); }\ncatch (CImgIOException&) { cimg::system(\"magick scan.pdf scan.png\"); img.load(\"scan.png\"); }","handlingStrategy":"try-catch","validationCode":"if (!cimg::is_file(path)) throw std::invalid_argument(\"missing input\");\nif (cimg::system(\"magick -version\",\"magick\")!=0) throw std::runtime_error(\"ImageMagick unavailable\");","typeGuard":"bool loadableByMagick(const char* p){ return p && cimg::is_file(p) && cimg::fsize(p)>0; }","tryCatchPattern":"try { img.load_imagemagick_external(path); }\ncatch (CImgIOException& e) {\n  // fallback: pre-convert then native load\n  if (cimg::system((std::string(\"magick \")+path+\" tmp_out.png\").c_str())==0)\n    img.load(\"tmp_out.png\");\n  else throw;\n}","preventionTips":["Verify magick/convert is installed in every deployment image","Check policy.xml allows the formats you load","Ensure TMPDIR is writable and has free space","Pre-convert to PNG and use native load() when possible"],"tags":["imagemagick","external-tool","io","cimg"],"backgroundTag":"file-read-failed","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"}