Laravel 5 intervention image upload multiple size - laravel-5

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...

Related

How to set a display icon depending on file type- Laravel

I'm currently making a web application that enables users to upload their files. Everything is working fine so far. I was asked to add a display icon on the side of the file that was uploaded.
How do I set an image to be displayed when a certain file type is uploaded?
Adding an accessor method in your file model like;
public function getIconAttribute() {
$extensions = [
'jpg' => 'jpeg.png',
'png' => 'png.png',
'pdf' => 'pdfdocument.png',
'doc' => 'wordicon.jpg',
];
return array_get($extensions,$this->extension,'unknown.png');
}
The array_get is a laravel helper that looks up a value in an array and can have a third parameter of a default if the extension is not found in the array.
Then in blade you can just icon}}' />
Or
A simpler approach where you just make sure to name the icon the same as the extension.
Then in blade you can just extension}}.png' /> and then you don't need to add the accessor at all.
It does not though allow for unknown file types but this might be ok if you are restricting the types of upload (which you should).

Save image from URL without form (Symfony 3)

I want to save an image (from URL) and relocate it to another folder without creating a form, into a controller. How can I realize it?
Download image inside Controller -> relocate it to another folder -> save image name into my database (in an existing table).
Your question is broad and you don't specify if you are using Doctrine DBAL and ORM and what exactly your problem is, so I will assume that you do use them and you are aware how to inject entity manager inside a controller action.
First you have to download the image and save the image:
$content = file_get_contents("http://example.com/image.jpg");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);
Then save the path into the database:
$imageEntity->setPath('image.jpg');
$entityManager->flush($imageEntity);

API for resizing images in laravel 5.4

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));
}

Loading dynamic images in Angular with webpack

We are using Webpack 1 with Angular 2 with typescript. In our app we would like to specify an image to be loaded in the typescript file dynamically.
For example, we have 10+ images (svg) in our pre-processed folder structure:
src\content\images\image1.svg
src\content\images\image2.svg
etc..
In the angular component html file we have the image element with the src binding:
<img [src]="_imgSrc">
In the type script file we set the correct url dynamically based on some criteria:
private _imgSrc: string;
public ngOnInit(): void {
// Some logic...
if (true) {
this._imgSrc = './../../../content/images/image2.svg';
}
else {
this._imgSrc = './../../../content/images/image1.svg';
}
// In real code, the file name will come from a typescript object returned from a web api call
We have configured webpack to use file loader for loading images:
{
test: /\.(png|jpe?g|gif|ico|svg|wav|mp3)$/,
loader: 'file?name=assets/[name].[hash].[ext]'
},
In all current scenarios (apart from this one), we specify the image path in the html file, e.g
<img src="./../../../content/images/image2.svg">
and these images are copied to the proper output folder, and the path in the typescript file is set to the output folder, so we know our webpack file loader is set up correctly for the standard static approach.
The issue we have with this more dynamic approach:
Webpack does not emit the images to the final image folder:
assets/images
The URL in the typescript file is not converted to the output
image folder.
Is it possible for webpack to find the required images via the path in the typescript file? If so, how do we do this? If not, is there an alternative approach anyone has used for dynamically loading images based on some criteria in typescript?

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