Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PHP Imagine to apply a mask

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/

like image 805
imperium2335 Avatar asked Jul 26 '15 10:07

imperium2335


1 Answers

Update 2015-08-04 11:32 +0000

Turns out white transparency is the chosen masking logic in Imagine.
https://github.com/avalanche123/Imagine/pull/449#issuecomment-127516157

Original

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.

  1. It still supports only RGB palette for mask, but now accounts average between colors.
  2. 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: php-imagine-applymask-using-blackamount-20150731-1831-gmt

And my fix of the fix, using $whiteAmount: php-imagine-applymask-using-whiteamount-20150731-1831-gmt

like image 111
spenibus Avatar answered Oct 19 '22 23:10

spenibus