public function kraken::applyToImage in Kraken 8
File
- src/
Plugin/ ImageAPIOptimizeProcessor/ kraken.php, line 141
Class
- kraken
- Optimize images using the Kraken.io webservice.
Namespace
Drupal\kraken\Plugin\ImageAPIOptimizeProcessorCode
public function applyToImage($image_uri) {
if (class_exists('\\Kraken')) {
if (!empty($this->configuration['api_key']) && !empty($this->configuration['api_secret'])) {
$kraken = new \Kraken($this->configuration['api_key'], $this->configuration['api_secret']);
$params = array(
'file' => $this->fileSystem
->realpath($image_uri),
'wait' => TRUE,
'lossy' => (bool) $this->configuration['lossy'],
);
// Send the request to Kraken.
$data = $kraken
->upload($params);
if (!empty($data['success']) && !empty($data['kraked_url'])) {
try {
$krakedFile = $this->httpClient
->get($data['kraked_url']);
if ($krakedFile
->getStatusCode() == 200) {
$this->fileSystem
->saveData($krakedFile
->getBody(), $image_uri, FileSystemInterface::EXISTS_REPLACE);
$this->logger
->info('@file_name was successfully processed by Kraken.io.
Original size: @original_size; Kraked size: @kraked_size; Total saved:
@saved_bytes. All figures in bytes', array(
'@file_name' => $image_uri,
'@original_size' => $data['original_size'],
'@kraked_size' => $data['kraked_size'],
'@saved_bytes' => $data['saved_bytes'],
));
return TRUE;
}
} catch (RequestException $e) {
$this->logger
->error('Failed to download optimized image using Kraken.io due to "%error".', array(
'%error' => $e
->getMessage(),
));
}
}
else {
$this->logger
->error('Kraken.io could not optimize the uploaded image.');
}
}
else {
$this->logger
->error('Kraken API key or secret not set.');
}
}
else {
$this->logger
->error('Could not locate Kraken PHP library.');
}
return FALSE;
}