Adding watermark on image from databases in Laravel 5.3 - laravel

I am trying to add watermark on image from database in Laravel using Intervention Image package. In my database table I am saving the path of the image. I am using Accessors in my model to get access to the field of the image path but I am getting this error:
Method insert does not exist.
Here is my model:
Here is my blade:

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
}
It is better to do it on upload so you do it Once not always when trying to access the image path from DB (less proccess) FYI: this will save already watermarked image

Related

Adding Image PHPWord in Laravel

So I want to add an header image to my document in PHPWord in Laravel.
So this is my code
public function generateDocx()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$headerLogo = 'http://127.0.0.1:8000/img/logoAnevBulanan.png';
$section->addImage($headerLogo);
// Bunch of line to download the docx
}
And I got Maximum execution time of 60 seconds exceeded, when I try the other method from the documentation, I still got the same error. I try to use asset() helper from laravel and still did not work
Try getting your image using local path instead of URL
$source = file_get_contents('/path/to/my/images/earth.jpg');
$textrun->addImage($source);
Refer to documentation : https://phpword.readthedocs.io/en/latest/elements.html#images

Laravel Backpack save image with original name

I am using laravel backpack for my backoffice and i need to upload some images, i copy the code from their website and works fine, but i need the original name on the images, i tried some things, but is not working.
What i need to change here?
public function setImageAttribute($value)
{
$attribute_name = "image1";
$disk = "uploads"; // or use your own disk, defined in config/filesystems.php
$destination_path = "productimg"; // path relative to the disk above
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk)->delete($this->{$attribute_name});
// set null in the database column
$this->attributes[$attribute_name] = null;
}
// if a base64 was sent, store it in the db
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image1 = \Image::make($value)->encode('jpg', 90);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image1->stream());
// 3. Delete the previous image, if there was one.
\Storage::disk($disk)->delete($this->{$attribute_name});
// 4. Save the public path to the database
// but first, remove "public/" from the path, since we're pointing to it from the root folder
// that way, what gets saved in the database is the user-accesible URL
$public_destination_path = Str::replaceFirst('uploads/', '', $destination_path);
$this->attributes[$attribute_name] = $public_destination_path.'/'.$filename;
}
}
As far as I know, Backpack gets the content of the image file base 64 encoded. So the original file information is discarded.
If you need it, you should use the upload field type (but you won't see the image preview) or create your own field type modifying the original (/vendor/backpack/pro/resources/views/fields/image.blade.php).
For example you can create a support <input type="hidden" name="{{ $field['name'] }}__filename"> where you store the file name via JS.
you need to replace this
$filename = md5($value.time()).'.jpg';
with this
$filename = $value->getClientOriginalName();
but you have to be sure that all your images has an unique name or you must add a random prefix to file name to prevent duplacated names

Laravel Voyager BREAD Image Edit and Delete Issue

I have ran into a problem with image upload using voyager BREAD System. If I delete or update an image using BREAD the old image not replaced or deleted. It is still in the storage directory. I was using latest version of voyager with laravel 5.5. Is there any solution to this problem? Thank you in advance.
There is no public function deleteBreadImages($data, $rows) {...} anymore in Laravel Voyager 1.2 class file vendor/tcg/voyager/src/http/controllers/VoyagerBreadController.php
After two days of googling same issue I figured out the Hard Solution...
in my case image field name is img and model name is Company
code is executed while the Model BREAD is updating
Works on Voyager 1.2
Hope it helps ))
use Storage;
class Company extends Model
{
public static function boot()
{
parent::boot();
static::updating(function($model)
{
// Check if Old File Exists
$oldFileExists = Storage::disk('public')->exists($model->original['img']);
// If Old File Exists DELETE it, else Continue Adding New Image
if($oldFileExists)
{
//Get File Extension:: .jpg .png .gif
$fileExt = substr(strrchr($model->original['img'], "."), 0);
//If File is not .GIF
if($fileExt != '.gif'){
// Delete Old Non-GIF Image
Storage::disk('public')->delete($model->original['img']);
}
// Find .gif , -static.gif Old Images And Delete
else{
// filename-static.gif
$staticOld = str_replace($fileExt,"-static".$fileExt,$model->original['img']);
// Delete Old Image.gif
Storage::disk('public')->delete($model->original['img']);
// Delete Old Image-static .gif if Exists
if($staticOld) Storage::disk('public')->delete($staticOld);
}
}
});
}
}
Hey please check below file in your project's vendor/tcg/voyager/src/http/controllers/VoyagerBreadController.php directory
and check for this
public function deleteBreadImages($data, $rows) {...}
function at line number 403.
in this function find $this->deleteFileIfExists($data->{$row->field}); make it un-comment and check.
may i hope this helps you

Laravel and Cloudfront: Image caching issue

If I load this image from Cloudfront into my browser it shows me the correct and latest version:
https://d8z37rm9cmbl3.cloudfront.net/1/groupings/extras/27/FS_27_0003.JPG
However, when I load it programmatically in my Laravel app from my Forge server, it shows an old cached version of the image (which in fact no longer exists on AWS). The call is exactly the same within the program and the app's URL is:
http://imagetester.artlookonline.com/img
The code for the procedure is very simple and uses Intervention Image (but not their cache):
public function serveDirectImage() {
$img = Image::make('https://d8z37rm9cmbl3.cloudfront.net/1/groupings/extras/27/FS_27_0003.JPG');
// create response and add encoded image data
$response = Response::make($img->encode('jpg', 70));
// set content-type etc header
$response->header('Content-Type', 'image/png')
->header("Cache-control", "no-store");
// output
return $response;
}

How to get just one image path from multiple images paths in laravel?

I am woring on multiple images upload in laravel 5.2 and i'm not using Intervention. I need just one image for thumbnail view and all the images after clicking the thumbnail for image gallery. I have been successfully uploading images but not been able to fetch into view.I have two tables posts and images.
posts table
| id | title description
images table
| id | p_id | path
I have setup relationship like this.
// Post model
public function images()
{
return $this->hasMany('App\Models\Image','p_id');
}
//Image model
public function posts()
{
return $this->belongsTo('App\Models\Post','p_id');
}
$posts=Post::with('images')->get();
then, this gives the first image path, you can use for thumbnail.
$posts[0]->images[0]->path;
and, this gives all the images path.
foreach($posts as $post)
{
foreach($post->images as $image)
{
$image->path;
}
}

Resources