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.
Related
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 use laravel 5.6
I want to upload files on 2 folders at once. So if the image uploaded successfully in the product folder, I want the image uploaded in the thumbs folder as well
I try like this :
public function uploadImage($file)
{
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPath = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product';
if(!File::exists($destinationPath)) {
File::makeDirectory($destinationPath, 0755, true);
}
$file->move($destinationPath, $fileName);
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
If the code run, it just success to upload in the product folder. It did not upload in the thumbs folder
There exist error like this :
message Unable to find file (/tmp/phpUSxbEJ).
exception Intervention\Image\Exception\NotReadableException
I try run this code :
public function uploadImage($file)
{
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
So I remove code to upload in product folder. I try it and it works. It success upload on the thumbs folder
So I think in one process, it only upload in one folder
Is there any other way to upload on 2 folders?
You first upload the temporary file which will be removed once you save it on your disk, thats why u can't reuse it, instead of reusing it, you fetch the saved image and do resizing on it and save it with a different name:
public function uploadImage($file)
{
...
$file->move($destinationPath, $fileName);
//$file here doesn't exist anymore, hence it can't be read
...
$uploadedFile = Storage::get($destinationPath . DIRECTORY_SEPARATOR . $filename);
$image_resize = Image::make($uploadedFile);
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
I find a solution
I try like this :
$file = $file->move($destinationPath, $fileName);
It works
I have my upload php code where my intent is , obtained file from $_files,
add a random number between 0 and 9999 to the name of image like this:
image sent : image.jpg
before saving : image321.jpg
the image is saved in my upload folder but the filename are like
"php2983204tmp"
if ($file !== null) {
$rand = rand(0000,9999);
$path = "some_path";
$file_name = $file->getClientOriginalName(); // file
$extension = $file->getClientOriginalExtension(); // jpg
$file->move($path, $file_name.$rand.$extension);
$response = "File loaded successfully: " . $file_name.$extension;
$response .= '<br>size: ' . filesize($path . '/' . $file->getClientOriginalName()) / 1024 . ' kb';
return new Response($response);
any ideas to fix?
The filename in your example is php and your extension is tmp. None of them have the . that you are missing.
You need to add the dot . as a string after the $file_name and $rand, before the $extension like this:
$file->move($path, $file_name.$rand. "." .$extension);
TIME is always unique identity, use it as below (maybe helpful):
if ($file !== null) {
$rand = rand(0000,9999).time();
$path = "some_path";
$file_name = $file->getClientOriginalName(); // file
$extension = $file->getClientOriginalExtension(); // jpg
$file->move($path, $file_name.$rand.$extension);
$response = "File loaded successfully: " . $file_name.$extension;
$response .= '<br>size: ' . filesize($path . '/' . $file->getClientOriginalName()) / 1024 . ' kb';
return new Response($response);
You need add in the desired chars to the actual string.
$file->move($path, $file_name.$rand.".".$extension);
But I have to say, I am against how you've done this, you don't even check if the "newly" created string already exists in the directory. Its better to hash the time of upload with the original filename, rename the file to the new hash and use a database to point to the file as this way the filename collisions don't occur.
$fn = md5(microtime(true) . $extension . $file_name);
$file->move($path, $fn);
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;
}
}