使用任何库或自定义开发使用Laravel上传图像时,如何添加水印



我试图使用http://image.intervention.io库为图像添加水印上传或检索图像时,我需要动态实现水印

控制器代码

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();
    }

模型

use IlluminateDatabaseEloquentModel;
use IlluminateSupportFacadesValidator;
use LaravelScoutSearchable;
use InterventionImageFacadesImage;

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
    }

图像的位置为

public/Storage/newpaper_jobs/

水印图像的位置

public/favico.png

经过大约100个不同的"答案"后,这是使用干预和laravel存储的解决方案:

// 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));

我使用spatie/laravel-Medialibrary来管理我的模型媒体,这使得该过程非常容易。它基于转换的概念,因此您只需将模型设置为具有水印的媒体转换:

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();
    }
}

它使用Spatie/Image进行图像修改,因此您可以使用包装中的任何方法并将它们链接在一起,直到您进行所需的修改为止。

希望这对您有帮助。

$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);

相关内容

  • 没有找到相关文章

最新更新