Reputation: 113
I am trying to add watermark to images using http://image.intervention.io library but not working here's my code given below I need to implement watermark dynamically when uploading or retrieving images
Controller Code
public function store(storeNewspaperJobFormValidation $request)
{
$values = $request->input();
list($city, $catagory) = $this->_gettingValues($values);
unset($values['city_id']);
unset($values['catagory_id']);
$values['slug'] = str_slug($values['organization_name'] . '-' . rand(1, 1000), '-');
if ($request->hasFile('image_file')) {
$request->file('image_file');
$filename = $request->image_file->getClientOriginalName();
$originalfile['image_file'] = $request->image_file->storeAs('public/newpaper_jobs', $filename);
$data = array_merge($values, $originalfile);
$newspaper = new newspaper_jobad($data);
$newspaper->save();
$insertedId = $newspaper->id;
$this->_saveCatagoryNewspaperJobadbAssociations($catagory, $insertedId);
$this->_saveNewspaperJobCities($city, $insertedId);
//a flash message shold be shown that data successfully created
flash('Data successfully added')->success();
return back();
} else
flash('Data Not Inserted Image is Missing')->success();
return back();
}
Model
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;
use Laravel\Scout\Searchable;
use Intervention\Image\Facades\Image;
class newspaper_jobad extends Model
{
use Searchable;
protected $fillable = ['organization_name', 'job_apply_link', 'job_description', 'city_id', 'province_id', 'sector_id', 'image_file', 'test_id', 'newspaper_id', 'catagory_id', 'slug', 'meta_keywords', 'meta_description', 'job_title'];
public function getFilePathAttribute($value)
{
$img = Image::make("public/newpaper_jobs/$value"); //your image I assume you have in public directory
$img->insert('public/favico.png', 'bottom-right', 10, 10); //insert watermark in (also from public_directory)
$img->save("public/Storage/newpaper_jobs/$value"); //save created image (will override old image)
return ($value); //return value
}
Location of image is
public/Storage/newpaper_jobs/
Location of watermark image
public/favico.png
Upvotes: 1
Views: 4068
Reputation: 10044
After going through about 100 different "answers", here is the solution using intervention and Laravel storage:
// store uploaded image
$uploaded_image = request()->file('image')->store('public');
// add watermark and save
$image = Image::make(Storage::get($uploaded_image));
$image->insert(Storage::get('public/watermark.png'), 'bottom-right');
$image->save(Storage::path($uploaded_image));
Upvotes: 2
Reputation: 2711
$image=$request->file($formFileName);
//watermark image
$watermark =\Image::make('img/sd_wat_60.png');
//Image which you want to upload
\Image::make($image->getRealPath())->insert($watermark, 'bottom-right')->save($path.$fileFinalName);
Upvotes: 0
Reputation: 4017
I use spatie/laravel-medialibrary for managing my models media, it makes the process really easy. It's based on the concept of conversions, so you just set your model to have a media conversion with a watermark:
class ModelWithMedia extends Model implements HasMediaConversions
{
/* Traits */
use HasMediaTrait;
/* Attributes */
public function registerMediaConversions(Media $media = null)
{
// Watermark conversion
$this->addMediaConversion($media->basename.'-watermark')
->watermark(public_path('/img/watermark.png'))
->nonQueued();
}
}
It uses spatie/image for image modifications, so you can use any method available in the package and chain them together until you get your desired modification.
Hope this helps you.
Upvotes: 1