I want to store my image upload path in table.Currently it returns
/storage/images/user/business_card/1524811791.jpg"
I want to store full access URL for example abc.com/storage/images/user/business_card/1524811791.jpg
How can i do this ?
Image Upload Code :
if(Input::file('profile_picture'))
{
$profilepic = Input::file('profile_picture');
$filename = time() . '.' . $profilepic->getClientOriginalExtension();
$request->file('profile_picture')->storeAs('public/images/user/profile', $filename);
$accessUrl = Storage::url('images/user/profile'). '/' . $filename;
$url = Storage::put('public/user/profile/', $filename);
$saveUserInformation->profile_picture = $accessUrl;
$saveUserInformation->save();
}
First of all it is bad practice. Imagine a problem when your domain changes, then you need to make huge work in database. Therefore the relative path is better.
But If you realy need absolute path you can take domain:
{{ Request::root() }}
Or
{{ Request::server ("SERVER_NAME") }}
and add it before storage path.
Good luck!
$saveUserInformation->profile_picture = URL::to('/').$accessUrl;
Related
I am uploading images to storage/uploads and have a queue job that uploads those images to AWS. Here is the code of the job:
$path = storage_path() . '/uploads/' . $this->fileId;
$fileName = $this->fileId . '.png';
if (Storage::disk('s3images')->put('profile/' . $fileName, fopen($path, 'r+'))) {
File::delete($path);
}
Once the image has been uploaded to AWS the job is supposed to delete the image from storage/uploads but it's not doing that. The images are successfully uploaded to AWS. I tried to delete specific files directly without the if-statement but nothing seems to work. I even tried with Storage::delete but that didn't work either. Could someone point me in the right direction please?
You may use
Storage::delete('upload/profile/' . $fileName);
See Documentation
If your specifying the full directory path of the file you can use unlink a native way to delete files from the server.
$path = storage_path() . '/uploads/' . $this->fileId;
$fileName = $this->fileId . '.png';
$isUploaded = Storage::disk('s3images')->put('profile/' . $fileName, fopen($path, 'r+'));
if ($isUploaded) {
unlink($path);
}
Documentation: unlink php
When I store an image in Laravel by doing:
$path = $request->file('myImage')->store('public/src/');
It returns the full path, but how do I get only the filename it was given?
This is an example of the returned path:
public/src/ltX4COwEmvxVqX4Lol81qfJZuPTrQO6S2jsicuyp.png
Here, you can try this one.
$fileNameWithExt = $request->file('myImage')->getClientOriginalName();
$fileNameWithExt = str_replace(" ", "_", $fileNameWithExt);
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$filename = preg_replace("/[^a-zA-Z0-9\s]/", "", $filename);
$filename = urlencode($filename);
$extension = $request->file('myImage')->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extension;
$path = $request->file('myImage')->storeAs('public/src/',$fileNameToStore);
return $fileNameToStore;
You will get your stored filename in $fileNameToStore.
Also, all the spaces will be replaced with "_" and you will get your stored filename with current time attached with it, which will help you differentiate between two files with the same name.
Since $path returns the full path to the saved file, it contains its generated name.
You have just to parse this string :
$extension = explode('/', $path);
$filename = end($extension)
which will give you ltX4COwEmvxVqX4Lol81qfJZuPTrQO6S2jsicuyp.png
In Laravel, the store() method generates the name dynamically .. so you can't get it from the store() method.
But you can use storeAs() method. Basically the store() method is calling the storeAs() method. So:
$path = $request->file('myImage')->store('public/src');
What Laravel is doing is calling ->storeAs('public/src', $request->file('myImage')->hashName()); .. you see the hashName() method? that is what generates the name.
So you can call hashName() first and know your name before the storing happens .. here is an example:
$uploadFile = $request->file('myImage');
$file_name = $uploadFile->hashName();
$path = $uploadFile->storeAs('public/src', $file_name);
Now you have $file_name and $path.
See:
https://laravel.com/docs/6.x/filesystem#file-uploads .. Specifying A File Name
https://github.com/laravel/framework/blob/6.x/src/Illuminate/Http/UploadedFile.php#L33
https://github.com/laravel/framework/blob/6.x/src/Illuminate/Http/FileHelpers.php#L42
I've create an app that upload image along with title, description & etc. However, i'm having a problem in some of the images to upload, it returns an error ("Image source not readable") as shown below:
Here's my Code:
$image = $request->file('image');
// $image = Input::file('image'); // already tried this one still same problem
$orginal_filename = $image->getClientOriginalName();
$ext = $image->getClientOriginalExtension();
$fileName = md5(microtime() . $orginal_filename) . '.' . $ext;
$img = Image::make($image->getRealPath());
$img->stream();
$img->resize(1200, null, function ($constraint) {
$constraint->aspectRatio();
});
Storage::disk('storage_dir')->put($dir . $fileName, $img, 'public');
Already tried following solutions:
Change to Input::file('file')
Check if Request Content-Type has multipart/form-data (Request already has multipart/form-data Content-Type)
Change Intervention Image driver from "gd" to "imagick"
but still have the "Image source not readable" error.
Note: Error only occurs in some images. (I've also tried moving the image(w/c produced the errors) into another directory but still error occurs).
Thank you so much for the help!
You can try running my code
if ($request->file('photo')->isValid()) {
$avatar = $request->file('photo');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename) );
}
/uploads/avatars/ is my directory
If the problem occours in a Laravel 5.7 project, and you store your pictures in the storage folder, it might solve the problem to enter this into the terminal:
php artisan storage:link
(The problem occurs if you have cloned the project from github og bitbucket)
Sorry for bothering guys! It seemed that it was all my fault not realizing php post_max_size and php upload_max_file_size. Since i was trying to upload an image larger than 8MB i only increased the post_max_size > than the current image file size, but not the upload_max_file_size coz i only increased it by 2 (stated: 4MB).
Thanks btw for the help and suggestions!
Replace
$resize = Image::make('storage/app/public/'.$user->image)->resize(300,300);
with
$resize = Image::make(storage_path('app/public/'.$user->image))->resize(300,300);
Give storage full path will solve the problem.
If you got this error on your server, you need to be sure you pushed the all images to server. You are facing this error because of server could not find or read to image/images.
-> Be sure image/images uploaded
-> Make readable to image/images.
My code to upload image like this :
$file = $file->move($path, $fileName);
The code works
But I want to change it using Storage::put like this reference :
https://laravel.com/docs/5.6/filesystem#storing-files
I try like this :
Storage::put($fileName, $path);
It does not works
I'm confused, where I must put $file on the code
How can I solve this problem?
Update :
$file = file of image
$path = storage_path('/app/public/product/')
$fileName = chelsea.jpg
So I want to save the file with name chelsea.jpg on the /app/public/product/
Easy Method
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
This will automatically store the files in your default configuration.
This is another example
Storage::put($fileName, $path);
Hope this helps
I ask this question, since I am trying to get the images I have just copied from Domain A to work in Domain B, (which is using the same database).
http://DOMAIN_A/magento/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/0/b0041-1.jpg
I think knowing what the 32 character string is, which help me find a good explanation why the images are not being found in the front or backend of Magento after reinstall on DOMAIN B.
RE: Magento version 1.4.0.1
Here's the code that creates that filename path, found in Mage_Catalog_Model_Product_Image:
// build new filename (most important params)
$path = array(
Mage::getSingleton('catalog/product_media_config')->getBaseMediaPath(),
'cache',
Mage::app()->getStore()->getId(),
$path[] = $this->getDestinationSubdir()
);
if((!empty($this->_width)) || (!empty($this->_height)))
$path[] = "{$this->_width}x{$this->_height}";
// add misk params as a hash
$miscParams = array(
($this->_keepAspectRatio ? '' : 'non') . 'proportional',
($this->_keepFrame ? '' : 'no') . 'frame',
($this->_keepTransparency ? '' : 'no') . 'transparency',
($this->_constrainOnly ? 'do' : 'not') . 'constrainonly',
$this->_rgbToString($this->_backgroundColor),
'angle' . $this->_angle,
'quality' . $this->_quality
);
// if has watermark add watermark params to hash
if ($this->getWatermarkFile()) {
$miscParams[] = $this->getWatermarkFile();
$miscParams[] = $this->getWatermarkImageOpacity();
$miscParams[] = $this->getWatermarkPosition();
$miscParams[] = $this->getWatermarkWidth();
$miscParams[] = $this->getWatermarkHeigth();
}
$path[] = md5(implode('_', $miscParams));
// append prepared filename
$this->_newFile = implode('/', $path) . $file; // the $file contains heading slash
So, the hash is generated from the configuration info (aspect ratio, etc), as well as the watermark info. This information will not usually change. However, I do see that the path is partially generated from the store_id of the current store, so your trouble may be there.
Is there a reason you can't let Magento use its normal caching procedures for both stores? Since Magento checks the filesystem for the cached image, there shouldn't be a conflict.
Hope that helps!
Thanks,
Joe
Upon contemplation, are you just trying to get the catalog images to work in both domains? The non-cached version of the catalog images are at %magento%/media/catalog/product. Copy the directories from that location and your catalog images should work.
Moving over the cached images isn't going to go far, since they will be deleted next time you flush the Magento cache. So, having moved the images that are in /media/catalog/product, flush the Magento image cache. Make sure that the file permissions are correct for reading. Then, head into Mage_Catalog_Model_Product_Image and take a look at the following code (approx line 270):
if ($file) {
// add these for debugging
Mage::log($baseDir.$file);
Mage::log(file_exists($baseDir.$file));
Mage::log($this->checkMemory($baseDir.$file));
if ((!file_exists($baseDir . $file)) || !$this->_checkMemory($baseDir . $file)) {
$file = null;
}
}
Add a var_dump or Mage::log statement in there (depending on whether you have logging enabled), and verify that the path to the images is correct, and that you have enough memory for the operation. This is the code that will choose the default image for you if no image path exists. If you still can't get it, post the output of those three logging statements and we'll keep trying. :)