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?
– fernandosavio
Yes, the Laravel.
– Diego Vieira
Theoretically it’s all about trading
$request->img
for$request->img->path()
. Since$request->img
is an instance ofUploadedFile
– fernandosavio
@fernandosavio Dude, that’s right. Thanks!
– Diego Vieira
I’ll create an answer in case someone drops in here from Google...
– fernandosavio