Reduce image size in laravel - php

I used ImageOptimizer package for reducing image size.
source: http://image.intervention.io/getting_started/installation
in controller
use Image;
if (Input::hasFile('title_image')) {
/*$this->validate($request,[
'photo' =>'required|image|mimes:jpg,jpeg,png|max:2048'
]);*/
$Product = Input::file('title_image');
$Product->move(public_path() . '/../../products', md5($Product->getClientOriginalName()) . ".png");
$product->title_img = "products/" . md5($Product->getClientOriginalName()) . ".png";
}
Now I want to convert image in this function when I upload. if I add this method $img = Image::make('foo.jpg')->resize(300, 200); it says storage not found error.
Now what can I do. please give me some suggestion. Thanks in advance.

Please use "resize function" before move into desire folder.
use Image;
if (Input::hasFile('title_image')) {
/*$this->validate($request,[
'photo' =>'required|image|mimes:jpg,jpeg,png|max:2048'
]);*/
$Product = Input::file('title_image');
$filename = time() . '.' . $Product->getClientOriginalExtension();
Image::make($Product)->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename) )->move(public_path() . '/../../products', md5($Product->getClientOriginalName()) . ".png");
$product->title_img = "products/" . md5($Product->getClientOriginalName()) . ".png";
}

if you want reduce or resize the image you can use the laravel image compression package before upload the image.this is link below how you can do that.
https://www.itsolutionstuff.com/post/laravel-compress-image-before-upload-exampleexample.html
image compression is need more because when browser access the url and that page having the more that 10 image then it first download the all image and send request to server to for each due to which its taking more time to load the page.so,get ride from this problem use image compression package.

Related

How to duplicate the uploaded GIF and change its size?

Can anyone suggest a solution to the issue?
There is such a code
private function saveGif() {
$path = PATH_TEMP . '/' . $this->post('hash') . '.gif';
$this->web->Request($this->post('external'), null, true)->SaveToFile($path);
}
private function saveThumbs() {
$path = PATH_TEMP . '/' . $this->post('hash');
$img = new Image($path . '.gif');
$img->toFile($path . '.jpg', 'image/jpeg');
$img->resize(350, null)->toFile(PATH_TEMP . '/m_' . $this->post('hash') .'.jpg', 'image/jpeg');
}
If you add
$img->resize(250, null)->toFile(PATH_TEMP . '/m_' . $this->post('hash') .'.gif', 'image/gif');
Then a file m_...gif of the desired size is created, but there is no animation in the gif.
Maybe of course I'm not looking there, but still.
Sorry if I'm not writing correctly.
It is necessary that when uploading a gif to the site, the file appears m_...gif, with the width I need.

How to reduce size of image in laravel when upload?

I want to reduce the image size on my e commerce site when I upload a picture.
which function should I add in this code below?
if (Input::hasFile('title_image')) {
$Product = Input::file('title_image');
Product->move(public_path() . '/../../products', $Product->getClientOriginalName() . ".png");
$product->title_img = "products/" .$Product->getClientOriginalName() . ".png";
}

Laravel move() is not uploading images everytime

I use code below:
$image_name = 'image' . time() . '.' . $request->file('image')->getClientOriginalExtension();
$destinationFolder = public_path('images');
$request->file('image')->move($destinationFolder, $image_name);
but sometime its not working, images are not storing. Im using heroku as host.
Try to use Storage facade :
$path = \Storage::putFile('images', $request->file('image'));
Laravel will generate a name automatically, About Storage facade, just see this. :)
I made function for upload file and if there is no uploaded file do it again till its upload
$imageFile->move($destinationFolder, $fileName);
if(file_exists(public_path('images') . '/' . $fileName))
return public_path('images') . '/' . $fileName;
else
self::uploadImage($imageFile, $fileName);
I used the code and it's working fine.
self::$image = $request->file('image');
self::$imageName = self::$image->getClientOriginalName();
self::$directory = 'category-images/';
self::$image->move(self::$directory, self::$imageName);
return self::$directory.self::$imageName;

Laravel 5.2: create image thumbnails in using intervention

I am trying to create image thumbnails using intervention here is my controller
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image)->resize(600, 390)->save(public_path('images/blog/' . $filename));
$image->fit(240, 157)->save(public_path('images/blog/' . $filename . '-thumbs.jpg'));
$add->image = $filename;
}
Got
Method fit does not exist.
What I am doing wrong here?
You have to update this line:
Image::make($image)->resize(600, 390)->save(public_path('images/blog/' . $filename));
to
$image = Image::make($image)->resize(600, 390)->save(public_path('images/blog/' . $filename));
because fit is a method of InterventionImage object.

Resize returns "Unsupported image format." on Magento

I need to resize a image that come from media folder, but when i use the following code magento returns this erros.
"Unsupported image format."
Code:
<?php $fileName = $post->getImagemdestacada(); ?>
<?php
$folderURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
$imageURL = $folderURL . $fileName;
$basePath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . $fileName;
$newPath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . "blog" . DS . $fileName;
$image= new Varien_Image($basePath);
$image->resize(200, 200);
$image->save($imageResized);
?>
I've already tried to use timthumb on magento, but didn't work too, aparently the magento denied my access to this folder using php.

Categories