Created
January 5, 2014 23:01
-
-
Save andreia/8275264 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Component; | |
use Symfony\Component\HttpKernel\Log\LoggerInterface; | |
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface; | |
use PhpAmqpLib\Message\AMQPMessage; | |
class GenerateImageConsumer implements ConsumerInterface | |
{ | |
/** | |
* @var LoggerInterface $logger | |
*/ | |
private $logger; | |
/** | |
* Construtor | |
* | |
* @param LoggerInterface $logger | |
* | |
* @return void | |
*/ | |
public function __construct(LoggerInterface $logger) | |
{ | |
$this->logger = $logger; | |
} | |
/** | |
* Método principal que recebe e processa a mensagem | |
* | |
* @param AMQPMessage $msg | |
* | |
* @return boolean Status da execução | |
*/ | |
public function execute(AMQPMessage $msg) | |
{ | |
// Dados enviados através do RabbitMQ ($msg->body) | |
$imageToGenerate = json_decode($msg->body, true); | |
// Gera a imagem | |
if(!$this->generateImageJpg($imageToGenerate['imageToGenerate'], $imageToGenerate['imageFilename'])) | |
{ | |
// deve gerar a imagem novamente | |
return false; | |
} | |
// realiza outras operações se necessário | |
// ... | |
return true; | |
} | |
/** | |
* Gera imagem jpg conforme imagem fornecida (caminho) | |
* | |
* @param string $imageToGenerate Caminho da imagem origem | |
* @param string $imageFilename Caminho e nome da imagem a ser gerada | |
* | |
* @return boolean Status | |
*/ | |
protected function generateImageJpg($imageToGenerate, $imageFilename){ | |
$status = false; | |
// Verifica se a imagem já existe | |
if (file_exists($imageFilename)) { | |
$this->logger->info(sprintf('O arquivo "%s" já existe', $imageFilename)); | |
return true; | |
} | |
try { | |
$image = new \Imagick($_SERVER['DOCUMENT_ROOT'].$imageToGenerate); | |
$image->setIteratorIndex(0); | |
$image->setImageFormat('jpg'); | |
$image->setImageCompressionQuality('80'); | |
$image->setImageCompression(\Imagick::COMPRESSION_JPEG); | |
$image->stripImage(); | |
$image->writeImage($imageFilename); | |
$status = true; | |
} catch (\Exception $e) { | |
// log do erro | |
$this->logger->err(sprintf('Não foi possível gerar o jpg da imagem: "%s" para "%s". Erro: "%s"', $imageToGenerate, $imageFilename, $e->getMessage())); | |
} | |
return $status; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment