<?php
namespace App\EventSubscriber;
use Imagick;
use Vich\UploaderBundle\Event\Event;
class FotoColaboradorSubscriber
{
const LIMIT_PHOTO_SIZE = 200000;
public function onVichUploaderPreUpload(Event $event)
{
$object = $event->getObject();
$fileName = $object->getFotoFile()->getPathName();
$img = new Imagick($fileName);
if ($img->getImageHeight() > $img->getImageWidth()) {
if ($img->getImageWidth() > 728) {
$img->adaptiveResizeImage(0, 728);
}
} else {
if ($img->getImageHeight() > 1024) {
$img->adaptiveResizeImage(1024, 0);
}
}
$img->writeImage($fileName);
}
public function onVichUploaderPostUpload(Event $event)
{
$object = $event->getObject();
$img = new Imagick('files/foto_persona/' . $object->getFoto());
$originalImg = clone $img;
$quality = $img->getImageCompressionQuality() - 3;// (ceil(100 / (($img->getImageLength() * 100 / $img->getImageCompressionQuality()) / self::LIMIT_PHOTO_SIZE)));
while ($img->getImageLength() > self::LIMIT_PHOTO_SIZE) {
$originalImg->setImageCompressionQuality($quality);
$originalImg->writeImage('files/foto_persona/' . $object->getFoto());
$img = new Imagick('files/foto_persona/' . $object->getFoto());
$quality = $quality - 3;
}
}
}