zxbodya / yii2-gallery-manager

93 stars 61 forks source link

WaterMark #25

Open nazakar opened 8 years ago

nazakar commented 8 years ago

Is it possible to create watermark for images? Thanks

zxbodya commented 8 years ago

Yes.

http://imagine.readthedocs.org/en/latest/usage/introduction.html#image-watermarking

You can do it in versions functions.

nazakar commented 8 years ago

Thanks, but i can't find versions functions in GalleryBehavior vestions declaration is public $versions; Thanks

zxbodya commented 8 years ago

I mean versions field in behavior configuration, it would be something like:

public function behaviors()
{
    return [
         'galleryBehavior' => [
             'class' => GalleryBehavior::className(),
             'type' => 'product',
             'extension' => 'jpg',
             'directory' => Yii::getAlias('@webroot') . '/images/product/gallery',
             'url' => Yii::getAlias('@web') . '/images/product/gallery',
             'versions' => [
                 'medium' => function ($img) {
                     /** @var Imagine\Image\ImageInterface $img */
                     $dstSize = $img->getSize();
                     $maxWidth = 800;
                     if ($dstSize->getWidth() > $maxWidth) {
                         $dstSize = $dstSize->widen($maxWidth);
                     }
                     // load watermark
                     $watermark = $imagine->open('/my/watermark.png');
                     $wSize     = $watermark->getSize();
                     // calculare watermark posiotion 
                     $bottomRight = new Imagine\Image\Point($dstSize->getWidth() - $wSize->getWidth(), $dstSize->getHeight() - $wSize->getHeight());

                     return $img
                         ->copy()
                         ->resize($dstSize)
                         // draw watermark
                         ->paste($watermark, $bottomRight);
                 },
             ]
         ]
    ];
}
nazakar commented 8 years ago

Thank, I'll try

nazakar commented 8 years ago

Unfortunately - after this, no medium image created and error "NetworkError: 500 Internal Server Error - http://localhost/property/backend/property/galleryApi?type=img&behaviorName=galleryBehavior&galleryId=1&action=ajaxUpload" my watermark is Yii::getAlias('@front').'/uploads/cancel-off.png'

zxbodya commented 8 years ago

can you show your error? and maybe code you have?

nazakar commented 8 years ago

I paste your code with path to my image

zxbodya commented 8 years ago

It is just an example... I have not tested it. Maybe error is here $watermark = $imagine->open('/my/watermark.png');

Try replacing it with: $watermark = Image::getImagine()->open('/my/watermark.png');

nazakar commented 8 years ago

still error

2016-02-09 18:07 GMT+02:00 Bogdan Savluk notifications@github.com:

It is just an example... I have not tested it. Maybe error is here $watermark = $imagine->open('/my/watermark.png');

Try replacing it with: $watermark = Image::getImagine()->open('/my/watermark.png');

— Reply to this email directly or view it on GitHub https://github.com/zxbodya/yii2-gallery-manager/issues/25#issuecomment-181931232 .

Nazelina Karanfilian

nazakar commented 8 years ago

i simply put this code in my view $imagine = new \Imagine\Imagick\Imagine(); $watermark = $imagine->open(Yii::getAlias('@front').'/uploads/cancel-off.png'); $image1 = $imagine->open('../uploads/small.jpg'); $size = $image->getSize(); $wSize = $watermark->getSize();

$bottomRight = new Imagine\Image\Point($size->getWidth() - $wSize->getWidth(), $size->getHeight() - $wSize->getHeight());

$image1->paste($watermark, $bottomRight); and i get Imagick not installed, maybe the problem is with instance Please Help me

nazakar commented 8 years ago

Finally, worked!!! Thanks Code is $imagine= Image::getImagine(); $watermark =$imagine->open(Yii::getAlias('@front').'/uploads/cancel-off.png'); //Yii::getAlias('@front').'/uploads/cancel-off.png'; $wSize = $watermark->getSize(); //calculare watermark posiotion $bottomRight = new \Imagine\Image\Point($dstSize->getWidth() - $wSize->getWidth(), $dstSize->getHeight() - $wSize->getHeight());

                 return $img
                     ->copy()
                     ->resize($dstSize)
                 ->paste($watermark, $bottomRight);

and don't forget use yii\imagine\Image;