Skip to content

Instantly share code, notes, and snippets.

@rohankhudedev
Created February 8, 2018 07:58
Show Gist options
  • Save rohankhudedev/b40da82ca5ab0d34770ac30eca0a0b28 to your computer and use it in GitHub Desktop.
Save rohankhudedev/b40da82ca5ab0d34770ac30eca0a0b28 to your computer and use it in GitHub Desktop.
Laravel - compress image using tinypng API
//Get API KEY from https://tinypng.com/developers
try
{
\Tinify\setKey("W2zSUtK-Up2aS8Q6kQ-ndeHOL06SGHVz");
//TinyPNG Compress Image
$filepath = public_path('images/' . "knowledge-tribe.png");
$source = \Tinify\fromFile($filepath);
$source->toFile($filepath);
} catch( \Tinify\AccountException $e )
{
// Verify your API key and account limit.
return redirect('images/create')->with('error', $e->getMessage());
} catch( \Tinify\ClientException $e )
{
// Check your source image and request options.
return redirect('images/create')->with('error', $e->getMessage());
} catch( \Tinify\ServerException $e )
{
// Temporary issue with the Tinify API.
return redirect('images/create')->with('error', $e->getMessage());
} catch( \Tinify\ConnectionException $e )
{
// A network connection error occurred.
return redirect('images/create')->with('error', $e->getMessage());
} catch( Exception $e )
{
// Something else went wrong, unrelated to the Tinify API.
return redirect('images/create')->with('error', $e->getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment