Yalantis/uCrop · error · CImgIOException

cimg::load_network(): Failed to load file '%s' with libcurl.

Error message

cimg::load_network(): Failed to load file '%s' with libcurl.

What it means

After the libcurl attempt in cimg::load_network() fails (curl not available, DNS failure, HTTP error, timeout, or empty/partial download) and fallback is not allowed, the library throws this CImgIOException reporting that the URL could not be downloaded with libcurl. It wraps any underlying curl error into a single user-visible failure.

Source

Thrown at ucrop/src/main/jni/CImg.h:70108

          curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,0L);
          curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1L);
          curl_easy_setopt(curl,CURLOPT_MAXREDIRS,20L);
          if (timeout) curl_easy_setopt(curl,CURLOPT_TIMEOUT,(long)timeout);
          if (std::strchr(url,'?')) curl_easy_setopt(curl,CURLOPT_HTTPGET,1L);
          if (referer) curl_easy_setopt(curl,CURLOPT_REFERER,referer);
          if (user_agent) curl_easy_setopt(curl,CURLOPT_USERAGENT,user_agent);
          res = curl_easy_perform(curl);
          curl_easy_cleanup(curl);
          cimg::fseek(file,0,SEEK_END); // Check if file size is 0
          const cimg_ulong siz = cimg::ftell(file);
          cimg::fclose(file);
          file = 0;
          if (siz>0 && res==CURLE_OK) { cimg::exception_mode(omode); return filename_local; }
          else std::remove(filename_local);
        }
      } catch (...) { }
      cimg::exception_mode(omode);
      if (!try_fallback) throw CImgIOException("cimg::load_network(): Failed to load file '%s' with libcurl.",url);
#endif

      CImg<char> command((unsigned int)std::strlen(url) + 1024), s_referer, s_user_agent, s_timeout;
      cimg::unused(try_fallback);

      // Try with 'curl' first.
      if (timeout) cimg_snprintf(s_timeout.assign(64),64,"-m %u ",timeout);
      else s_timeout.assign(1,1,1,1,0);
      if (referer) cimg_snprintf(s_referer.assign(1024),1024,"-e %s ",referer);
      else s_referer.assign(1,1,1,1,0);
      if (user_agent) cimg_snprintf(s_user_agent.assign(1024),1024,"-A \"%s\" ",user_agent);
      else s_user_agent.assign(1,1,1,1,0);
      cimg_snprintf(command,command._width,
                    "\"%s\" -L --max-redirs 20 %s%s%s-f --silent --compressed -o \"%s\" \"%s\"",
                    cimg::curl_path(),s_timeout._data,s_referer._data,s_user_agent._data,filename_local,
                    CImg<char>::string(url)._system_strescape().data());
      cimg::system(command,cimg::curl_path());

View on GitHub (pinned to f788b534b4)

Solutions

  1. Verify the URL is reachable (browser/curl) and returns 200; fix expired or malformed URLs.
  2. Enable fallback (try_fallback=true) so CImg can retry with the wget/curl binaries.
  3. Increase the timeout parameter and check device network connectivity/permissions (INTERNET permission on Android).
  4. Catch CImgIOException, log the URL, and fall back to a cached or bundled copy of the resource.

Example fix

// before
img.load_network(url, local, 5, false, 0, 0); // throws on any curl failure
// after
try {
  img.load_network(url, local, 30, true, 0, 0);
} catch (CImgIOException &e) {
  fprintf(stderr, "download failed, using cached copy: %s", e.what());
  img.load("cache/last_good.png");
}
Defensive patterns

Strategy: fallback

Validate before calling

// pre-check reachability
bool reachable = (system((std::string("curl -s -o /dev/null -w %{http_code} ") + url).c_str()) == 0);

Try / catch

try { img.load_network(url, local, 30, true, 0, 0); } catch (const CImgIOException &e) { fprintf(stderr, "download failed: %s", e.what()); img.load("cache/last_good.png"); }

Prevention

When it happens

Trigger: load_network(url, local, timeout, try_fallback=false, ...) where libcurl returned an error (CURLE_* failure, HTTP >= 400, zero-byte response) — thrown only when try_fallback is false, i.e. wget/curl binary fallback is skipped.

Common situations: Server or CDN returning 404/403/5xx for a remote image; device offline or DNS broken; TLS certificate problems on Android; timeout too small; URL wrong or expired (signed URLs).

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/52573c5bb3c812d0. Report an issue: GitHub.