src/EventListener/MediaObjectCreatedListener.php line 41
<?php
namespace App\EventListener;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\MediaObject;
use App\Entity\Resize;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Intervention\Image\ImageManager;
use Symfony\Component\HttpKernel\KernelInterface;
use App\Service\ImageResize;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class MediaObjectCreatedListener implements EventSubscriberInterface
{
private EntityManagerInterface $entityManager;
private $kernel;
private $imageResize;
private $other_user = '';
private $other_host = '';
private $other_port = '';
private $other_pass = '';
private $other_name = '';
private $my_user = '';
private $my_host = '';
private $my_port = '';
private $my_pass = '';
private $my_name = '';
private $params;
public function __construct(EntityManagerInterface $entityManager, KernelInterface $kernel, ImageResize $imageResize, ParameterBagInterface $params )
{
$this->entityManager = $entityManager;
$this->kernel = $kernel;
$this->imageResize = $imageResize;
$this->params = $params;
/** стороння база */
$this->other_user= $this->params->get('OTHER_DB_user');
$this->other_host= $this->params->get('OTHER_DB_host');
$this->other_port= $this->params->get('OTHER_DB_port');
$this->other_pass= $this->params->get('OTHER_DB_pass');
$this->other_name= $this->params->get('OTHER_DB_name');
/** моя база */
// $this->my_user= $this->params->get('MY_DB_user');
// $this->my_host= $this->params->get('MY_DB_host');
// $this->my_port= $this->params->get('MY_DB_port');
// $this->my_pass= $this->params->get('MY_DB_pass');
// $this->my_name= $this->params->get('MY_DB_name');
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['saveMediaObject', EventPriorities::POST_WRITE],
];
}
public function saveMediaObject(ViewEvent $event)
{
$mainMedia = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$mainMedia instanceof MediaObject) {
return;
}
// echo $mainMedia->getId();
// echo $mainMedia->filePath;
$year = date('Y');
$month = date('m');
$documentRoot = $this->kernel->getProjectDir() . '/public/media';
$path = $documentRoot . "/$year/$month/";
$name = $mainMedia->filePath;
$uri = $path . $name;
$this->dublicate([
'id' => $mainMedia->getId(),
'file_path' => $mainMedia->filePath,
'type' => $mainMedia->getType(),
'name' => $mainMedia->getName(),
'upload_timestamp' => $mainMedia->getUploadTimestamp()->format('Y-m-d H:i:s'),
'size' => null,
'resize_img_parent_id' => null,
]);
if ( $mainMedia->getResizeImgParent() == null AND $this->isImage($uri)) {
$sizes = [90, 230, 700, 1200];
$manager = new ImageManager(
new \Intervention\Image\Drivers\Gd\Driver()
);
foreach($sizes as $s){
$image = $manager->read($uri);
$image->scale( $s);
$newName = "{$s}_px_" . $name;
$image->save($path . $newName );
$newMedia = new MediaObject();
$newMedia->filePath = $newName;
$newMedia->setName("resize-$s");
$newMedia->setSize($s);
$newMedia->setResizeImgParent($mainMedia);
$this->entityManager->persist($newMedia);
$this->entityManager->flush();
$this->dublicate([
'id' => $newMedia->getId(),
'file_path' => $newMedia->filePath,
'type' => $newMedia->getType(),
'name' => $newMedia->getName(),
'upload_timestamp' => $newMedia->getUploadTimestamp()->format('Y-m-d H:i:s'),
'size' => $newMedia->getSize(),
'resize_img_parent_id' => $mainMedia->getId(),
]);
}
}
// die;
}
private function dublicate($data){
$dsn = "pgsql:host={$this->other_host};port={$this->other_port};dbname={$this->other_name};";
// 3. Опції
$options = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
];
// 4. Підключення
$pdo = new \PDO($dsn, $this->other_user, $this->other_pass, $options);
// 5. INSERT-запит
$sql = "INSERT INTO media_object (
id,
file_path,
type,
name,
upload_timestamp,
\"size\",
resize_img_parent_id
) VALUES (
:id,
:file_path,
:type,
:name,
:upload_timestamp,
:size,
:resize_img_parent_id
)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
'id' => $data['id'],
'file_path' => $data['file_path'],
'type' => $data['type'],
'name' => $data['name'],
'upload_timestamp' => $data['upload_timestamp'],
'size' => $data['size'],
'resize_img_parent_id' => $data['resize_img_parent_id'],
]);
}
private function isImage($file)
{
$allowedTypes = [
IMAGETYPE_GIF,
IMAGETYPE_JPEG,
IMAGETYPE_PNG,
IMAGETYPE_WEBP,
// Add more as needed
];
$imageInfo = @getimagesize($file);
if ($imageInfo && in_array($imageInfo[2], $allowedTypes)) {
return true;
}
return false;
}
}