2016-12-16 101 views
1

添加水印圖像從數據庫中我想上的圖像從數據庫中使用Laravel干預映像包添加水印。在我的數據庫表中,我保存了圖像的路徑。我在我的模型中使用訪問器來訪問圖像路徑的字段,但我得到此錯誤:在Laravel 5.3

方法插入不存在。

這裏是我的模型:

My model

這裏是我的刀鋒:

my view

+2

歡迎的StackOverflow!請不要將代碼包含在屏幕截圖中!只要在這裏發佈並正確設計。 – manniL

+0

你會嘗試在照片集合中插入水印,這是行不通的。嘗試從文件路徑加載照片,然後在其中插入水印照片。 – JackPoint

+0

感謝您指出試圖爲水印收集照片的錯誤。我遇到的問題是,我正在處理的網站已經運行了一段時間。圖像被盜,客戶需要圖像加水印。此圖片已上傳。指點就可以覆蓋舊的圖像 –

回答

2
public function getFilePathAttribute($value){ 
    $img = Image::make(public_path($value)); //your image I assume you have in public directory 
    $img->insert(public_path('watermark.png'), 'bottom-right', 10, 10); //insert watermark in (also from public_directory) 
    $img->save(public_path($value)); //save created image (will override old image) 
    return $value; //return value 
} 

這是更好地做它的上傳,所以你做一次不總是試圖從數據庫訪問圖像路徑時(較少進程)僅供參考:這將保存已經加了水印的圖像

+0

感謝,這將是非常有益的。 –

+0

@KiarieHenry這是更好地在上傳 – Froxz

+0

我也在想同樣的,但我有兩個文件夾,原始照片文件夾和水印的照片。現在我知道這個竅門。由於@Froxz –

0
//just add this code in your view....it will work fin 

<?php 
    function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) { 
     list($width, $height) = getimagesize($SourceFile); 
     $image_p = imagecreatetruecolor($width, $height); 
     $image = imagecreatefrompng($SourceFile); 
     imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height); 
     $black = imagecolorallocate($image_p, 0, 0, 0); 
     $font = 'arial.ttf'; 
     $font_size = 20; 
     imagettftext($image_p, $font_size, 20, 200, 200, $black, $font, $WaterMarkText); 
     if ($DestinationFile<>'') { 
      imagejpeg ($image_p, $DestinationFile, 100); 
     } else { 
      header('Content-Type: image/png'); 
      imagejpeg($image_p, null, 100); 
     }; 
     imagedestroy($image); 
     imagedestroy($image_p); 
    }; 
    ?> 

    <?php 
    $SourceFile = '1493375196.png';//path from public folder 
    $DestinationFile = 'vin.png'; 
    $WaterMarkText = 'Copyright KonfettiBox.com'; 
    watermarkImage($SourceFile, $WaterMarkText, $DestinationFile); 
    ?