src/EventListener/UploadListener.php line 9
<?phpnamespace App\EventListener;use Vich\UploaderBundle\Event\Event;class UploadListener{public function onVichUploaderPreUpload(Event $event): void{$object = $event->getObject();$file = $object->file->getRealPath();$type = getimagesize($file);if (true !== is_array($type)) {return;}if (!strpos($type['mime'], 'png') && !strpos($type['mime'], 'jpeg') && !strpos($type['mime'], 'jpg') && !strpos($type['mime'], 'gif')) {return;}$width = $type[0] >= 900 ? 900 : $type[0];$height = $width * ($type[0] / $type[1]);$this->_resizeImage($file, $type, $width, $height);}private function _resizeImage($file, $type, $w, $h, $crop = false): void{list($width, $height) = getimagesize($file);$r = $width / $height;if ($crop) {if ($width > $height) {$width = ceil($width - ($width * abs($r - $w / $h)));} else {$height = ceil($height - ($height * abs($r - $w / $h)));}$newwidth = $w;$newheight = $h;} else {if ($w / $h > $r) {$newwidth = $h * $r;$newheight = $h;} else {$newheight = $w / $r;$newwidth = $w;}}if (strpos($type['mime'], 'png')) {$src = @imagecreatefrompng($file);} else {$src = @imagecreatefromjpeg($file);}$dst = imagecreatetruecolor($newwidth, $newheight);if (strpos($type['mime'], 'png')) {// integer representation of the color black (rgb: 0,0,0)$background = imagecolorallocate($dst, 0, 0, 0);// removing the black from the placeholderimagecolortransparent($dst, $background);// turning off alpha blending (to ensure alpha channel information// is preserved, rather than removed (blending with the rest of the// image in the form of black))imagealphablending($dst, false);// turning on alpha channel information saving (to ensure the full range// of transparency is preserved)imagesavealpha($dst, true);imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);imagepng($dst, $file);} else {imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);imagejpeg($dst, $file);}imagedestroy($dst);}}