I have 3 image field for my products, on save method all working fine, but in update method first image saves temp file in database and the other two update without issue.
Code
Here I share all my 3 images on update method:
//saves temp file instead of file name
if ($request->hasFile('imageOne')) {
$imageOne = $request->file('imageOne');
$filename = 'productone' . '-' . time() . '.' . $imageOne->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename);
Image::make($imageOne)->resize(1200, 600)->save($location);
if(!empty($product->imageOne)){
Storage::delete('images/' . $product->imageOne);
}
$product->imageOne = $imageOne;
}
//works with no issue
if ($request->hasFile('imageTwo')) {
$imageTwo = $request->file('imageTwo');
$filename = 'producttwo' . '-' . time() . '.' . $imageTwo->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename);
Image::make($imageTwo)->resize(1200, 600)->save($location);
if(!empty($product->imageTwo)){
Storage::delete('images/' . $product->imageTwo);
}
$product->imageTwo = $filename;
}
//works with no issue
if ($request->hasFile('imageThree')) {
$imageThree = $request->file('imageThree');
$filename = 'productthree' . '-' . time() . '.' . $imageThree->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename);
Image::make($imageThree)->resize(1200, 600)->save($location);
if(!empty($product->imageThree)){
Storage::delete('images/' . $product->imageThree);
}
$product->imageThree = $filename;
}
Any idea?
Issue is here:-
$product->imageOne = $imageOne;
You are saving $imageOne. But you need to save $filename. Use following code:-
$product->imageOne = $filename;
Related
I am using Media Library v9. I am trying to follow this documentation to associate files. However, the package is newly installed to the project and there're lots of images uploaded. Is it possible to add the uploaded media to collection? Like the docs:
$yourModel = YourModel::find(1); // Maybe I can use `id` here instead of a number
$yourModel
->addMedia($pathToFile)
->toMediaCollection();
For my case I have two directories: public/app/lostFound/lostItems and public/app/lostFound/handoverStatements
This was my old code for the image upload:
if( $request->hasFile('LFImage') ) {
$destination = public_path('app/lostFound/lostItems' . $lostFound->LFImage);
if( File::exists($destination) )
{
File::delete($destination);
}
$file = $request->file('LFImage');
$extension = $file->getClientOriginalExtension();
$filename = $lostFound->LFNumber . '-' . $lostFound->lostItem . '.' . $extension;
$file->move('app/lostFound/lostItems', $filename);
$lostFound->LFImage = $filename;
}
if( $request->hasFile('handoverStatement') ) {
$destination = public_path('app/lostFound/handoverStatements' . $lostFound->handoverStatement);
if( File::exists($destination) )
{
File::delete($destination);
}
$file = $request->file('handoverStatement');
$extension = $file->getClientOriginalExtension();
$filename = $lostFound->lostItem . '-' . $lostFound->LFNumber . '.' . $extension;
$file->move('app/lostFound/handoverStatements', $filename);
$lostFound->handoverStatement = $filename;
}
How can I associate the already uploaded LFImage and handoverStatement to the media collection?
This is what I did so far, but it's not directing to the files, but the folder containing the files, which will obviously gives me an error:
$lostFound = LostFound::find('id');
$lostFound
->addMedia( public_path('app/lostFound/handoverStatement') )
->toMediaCollection();
Hello guys when I use file upload and save link in database it save this "/tmp/phpkPhygr|public/allFiles/files/26657d5ff9020d2abefe558796b99584/69adc1e107f7f7d035d7baf04342e1ca.gif" so why "tmp/php..." show in this link or I want to remove this to work this url.
$files = array();
if($files = $request->file('files')){
foreach($files as $file){
$fileName = md5(rand(100,1000)) . "." . strtolower($file->getClientOriginalExtension());
$folder='public/allFiles/files/' . md5(rand(100,10));
$file_url = $folder . "/" . $fileName;
$file->move($folder, $fileName);
$files[] = $file_url;
}
}
$filesReq = $files;
if($filesReq == ' '){
$filesReq = 'no files';
}else{
$filesReq = implode('|', $files);
}
I need when upload article image, automatic upload Original Image Size and resize image in thumb folder.
but in this method i receive this error (thumb folder can not automatic create) :
Can't write image data to path (storage/upload/images/articles/1400/05/06/thumb/Bx1WRTtp9IgSZgnP8VE11627500167.jpg)
Upload Image Method :
protected function articleUploadImage($file, $folder, $size)
{
$d = jdate();
$year = $d->format('Y');
$month = $d->format('m');
$day = $d->format('d');
$direct = 'storage/upload/images/'. $folder . '/' . $year . '/' . $month . '/' . $day;
$directWithResize = 'storage/upload/images/'. $folder . '/' . $year . '/' . $month . '/' . $day . '/thumb';
$extension = $file->getClientOriginalExtension();
$fileName = Str::random(20) . time() . '.' . $extension;
// if (!is_dir($directWithResize)) {
// mkdir($directWithResize);
// }
//$path = Image::make($file->getRealPath());
//$path->resize(100, 100);
//$path->save(public_path($directWithResize . '/' . $fileName));
Image::make($file->getRealPath())->fit($size, null, function ($constraint) {
$constraint->aspectRatio();
})->save($direct . '/thumb/' . $fileName);
//$path = $file->store($direct);
//$newPath = $direct;
return $direct;
}
Before uploading an image, you should create a directory.
protected function articleUploadImage($file, $folder, $size)
{
$d = jdate();
$year = $d->format('Y');
$month = $d->format('m');
$day = $d->format('d');
$direct = 'storage/upload/images/'. $folder . '/' . $year . '/' . $month . '/' . $day;
$extension = $file->getClientOriginalExtension();
$fileName = Str::random(20) . time() . '.' . $extension;
if (!file_exists($direct . '/thumb/')) {
mkdir($direct . '/thumb/', 666, true);
}
Image::make($file->getRealPath())->fit($size, null, function ($constraint) {
$constraint->aspectRatio();
})->save($direct . '/thumb/' . $fileName);
return $direct;
}
After deploying my laravel project from local to Apache web server, all works correctly except images link. Here the code:
Images are stored in:
storage/app/public/photos
after i've run command:
php artisan storage:link
Images are linked at:
public/storage/photos
Controller:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photo = $file->storeAs('public/photos', 'foto-' . time() . '.' . $extension);
$user->photo = $photo;
$user->save();
}
Images are uploaded correctly on storage/app/public/photos and correctly linked in public/storage/photos but it doesn't display on frontend.
in blade, i've tried to use Storage::url to retrieve the path
{{Storage::url($user->photo)}}
and asset()
{{asset($user->photo)}}
in both cases, image doesn't exist
The public path of image is:
http://mywebsite.com/storage/photos/foto-1522914164.png
You should Use url function to show your image like below way.
url($user->photo);
I'd suggest to change the controller code as follows:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photoFileName = 'foto-' . time() . '.' . $extension;
$photo = $file->storeAs('public/photos', $photoFileName);
$user->photo = 'photos/' . $photoFileName;
$user->save();
}
Then you can use {{asset($user->photo)}} in your blade.
on my webspace, it seems that the only way to display image correctly is to create a custom route that read and serve the image.
i'm solved like this:
i'm storing only image name in db:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photoFileName = 'photo-' . $model->id . '.-' . time() . '.' . $extension;
$photo = $file->storeAs('public/photos', $photoFileName);
$store = $photoFileName;
}
then, i've create custom route that read images and display them:
Route::get('storage/{filename}.{ext}', function ($filename, $ext) {
$folders = glob(storage_path('app/public/*'), GLOB_ONLYDIR);
$path = '';
foreach ($folders as $folder) {
$path = $folder . '/' . $filename . '.' . $ext;
if (File::exists($path)) {
break;
}
}
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header('Content-Type', $type);
return $response;
});
in blade, i'm using Storage to display image:
{{ Storage::url($photo->photo) }}}
I'm trying to make an image upload script with laravel 4. (using Resource Controller) and i'm using the package Intervention Image.
And what i want is: when uploading an image to save it as 3 different images (different sizes).
for example:
1-foo-original.jpg
1-foo-thumbnail.jpg
1-foo-resized.jpg
This is what i got so far.. it's not working or anything, but this was as far as i could get with it.
if(Input::hasFile('image')) {
$file = Input::file('image');
$fileName = $file->getClientOriginalName();
$fileExtension = $file->getClientOriginalExtension();
$type = ????;
$newFileName = '1' . '-' . $fileName . '-' . $type . $fileExtension;
$img = Image::make('public/assets/'.$newFileName)->resize(300, null, true);
$img->save();
}
Hopefully someone can help me out, thanks!
You may try this:
$types = array('-original.', '-thumbnail.', '-resized.');
// Width and height for thumb and resized
$sizes = array( array('60', '60'), array('200', '200') );
$targetPath = 'images/';
$file = Input::file('file')[0];
$fname = $file->getClientOriginalName();
$ext = $file->getClientOriginalExtension();
$nameWithOutExt = str_replace('.' . $ext, '', $fname);
$original = $nameWithOutExt . array_shift($types) . $ext;
$file->move($targetPath, $original); // Move the original one first
foreach ($types as $key => $type) {
// Copy and move (thumb, resized)
$newName = $nameWithOutExt . $type . $ext;
File::copy($targetPath . $original, $targetPath . $newName);
Image::make($targetPath . $newName)
->resize($sizes[$key][0], $sizes[$key][1])
->save($targetPath . $newName);
}
Try this
$file = Input::file('userfile');
$fileName = Str::random(4).'.'.$file->getClientOriginalExtension();
$destinationPath = 'your upload image folder';
// upload new image
Image::make($file->getRealPath())
// original
->save($destinationPath.'1-foo-original'.$fileName)
// thumbnail
->grab('100', '100')
->save($destinationPath.'1-foo-thumbnail'.$fileName)
// resize
->resize('280', '255', true) // set true if you want proportional image resize
->save($destinationPath.'1-foo-resize-'.$fileName)
->destroy();