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";
}
Related
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.
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.
I am trying to upload and resize an image in Laravel 5 and the Intervention library.
My code in the Controller is this:
// Upload img
if ($req->hasFile('img')) {
// Original
$image = Image::make(Input::file('img'));
// Mid sized
$image_mid = $image->fit(500,500, function($constraint) {
$constraint->upsize();
});
// Thumbnail
$image_thumb = $image->fit(100,100, function($constraint) {
$constraint->upsize();
});
// Path
$path = public_path('upload/artists/' . $id . '/profile/');
if (!File::exists($path)) {
File::makeDirectory($path . 'original', 0775, true, true);
File::makeDirectory($path . 'midsize', 0775, true, true);
File::makeDirectory($path . 'thumb', 0775, true, true);
}
// Save images
$image->save($path . 'original/' . $imgName);
$image_mid->save($path . 'midsize/' . $imgName);
$image_thumb->save($path . 'thumb/' . $imgName);
}
I am already trying to create de directories before moving the image, but it still gives me the same error, that I don't have permissions to move the image to that folder "NotWritableException".
Before using Intervention library, I was moving the image with the ->move() method and it was creating the directory if necessary.
I would really appreciate some help..
Thank you all in advance!
Any idea what's wrong?
I end up with 1bytes files and they are wrong. Using Intervention Image & PHP. Tried many ways to get it but nothing... If I just want to display it works but I can't save the picture...
$avatar_url = 'http://graph.facebook.com/' . $id . '/picture?type=square&width=140&height=140&redirect=false';
$avatar_pic_url = json_decode(file_get_contents($avatar_url), true)['data']['url'];
dd($avatar_pic_url);
$avatar = Image::make($avatar_pic_url);
$extension = 'jpg';
// save it
$destinationPath = 'uploads/avatars/';
$filename = uniqid(). '.' . $extension;
$path_to_temp_image = $avatar->dirname . '/' . $avatar->filename;
$key = $destinationPath . $filename;
// Upload avatar to remote storage
$uploadSuccess = Storage::put($key, $path_to_temp_image);
I am trying to upload an image from customer product page (upload file option), the image is uploaded to system/upload folder and renamed with: filename + md5 hash string. So, for example I upload image.jpg, the file will be uploaded to system/upload/image.jpg.392da1267fbfa4be65f7859bb0b974d9.
Now I want to see that image uploaded by customer from admin page (I created a new module page), how can I resize and display the image file? I can get the file name from table oc_upload in database, but the phisical file is using modified name which has no extension, I cannot resize this file because it is located in system/upload folder and its extension is changed. The image resize tool can only resize images in image folder with some allowed extensions configured in Settings page.
Any idea how to display images uploaded by customer? Do I need to copy the renamed uploaded file to image folder using its original name and then resize and display it? I appreciate any of your ideas. Thank you.
Just got the solution by copying the resize() method in admin/model/tool/image.php to my controller and modify it to resize image from system/upload folder.
private function resizeImageUpload($old_filename, $width, $height, $new_filename = '') {
$extension_arr = array('jpeg', 'jpg', 'png', 'gif');
$old_image = $old_filename;
if (!empty($new_filename)) {
$extension = pathinfo($new_filename, PATHINFO_EXTENSION);
$new_image = 'cache/system/upload/' . utf8_substr($new_filename, 0, utf8_strrpos($new_filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;
} else {
$extension = pathinfo($old_filename, PATHINFO_EXTENSION);
$new_image = 'cache/system/upload/' . utf8_substr($old_filename, 0, utf8_strrpos($old_filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;
}
if (!in_array($extension, $extension_arr)) return '';
if (!is_file(DIR_IMAGE . $new_image) || (filectime(DIR_UPLOAD . $old_image) > filectime(DIR_IMAGE . $new_image))) {
$path = '';
$directories = explode('/', dirname(str_replace('../', '', $new_image)));
foreach ($directories as $directory) {
$path = $path . '/' . $directory;
if (!is_dir(DIR_IMAGE . $path)) {
#mkdir(DIR_IMAGE . $path, 0777);
}
}
list($width_orig, $height_orig) = getimagesize(DIR_UPLOAD . $old_image);
if ($width_orig != $width || $height_orig != $height) {
$image = new Image(DIR_UPLOAD . $old_image);
$image->resize($width, $height);
$image->save(DIR_IMAGE . $new_image);
} else {
$success = copy(DIR_UPLOAD . $old_image, DIR_IMAGE . $new_image);
}
}
if ($this->request->server['HTTPS']) {
return HTTPS_CATALOG . 'image/' . $new_image;
} else {
return HTTP_CATALOG . 'image/' . $new_image;
}
}