I have the following:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use Imagine;
class UploadController extends Controller {
public function processImage($request) {
$file = $request->file('file');
$path = '/images';
$fileName = 'image.png';
if ($file) {
$file->move('../public' . $path, $fileName);
$gThumb = $this->createThumbnail(219, 300, '../public/images', 'image', 'png', 'thumb', true);
$pThumb = $this->createThumbnail(300, 300, '../public/images', 'image', 'png', 'pthumb');
return response()->json([
'gallery_thumbnail' => $path . '/' . $gThumb,
'upload_thumbnail' => $path . '/' . $pThumb
]);
}
}
function createThumbnail($height, $width, $path, $filename, $extension, $postfix = null, $mask = null)
{
$mode = ImageInterface::THUMBNAIL_OUTBOUND;
$size = new Box($width, $height);
$postfix = $postfix ? $postfix : 'thumb';
$thumbnail = Imagine::open("{$path}/{$filename}.{$extension}")->thumbnail($size, $mode);
if ($mask) {
$mask = Imagine::open('../public/images/masks/bubble-splash.png');
$thumbnail->applyMask($mask);
}
$destination = "{$filename}" . "." . $postfix . "." . "{$extension}";
$thumbnail->save("{$path}/{$destination}");
return $destination;
}
}
It saves the images as expected but does not apply the mask to the thumbnail.
Where am I going wrong (I am using Laravel 5)?
Also, when the script runs it takes literally about 1 minute to complete, so it's doing something but the images are still outputted with no mask applied.
In the end I think I'm going to use these guys https://www.imgix.com/
Turns out white transparency is the chosen masking logic in Imagine.
https://github.com/avalanche123/Imagine/pull/449#issuecomment-127516157
It is most likely a bug in the Imagine library. I found the following:
I could not make GD\Image::applyMask() to work as described in Reflection example in http://www.slideshare.net/avalanche123/introduction-toimagine so I made some fixes.
- It still supports only RGB palette for mask, but now accounts average between colors.
- It does change image if its transparency is less than 0.5.
From https://github.com/avalanche123/Imagine/pull/449
The associated fix not yet committed:
https://github.com/kasuparu/Imagine/commit/66a36652c76f9b5ff640f465d8f970c563841ae6
I tried the fixed code and it seems to work except the mask is (from my perspective) applied backwards, keeping the black parts and discarding the white parts. I commented on this issue in the pull request.
For reference, this is the fix in action:
Using $blackAmount:
And my fix of the fix, using $whiteAmount:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With