Error handling

iLoveIMG allows users to catch errors from all API requests. These errors can be from the Start request, Authentication, Upload files, Process task and Download. The following example shows how to catch all of these errors in a single task:

Examples using a libraryPHP
try {
    // start the class manager
    $iloveimg = new Iloveimg("PUBLIC_KEY", "SECRET_KEY");

    // and get the task tool
    $myTask = $iloveimg->newTask('compress');

    // file var keeps info about server file id, name...
    // it can be used later to delete the file
    $file = $myTask->addFile('/path/to/file/document.png');

    // and set name for output file.
    // in this case it will output a zip file, so we set the package name.
    $myTask->setPackagedFilename('compressed_images');

    // and name for each compressed image (inside the zip file)
    $myTask->setOutputFilename('compressed');

    // process files
    $myTask->execute();

    // and finally download file. If no path is set, it will be downloaded on current folder
    $myTask->download('path/to/download');

    // catch possible errors
    // Start task errors
} catch (\Iloveimg\Exceptions\StartException $e) {
    echo "An error occurred on start: " . $e->getMessage() . " ";
    // Authentication errors
} catch (\Iloveimg\Exceptions\AuthException $e) {
    echo "An error occurred on auth: " . $e->getMessage() . " ";
    echo implode(', ', $e->getErrors());
    // Uploading files errors
} catch (\Iloveimg\Exceptions\UploadException $e) {
    echo "An error occurred on upload: " . $e->getMessage() . " ";
    echo implode(', ', $e->getErrors());
    // Processing files errors
} catch (\Iloveimg\Exceptions\ProcessException $e) {
    echo "An error occurred on process: " . $e->getMessage() . " ";
    echo implode(', ', $e->getErrors());
    // Downloading files errors
} catch (\Iloveimg\Exceptions\DownloadException $e) {
    echo "An error occurred on download: " . $e->getMessage() . " ";
    echo implode(', ', $e->getErrors());
    // Other errors (such as connection errors)
} catch (\Exception $e) {
    echo "An error occurred: " . $e->getMessage();
}
PHP library ilovepdf/iloveimg-phpcomposer require ilovepdf/iloveimg-php