Laravel image intervention unable to init from given url - laravel

Good evening, i hosted a Laravel project and found some bug.
First of all, here is my code in my controller:
$request->file('FotoHT')->storeAs('FotoHT', $filenameToStore); //this is to safe
$path = asset('storage/app/FotoHT/'. $filenameToStore);
$width = Image::make($path)->width();
$height = Image::make($path)->height();
I save a picture, and it works but when i try using
Image::make($path)->width();
it give me the wrong url. It give me
https://myweb/public/storage/app/FotoHT/_MG_9549_1578913993.jpg
while the image should be accessed from
https://myweb/storage/app/FotoHT/_MG_9549_1578913993.jpg
Can anyone give me a help/solution?

You need to save the image encode to the public path
$fieldFile = $request->file('FotoHT');
$image = Image::make($fieldFile)->width();
Storage::disk('public')->put("FotoHT/".$filenameToStore, (string) $image->encode());

$path=$request->file('FotoHT')->storeAs('FotoHT',$filenameToStore);
$width = Image::make($path)->width();
$height = Image::make($path)->height();
Try this command.You can use asset() helper method for img tag in view

Related

Adding Image PHPWord in Laravel

So I want to add an header image to my document in PHPWord in Laravel.
So this is my code
public function generateDocx()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$headerLogo = 'http://127.0.0.1:8000/img/logoAnevBulanan.png';
$section->addImage($headerLogo);
// Bunch of line to download the docx
}
And I got Maximum execution time of 60 seconds exceeded, when I try the other method from the documentation, I still got the same error. I try to use asset() helper from laravel and still did not work
Try getting your image using local path instead of URL
$source = file_get_contents('/path/to/my/images/earth.jpg');
$textrun->addImage($source);
Refer to documentation : https://phpword.readthedocs.io/en/latest/elements.html#images

How to upload a file using laravel that will use the exact file name on my public folder and at my database

I have been searching a few codes but didnt manage to work.
The one I found is using jscon encode.
Is there any way to upload a file without changing its name into storage and database?
$fileModal = new Image();
$fileModal->name = json_encode($imgData);
$fileModal->image_path = json_encode($imgData);
$fileModal->save();
I didnt understand the json part. Was hoping to use a built in laravel.
I used the one in the documentation but didnt help.
$path = $request->file('namefile')->storeAs(
'DocumenSokongan', $request->user()->id
);
$image = $request->file('filename');
$imageName = time() . '.' . $image->getClientOriginalExtension();
$image->move($path, $imageName);

How to fake image upload for testing with Intervention image package using Laravel

I have a test asserting that images can be uploaded. Here is the code...
// Test
$file = UploadedFile::fake()->image('image_one.jpg');
Storage::fake('public');
$response = $this->post('/api/images', [
'images' => $file
]);
Then in controller i am doing something simpler..
$file->store('images', 'public');
And asserting couple of things. and it works like charm.
But now i need to resize the image using Intervention image package. for that i have a following code:
Image::make($file)
->resize(1200, null)
->save(storage_path('app/public/images/' . $file->hashName()));
And in case if directory does not existing i am checking first this and creating one -
if (!Storage::exists('app/public/images/')) {
Storage::makeDirectory('public/images/', 666, true, true);
}
Now Test should be green and i will but the issue is that every time i run tests it upload a file into storage directory. Which i don't want. I just need to fake the uploading and not real one.
Any Solution ?
Thanks in advance :)
You need to store your file using the Storage facade. Storage::putAs does not work because it does not accept intervention image class. However you can you use Storage::put:
$file = UploadedFile::fake()->image('image_one.jpg');
Storage::fake('public');
// Somewhere in your controller
$image = Image::make($file)
->resize(1200, null)
->encode('jpg', 80);
Storage::disk('public')->put('images/' . $file->hashName(), $image);
// back in your test
Storage::disk('public')->assertExists('images/' . $file->hashName());

Can't write image data to path in laravel

I am having the same error as this guy is :
Another thread
basically the error i have is uploading the image to the specific path, my code is as follows :
public function postCreate() {
$validator = Validator::make(Input::all() , Product::$rules);
if ($validator->passes()) {
$product = new Product;
$product->category_id = Input::get('category_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->price = Input::get('price');
$image = Input::file('image');
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products'.$filename);
$product->image = 'public/img/products'.$filename;
$product->save();
return Redirect::to('admin/products/index')
->with('message' , 'Product created');
}
return Redirect::to('admin/products/index')->with('message' , 'something went wrong')
->withErrors($validator)->withInput();
}
I was just trying to follow a tutorial on laravel e-commerce web application.
I guess the problem is that i don't have write permisson in my directory , how do i add write permission in my directory. I.E. the public folder, I googled a few places , but i don't understand what is it that i have to edit ?
I.E the htaccesss file or can i make write changes on the cmd ? also how do i check what weather a directory is write protected .
PS. i am using windows . i am attaching a screenshot of the error .
Thank you.
You might want to change the dateformat since windows doesn't allow colons in filenames:
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
And you also might want to add a trailing slash to your path so it doesn't concatenate the filename to the folder path:
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products'.$filename);
Generally this error occurs when you do not yet have the directory that will store the image inside the public directory. Sometimes it can be a permission issue.
Does you img directory exists in your public directory?
To fix this, follow the steps:
Use this snippet:
$relPath = 'img/'; //your path inside public directory
if (!file_exists(public_path($relPath))) { //Verify if the directory exists
mkdir(public_path($relPath), 666, true); //create it if do not exists
}
Or manually create the img directory in public
2.Then you can save your image:
Image::make($image)->resize(468, 249)->save(public_path('img/products'.$filename)); //save you image
$product->image = 'img/products'.$filename; //note
$product->save();
**NOTE: We do not need to specify the public directory in the path because we are using a relative path. The img directory will be created inside public directory.
Along with this, you need to make sure the folder path exists and which has right permissions set.
$relPath = 'img/product/';
if (!file_exists(public_path($relPath))) {
mkdir(public_path($relPath), 777, true);
}
Where $relPath is the path relative to public directory.
This requirement is however windows specific. In linux, folder directory will be created if it does not exist.
I also recommend all of you to check if $path exists. Like Jose Seie use native PHP check, I recommend you to thought about build-in helpers.
This can be achieved with File Facade helper:
File::exists($imagePath) or File::makeDirectory($imagePath, 777, true);
Advice you to use Laravel built-in functions, classes & helpers to improve the performance of your application!
well , i made the correction that john suggested and then made the following corrections :
I replaced the below code :
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products'.$filename);
with :
$path = public_path('img/products/'.$filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
problem solved , i don't know why public_path works , but never mind .

Laravel 4 get image from url

OK so when I want to upload an image. I usually do something like:
$file = Input::file('image');
$destinationPath = 'whereEver';
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
if( $uploadSuccess ) {
// save the url
}
This works fine when the user uploads the image. But how do I save an image from an URL???
If I try something like:
$url = 'http://www.whereEver.com/some/image';
$file = file_get_contents($url);
and then:
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
I get the following error:
Call to a member function move() on a non-object
So, how do I upload an image from a URL with laravel 4??
Amy help greatly appreciated.
I don't know if this will help you a lot but you might want to look at the Intervention Library. It's originally intended to be used as an image manipulation library but it provides saving image from url:
$image = Image::make('http://someurl.com/image.jpg')->save('/path/saveAsImageName.jpg');
$url = "http://example.com/123.jpg";
$url_arr = explode ('/', $url);
$ct = count($url_arr);
$name = $url_arr[$ct-1];
$name_div = explode('.', $name);
$ct_dot = count($name_div);
$img_type = $name_div[$ct_dot -1];
$destinationPath = public_path().'/img/'.$name;
file_put_contents($destinationPath, file_get_contents($url));
this will save the image to your /public/img, filename will be the original file name which is 123.jpg for the above case.
the get image name referred from here
Laravel's Input::file method is only used when you upload files by POST request I think. The error you get is because file_get_contents doesn't return you laravel's class. And you don't have to use move() method or it's analog, because the file you get from url isn't uploaded to your tmp folder.
Instead, I think you should use PHP upload an image file through url what is described here.
Like:
// Your file
$file = 'http://....';
// Open the file to get existing content
$data = file_get_contents($file);
// New file
$new = '/var/www/uploads/';
// Write the contents back to a new file
file_put_contents($new, $data);
I can't check it right now but it seems like not a bad solution. Just get data from url and then save it whereever you want

Resources