Image upload with Imagemagick

Asked

Viewed 119 times

1

Using the library Imagick, it is possible to load an image directly from the upload? That is, without first having to move it to a folder and specify the path in the Imagick.

I’m trying this way, but it returns me the error Can not process empty Imagick object

$image = new \Imagick($request->img);
$fileName = date('YmdHis') . microtime(true) . rand(111111111, 999999999) . '.jpg';
$image->setImageCompressionQuality(70);
$image->setImageFormat("jpg");
$image->stripImage();
$image->writeImage('uploads/perfil/' . $fileName);

That way, moving first to a folder, it’s working, but I believe it has a loss of performance since it needs to move the image twice:

$file = $request->img;
$fileName = date('YmdHis') . microtime(true) . rand(111111111, 999999999) . '.' . $file->getClientOriginalExtension();
$file->move('uploads/perfil', $fileName);

$image = new \Imagick(public_path('uploads/perfil/' . $fileName));
$image->setImageCompressionQuality(70);
$image->setImageFormat("jpg");
$image->stripImage();
$image->writeImage('uploads/perfil/' . $fileName);
  • Are you using any framework?

  • Yes, the Laravel.

  • Theoretically it’s all about trading $request->img for $request->img->path(). Since $request->img is an instance of UploadedFile

  • @fernandosavio Dude, that’s right. Thanks!

  • I’ll create an answer in case someone drops in here from Google...

1 answer

1


Theoretically it’s all about trading $request->img for $request->img->path(). Since $request->img is an instance of Uploadedfile.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.