Skip to content

Can I add a watermark ? #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Hredoy opened this issue Nov 29, 2017 · 10 comments
Closed

Can I add a watermark ? #480

Hredoy opened this issue Nov 29, 2017 · 10 comments

Comments

@Hredoy
Copy link

Hredoy commented Nov 29, 2017

I want to add watermark after uploading any image

@streamtw
Copy link
Member

streamtw commented Dec 1, 2017

@Hredoy Please check this post in our wiki. You can move those code into an event listener.

@Hredoy
Copy link
Author

Hredoy commented Dec 1, 2017

where i get that file i need location

@streamtw
Copy link
Member

streamtw commented Dec 4, 2017

The code from the wiki was in UploadController.

@Hredoy
Copy link
Author

Hredoy commented Dec 4, 2017

capture
capture

when i add same code i am facing this error invalid upload request

@Hredoy
Copy link
Author

Hredoy commented Dec 5, 2017

Hello I need that solution as soon as possible.

@Hredoy
Copy link
Author

Hredoy commented Dec 6, 2017

@g0110280
Help me to add watermark.
Bro.

@gwleuverink
Copy link
Collaborator

Hi @Hredoy.

You can add a watermark using a combination of the ImageWasUploaded Event and the Image facade provided by intervention image.

If you are not familiar with Laravel events I recommend you check out the docs or this video first.

First you need to create an event listener within your app that listens to the ImageWasUploaded event. Check out this page in the documentation for more info. Or refer to the demo project that provides usage examples here.

Next you need to add the watermark. You need to do this from within the event listener you just created. This filemanager uses the Intervention Image package. On this page you'll find an example on how to add a watermark.

Applying everything your event listener code should look something like the example below. I didn't test this but it will help you on your way.

class HasUploadedImageListener
{
    /**
     * Handle the event.
     *
     * @param  ImageWasUploaded  $event
     * @return void
     */
    public function handle(ImageWasUploaded $event)
    {
        // Grab the uploaded file's path
        $imagePath = $event->path();

        // Open the image file
        $img = Image::make($imagePath);

        // Insert the watermark
        $img->insert('public/watermark.png');

        // Save the image
        $img->save($imagePath);
    }
}

@Hredoy
Copy link
Author

Hredoy commented Dec 9, 2017

how can i get current image width which is for in upload query

@streamtw
Copy link
Member

streamtw commented Dec 11, 2017

@gwleuverink Thanks for your solution! I have added that to wiki.
@Hredoy Please check the official document of intervention/image.

@MFakhrani
Copy link

hi

private function proceedSingleUpload($file)
{
    $new_filename = $this->getNewName($file);
    $new_file_path = parent::getCurrentPath($new_filename);

    event(new ImageIsUploading($new_file_path));
    try {
        if (parent::fileIsImage($file) && !in_array($file->getMimeType(), ['image/gif', 'image/svg+xml'])) {
            // Handle image rotation
            Image::make($file->getRealPath())
                ->orientate() //Apply orientation from exif data
                ->resize(840, 560)->insert('assets/watermark.png','bottom-left',10,10)->save($new_file_path);

            // Generate a thumbnail
            if (parent::imageShouldHaveThumb($file)) {
                $this->makeThumb($new_filename);
            }
        } else {
            // Create (move) the file
            File::move($file->getRealPath(), $new_file_path);
        }
        if (config('lfm.should_change_file_mode', true)) {
            chmod($new_file_path, config('lfm.create_file_mode', 0644));
        }
    } catch (\Exception $e) {
        array_push($this->errors, parent::error('invalid'));

        Log::error($e->getMessage(), [
            'file' => $e->getFile(),
            'line' => $e->getLine(),
            'trace' => $e->getTraceAsString()
        ]);

        return false;
    }

    // TODO should be "FileWasUploaded"
    event(new ImageWasUploaded(realpath($new_file_path)));

    return $new_filename;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants