Image Upload with ImageMagick

1

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

I'm trying this way, but I get 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 to a folder first is working, but I believe it has a loss of performance since you need 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);
    
asked by anonymous 04.09.2018 / 20:13

1 answer

1

Theoretically, just change% from% to% with%. Since $request->img is an instance of UploadedFile .

    
04.09.2018 / 20:25