Sangit
Sangit

Reputation: 159

Getting Image dimensions in symfony file upload

I was wondering if someone could tell me how to get the image dimensions of i file uploaded in symfony 2.8 with formbulder

The image entity had a property $file

/*
 * @Assert\Image(
 *     minWidth = 400,
 *     maxWidth = 1200,
 *     minHeight = 400,
 *     maxHeight = 1200
 * )
 */
public $file;

In the controller i build the form like this

$form = $this->createFormBuilder($image)
        ->add('file', FileType::class)
        ->add('title', TextType::class)

And then process the request

$form->handleRequest($request);

    if ($form->isSubmitted()) {

        $file = $image->getFile();
        if (!in_array($file->getClientMimeType(), self::$allowedMimeTypes)) {
            throw new \InvalidArgumentException(sprintf('Files of type %s are not allowed.', $file->getClientMimeType()));
        }

  [...]

I have two setter mehods

$image->setSizeX($x);
$image->setSizeY($y);

But cannot for the love of god figure out how to get $x and $y from the file object.

Upvotes: 4

Views: 6383

Answers (1)

delpi
delpi

Reputation: 109

 $info = getimagesize($file);
 list($x, $y) = $info;

Upvotes: 8

Related Questions