API for resizing images in laravel 5.4 - laravel

i am working on a marketing website with laravel 5.4
i need to upload and load lots of images on the website, if i load the original image every time, it will going slow. so i need your suggestions
*
should i store the image with two quality? (original and
resized)
store the original images, resize them when they loaded
on the screen? (need an API)
share some ideas and the solutions please!
if you have such an API, please share!

Laravel does not have a default resize of image. But most laravel developers use 'Image intervention' in handling the image. (Easy to use)
To install (Image intervention):
STEP 1 Run
composer require intervention/image
STEP 2 On your config/app.php:
In the $providers array, add the following:
Intervention\Image\ImageServiceProvider::class
In the $aliases array,add the following:
'Image' => Intervention\Image\Facades\Image::class
If you have problems your GD librabry is missing, intall it
PHP5: sudo apt-get install php5-gd
PHP7: sudo apt-get install php7.0-gd
~~ To use on your controller ~~
STEP 3 On top of your controller
use Intervention\Image\ImageManagerStatic as Image;
STEP 4 On your method (there are several ways but this will give you an idea)
if($request->hasFile('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save(public_path('images/ServiceImages/'
.$filename));
}

Related

Uploading image in Laravel after specific time doesn't load or display on server

I upload an image on form vue to server laravel and store it in public in folder image_cards correctly and display it correctly. But after maybe one hour or two, it doesn't show. What is the problem I have?
if ($request->hasFile('image')) {
$file = $request->file('image');
$filename = $file->getClientOriginalName();
$path = $request->file('image')->move(public_path('image_cards'), $filename);
$card->image = $filename;
}
Edit: the problem with Heroku is it doesn't store images and save its path. The other web host saves the path to the image.
You are missing image path and you need to return full image path. please use following code to return image path
$card->image=public_path('image_cards').'/'.$filename;
the solution is heroku in documentation it doesn't save the path of file after some time like one or two hour –

Serialization of 'Illuminate\Http\UploadedFile' is not allowed ,When tried to update image

I tried to update my image for category banner using Laravel(v8).
This is my code to update any image, first thing I checked was whether there is any image. If there ware any, I removed them. Then I added the new image.
But it showed me this error, Serialization of 'Illuminate\Http\UploadedFile' is not allowed ,When tried to update image.
Please help me to solve this.
Edit: My image was update without delete the old image.
if($request->hasFile('image') && $request->file('image')->isValid()){
$image = $request->file('image');
$imageName= $image->getClientOriginalName().Str::random(5).'.'.$image->getClientOriginalExtension();
$image->storeAs('category_image', $imageName);
$category = Category::find($id);
if($category->banner !==null){
unlink(public_path().'/allfiles/category_image/'.$category->banner);
}
$category->banner = $imageName;
$category->save();
}
Category::find($id)->update([
'name' => trim($request->category)
]);
Error messages,
This is error detail's
This error showing
I don't know what was the actual problem, I removed old image link manually from the database and then tried to update a new image. Then it got updated successfully. Next, I updated the same image again to confirm whether it works or not, and it worked without any error. Maybe it was the problem with the browser session or the previous database image link.

Add watermark to PDF in laravel

I upload PDFs using File/browse option from frontend and As I Upload, I want to have a watermark uploaded to those PDFs automatically. Below is a sample code for upload pdf in Store function of the controller
if ($request->hasFile('qpid')) {
$paperuploads = $request->file('qpid');
foreach ($paperuploads as $paperupload){
$paperuploadfilename = 'QP_'. time() . '.' . $paperupload->getClientOriginalExtension();
$destinationPath = public_path('/storage/papers');
Is there any way to dynamically add watermark to PDF? Thanks
For benefit of others, I got this solved by using following Github repository:
https://github.com/ajaxray/markpdf
Then using symfony process to call MarkPDF and thereby getting this implemented.

Laravel 5 intervention image upload multiple size

I am using laravel 5 and intervention, and would like to store multiple sizes of a image when it is uploaded via a form. Can anybody guide me
So i don't know what you already did. So let's start from the beginning.
First of all you need the Intervention Library. So switch to your main Folder (containing your composer.json file)
And type
composer.phar require intervention/image
Or just add "intervention/image": "~2.1" to your require array in composer.json. ( And do a composer update after that )
"require": {
"laravel/framework": "5.0.*",
"intervention/image": "~2.1"
},
Now you have to add
'Intervention\Image\ImageServiceProvider',
to the providers array
and
'Image' => 'Intervention\Image\Facades\Image'
to your aliases Array. Both in config/app.php
Now you could create a "upload function" somewhere in a controller like
public function upload() {
$image = \Image::make(\Input::file('image'));
$path = storage_path('app')."/";
// encode image to png
$image->encode('png');
// save original
$image->save($path."original.png");
//resize
$image->resize(300,200);
// save resized
$image->save($path."resized.png");
}
This would save two images to the storage/app folder. One in the original size and one resized to 300x200.
This code is only an example, it does not contain any checks, for valid images or stuff like that. It just takes a file (assuming an image) and saves it two times.
And of course you also don't need to encode to png...

Magento 1.7 - Product Import - Images do not load

I have been trying to import products into Magento and images. The products are being imported and created properly but the images to not get imported.
I am aware of the basics of importing as I have done it in the past but cannot figure out what the issue is and what to do next.
some notes how and what I did:
I am aware to add a "/" before the image name
image name is case sensitive
upload images in media/import folder
media folder to be 777
Tried different folders like var/import or
media/catalog/product/import
Removed the .htcaccess file in media
Flushed cache
when i upload manually an image on a product it does show up properly
i tried specifying the _media_attribute_id in a column as 88 maybe
that should help but it didn't
If you have a Mage_Catalog_Model_Product, all you need to do is this:
$fn = '/absolute/path/to/image.jpg';
$types = array('thumbnail', 'small_image', 'image'); // or null if you already have the product's thumbnail, small image, and image set
$move = true; // move the source file, don't leave it sitting around
$exclude = false; // enabled in product view
$label = 'Turquoise';
$product->addImageToMediaGallery($fn, $types, $move, $exclude); // Strictly speaking, only the first parameter is required
if(!empty($label)) {
$gallery = $product->getData('media_gallery');
$gallery_img = array_pop($gallery);
$gallery_img['label'] = $label;
array_push($gallery['images'], $gallery_img);
$product->setData('media_gallery', $gallery);
}
$product->save();
I have faced the same problem while uploading product image. And i tested many methods to figure that out. But nothing helped me. At last in one of the tutorial I have seen that the image name and the sku name has to be same. with the extension of file extension. So i followed it and by God's grace it worked. You may try that also. It worked for me. I had spent almost one full day to figure that out.

Resources