Saving Images in two size in Laravel Livewire - laravel

I have a form where I upload images using Laravel Livewire.
In my class I can save the files in original size using:
$filenames = collect($this->photos)->map->store('posts');
The issue that I faced is that I don't know how to save the same images (with same name as above) in another folder (thumbnail folder) in another size. For instance, width 200px.

I have used this with Laravel 8 and it works. I can't see what you have before this code but you can iterate through your photos and save 2 images, one in the parent folder and one resized in a "thumbnail" folder;
$ext = $image->getClientOriginalExtension();
$date = new Carbon;
$folder = "photos";
// Save original file
$orig_filename = $date->format('YmdHisv') . '.' . $ext;
$img_url = $image->storeAs('public/' . $folder, $orig_filename);
// Create and store thumbnail
$thumbnail = $image->storeAs('public/' . $folder . '/thumbnails', $orig_filename);
$thumb_path = Storage::path('public/' . $folder . '/thumbnails/' . $orig_filename);
$thumb_img = Image::make($thumb_path)->resize(150, 93, function ($constraint) {
$constraint->aspectRatio();
});
$thumb_img->save($path);
This is the package Intervention\Image and here is a great tutorial on that.

Related

How to store image as URL in Laravel 8.*

In my application I add photos that are then scaled, I call them to my views using {{asset()}}
Everything works fine, but for my mobile app I need to send to API URL of image instead of just image path called from db.
That's how I save images now:
$image = $request->photo_patch;
$filename = time() . '.' . $image->extension();
$event->photo_patch = $filename;
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(1200, 630);
$image_resize->save('storage/images/' . ($filename));
$event->save();
Example of saved image name:
1630531230.jpg
That's how I get image on view:
<img src="{{ asset('storage/images/' . $eventView->photo_patch) }}">
What I tried:
$url = Storage::url($filename);
$event->photo_patch = $url;
After this file name looks like this
"/storage/1631043493.png"
But that isnt really URL
What can I do to save photo path like this:
"localhost/storage/images/1631043493.png"
Edit:
# Фарид Ахмедов suggested to call whole URL in API.
How can I do that then?
This is what I tried:
Route::get('/events/{id}', function($id){
return [
Event::findOrFail($id),
'image' => asset('storage/images/'.$this->photo_patch)
];
});
That's it.
url(Storage::url($filename));
But I think, you don't need to save the whole path. You can convert it to full URL before sending response.

Image src not get right pat in Laravel

I tried to display an image in view page, but the src attribute is incorrect.
C:\xampp\htdocs\new\public\uplode/Screenshot_5.png
but src is:
http://localhost/new/C:\xampp\htdocs\new\public\uplode/Screenshot_5.png
function is:`public function bpl(){
$ob=banner::all();
return view('admin.page.bpl',['var'=>$ob]);
}
View file is:<img src="{{asset($ob->bannerimage)}}">.
How do I get the right path in view file.
Try this on your view file.Use this link i belive it will resolve your issue.
<img src="{{asset('uplode/Screenshot_5.png')}}"/>
$image = $request->file('image');
$filename = time().'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/uploads');
$image->move($destinationPath, $filename);
you should save only the file name and extension like (filename.png) on database. you have save desktop actual path which contains /\ both so image destination is not working properly.

Laravel 5.1 Snappy pdf image not rendering in pdf file

I am using barryvdh/laravel-snappy to generate pdf file. I have two image files 1. yourlogohere.png is in public/image/ folder and 2. logo2.png is in folder other than public i.e. storage/app/logo and to get this file I defined a route (www.example.org/logo/logo.png) and use following code to access it.
public function logo($filename)
{
$file = Storage::disk('local_logo')->get($filename);
$mime = 'image/png';
return (new Response($file, 200))->header('Content-Type', $mime);
}
Problem:
When I use following code to generate pdf from the html containing the first file, pdf contains the yourlogohere.png image
$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/images/yourlogohere.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);
But when I do exact same thing for the second file, pdf does not render the image.(When I open the link http://www.example.org/logo/logo2.png in browser I get the image). What am I missing?
$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/logo/logo2.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);
Thanks,
K
You can also do:
<img src="data:image/jpeg;base64,
{{ base64_encode(#file_get_contents(url('your.image.url'))) }}">
I think I got the what the problem is, the route to access the image is via auth, even when user is logged in while accessing the snappy, the wkhtmltopdf exe runs in a shell that is totally different session. Now the right fix would be to be embed the image in the html that is sent to snappy instead of the link, Which I am not sure how I will do? Any suggestions welcome there.
Update:
I as able to convert the image to data:image/png;base64, and embed it in html.
$html = view('mytemplate.default', compact('variable1', 'variable2'))->render();
/*Convert logo image to base64 before pdf conversion*/
//search for <img src="http://example.org/mytemplate/logo/logo1.png">" and replace the src with data:image/png;base64,
$search = '/(<img\s+src=["\'])([^"\']+)(\/mytemplate\/logo\/)(.*)(\.)(.*?)(["\']\s+[^>]+>)/';
$html = preg_replace_callback($search, function ($matches) use ($invoicedetail) {
$filename = $matches[4] . $matches[5] . $matches[6];
$file = Storage::disk('local_logo')->get('yourlogohere.png');
$mime = "image/png";
$mytemplate = MyTemplate::where('logo_filename', '=', $filename)->first();
if (!empty($mytemplate)) {
$file = Storage::disk('local_logo')->get($mytemplate->logo_filename);
$mime = $mytemplate->logo_mime;
}
$base64 = 'data:' . $mime . ';base64,' . base64_encode($file);
return $matches[1] . $base64 . $matches[7];
}, $html);
$pdf_filename = 'template' . $mytemlpate->id . '.pdf';
$path = storage_path('app' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $filename);
$snappy = App::make('snappy.pdf');

Laravel file upload path and view rendering

I successfully upload a file and store its path with the following snippet:
/*Image Handling*/
$file = Input::file('profilePicture');
$destinationPath = public_path().'/images/';
$filename = $file->getClientOriginalName();
Input::file('profilePicture')->move($destinationPath, $filename);
//Profile image
$profileImg = $destinationPath.$filename;
then I store profileImg in database. This is what it looks like:
/Applications/MAMP/htdocs/devproject/public/images/picture.jpeg
Now I want to show this picture in one of the views. So I did this:
<a class="th" href="{{URL::to('/')}}">{{ HTML::image($details->profileImg, "work", array('style' => 'width:100px;height:100px;')) }}</a>
and this is how it is rendered:
<a class="th" href="http://localhost:8888/devproject/index.php"><img src="http://localhost:8888/devproject/Applications/MAMP/htdocs/devproject/public/images/picture.jpeg" style="width:100px;height:100px;" alt="work"></a>
This of course doesn't work because the path is incorrect but it is how it was stored. I need the path to the image to be :
/public/images/picture.jpeg
instead of this:
/Applications/MAMP/htdocs/devproject/public/images/picture.jpeg
as this would fit in the url and show the picture. Any advices on how to achieve this will be appreciated.
Don't save the whole path to the model, save just the filename:
$profileImg = $filename;
Then, instead of using $details->profileImg by itself, use:
asset('images/' . $details->profileImg)
i.e.:
{{ HTML::image('images/' . $details->profileImg, "work", array('style' => 'width:100px;height:100px;')) }}

How to resize the categories images in Magento?

How to resize the categories images in Magento? I used the following code to resize product images, but I'm not able to use it for showing categories images:
$this->helper('catalog/image')->init($_product, 'small_image')->resize(170);
If I am not mistaken, it should be :
init($_product, 'small_image')->resize(100,100);
// single parameter work with 'image'
init($_product, 'image')->resize(100);
// How about this
$this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $image->getFile())->resize(100,100);
Here is the new code. If you tell me before which extension used, we were solve quickly.
If I am not mistaken, you used Template Monster Catalog Image Extension. So, there is a function inside of the extension, like below.
// app/design/frontend/default/default/template/easycatalogimg/homepage.phtml
<?php echo Mage::helper('easycatalogimg/image')->resize($imageUrl, $width , $height) ?>
<?php
$_file_name = $cat->getThumbnail(); // Here $cat is category data array
$_media_dir = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS;
$cache_dir = $_media_dir . 'resize' . DS; // Here i create a resize folder. for upload new category image
if (file_exists($cache_dir . $_file_name)) {
$catImg =Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . 'resize' . DS . $_file_name;
} elseif (file_exists($_media_dir . $_file_name)) {
if (!is_dir($cache_dir)) {
mkdir($cache_dir);
}
$_image = new Varien_Image($_media_dir . $_file_name);
$_image->constrainOnly(true);
$_image->keepAspectRatio(false);
$_image->keepFrame(false);
$_image->keepTransparency(true);
$_image->resize(224, 174); // change image height, width
$_image->save($cache_dir . $_file_name);
$catImg = Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . 'resize' . DS . $_file_name;
}
echo $catImg ; // display resize category thumbnail imagename
?>
" />
For more clarification see here
If you want to resize category images, here is a free module based on core image resize. As I was struggling myself so, ended up creating this extension to resize category images with ease same as product image resizing.
I realize that I'm late to the party but I had a chance to look at this recently and using init() to resize product images is perfectly fine as described in the question but you shouldn't have to use the same function to resize a single image, through a function where the image itself (third parameter) is an optional value.
Here's what I've done to resize the image, this is for any image:
$model = Mage::getModel('catalog/product_image');
$model->setBaseFile('test.png');
$model->setWidth(100);
$model->resize();
$model->saveFile();
echo $model->getUrl();
// generates the following url: http://example.com/media/catalog/product/cache/1//9df78eab33525d08d6e5fb8d27136e95/test.png
If you don't want to save your image in the media directory then you can use Varien_Image object to achieve that:
$varienImage = new Varien_Image('test.png');
$varienImage->resize(100);
$varienImage->save('var', 'test2.png');
save() takes the first parameter as the directory where you'd like the new file to be saved and second parameters is the name of the new file.
The steps are similar as to what the init function does aside from dealing with a bunch of logic with the product itself.
If you need to resize the category image as it keeps shrinking to 475px wide, you can do so by deleting the 'width="475"' from the following template:
app/design/frontend/default/default/template/catalog/category/view.phtml
You might also need to turn off or clear the cache in the backend at: System --> Cache Management --> Image Cache --> Clear.
From: http://www.pridedesign.ie/content/magento-resize-category-image

Resources