, :
$image = new Imagick;
$image->setResolution(300, 300);
$image->readImage("${originalPath}[0]");
$image->setImageFormat('jpg');
$image->scaleImage(500, 500, true);
$image->setImageAlphaChannel(Imagick::VIRTUALPIXELMETHOD_WHITE);
Laravel, Laravels.
Storage::put($storePath, $image->getImageBlob());
Update: so I changed OS recently, and although it previously worked on my Ubuntu machine on my Mac, some images still came out of black.
I had to change the script to below:
$image = new Imagick;
$image->setResolution(300, 300);
$image->setBackgroundColor('white');
$image->readImage("${originalPath}[0]");
$image->setImageFormat('jpg');
$image->scaleImage(500, 500, true);
$image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
It seems important to set the background color before reading on the image. I also smooth out any possible layers and delete the alpha channel. I feel like I tried ALPHACHANNEL_REMOVEon my Ubuntu machine and it didn’t work so reliably between these answers, readers may find something that works for them.
source
share