Laravel Voyager BREAD Image Edit and Delete Issue - laravel

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

Related

Delete Image with category Delete

I have this function that deletes the row but does not delete the image from the folder. I thought this would work but it doesn't. Can someone help change this code so I can delete the image from the folder on delete?
Thanks
public function PermDelete($id)
{
$category = Category::find($id);
File::delete(public_path('images/categories/'. $category->cat_image));
$delete = Category::onlyTrashed()->findOrFail($id)->forceDelete();
return redirect()->back()->with('success','Category has been permanently deleted successfully!');
}
instead of use
File::delete(public_path('images/categories/'. $category->cat_image));
you can simply use the PHP function unlink to delete
unlink(public_path('images/categories/'. $category->cat_image));
or you can debug with the below code
$path = public_path('images/categories/'. $category->cat_image);
if(\File::exists($path)){
\File::delete($path);
}else{
dd('File does not exists.');
}

How can I limit/restrict users who can access files in my storage's folder? (laravel)

I already created a symbolic link to access the public folder from storage storage/app/public
php artisan storage:link
This time I'm having questions on my mind on how to filter users who can view the image? Like, How can I implement Only Me, My Friends Only?
Any ideas Sirs?
NOTE: There is a little performance issue with this solution. This is just to answer the question, scroll down more to see other approach from other.
<input type="file" name="file">
I personally save the files inside /storage/app/files not inside the /public folder (As it tells public for everyone).
request()->file->storeAs('files', 'filename.ext');
To view & add restrictions: create a route responsible for viewing:
Route::get('files/{$filename}', 'FileController#show')->name('files.show);
Controller:
public function show($filename)
{
$path = storage_path('app/files/'.$filename);
//do some If-Else or Filters here, for user who can access the file
return response()->download($path, null, [], null);
}
To delete the file:
File::delete('app/files/'.$filename);
i think you should set a field 'image_option' in your table 'storage_file'.
image_option: string,
image_option = {privacy_level:[1, 2, 3]}
privacy_level = 1 // public
privacy_level = 2 // only me
privacy_level = 3 // only friend only

Laravel Multiple Image Issue

I need to download multiple images in a Laravel response download function without Zipper because I need to show images on a new page in Angular. Thanks.
If I understand you correctly you want images served by server?
public function get($path)
{
$file = Storage::disk('s3')->get($pathToImage);
return response($file, 200)->header('Content-Type', 'image/png');
}

Adding watermark on image from databases in Laravel 5.3

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

How to get original size image url in magento 2.0

I am pretty new in Magento 2.0 and I have been struggling to find out solution. My first question, which I didn't figure out yet, how to reach specific function? Beacuse I noticed that many people use those in Magento 1.+:
Mage::helper('cms/page')->
or
Mage::getModel('catalog/product_media_config')
(e.g Get original image url Magento (1.6.1.0))
but I can't use them in Magento 2.0. If that kind of using is not available in last version anymore, what can I use it as an alternative method to reach functions?
As for another question, I am unable to get original sized image in grid.phtml (catalog listing). Here is how to get images:
<?php
echo $block->getImage($_item, $image)->getImageUrl();
echo $block->getImage($_item, $image)->getWidth();
echo $block->getImage($_item, $image)->getHeight();
?>
And the result like that:
http://192.168.1.4/magento/pub/media/catalog/product/cache/1/small_image/240x300/beff4985b56e3afdbeabfc89641a4582/t/h/thumbnail_1.jpg240300
As I mentioned above, I want to get original sized image url instead of small_image. I hope I explained my problems. If anyone has any idea, please let me know it. Thank you!
Use the below function to get the Media base url for a particular store:
function getMediaBaseUrl() {
$om = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $om->get('Magento\Store\Model\StoreManagerInterface');
$currentStore = $storeManager->getStore();
return $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
}
Using getMediaBaseUrl() function we can find image url in Magento2:
echo $block->getMediaBaseUrl() .'catalog/product'. $_product->getImage();
1) Quick Solution by directly writing media directory path
echo $block->getUrl().'pub/media/catalog/product' . $_product->getImage();
2) Getting media directory path using StoreManager class object
namespace YourNamespace\YourModule\Block;
class YourModule extends \Magento\Framework\View\Element\Template
{
protected $_storeManager;
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
...
)
{
$this->_storeManager = $storeManager;
...
}
public function getMediaBaseUrl()
{
return $this->_storeManager
->getStore()
->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
}
}
And then in your module's template (.phtml) file, you can write as:
echo $block->getMediaBaseUrl() . $_product->getImage();

Resources