How get cloudinary file url/other properties by public_id? - laravel

Saving images under cloudinary( with cloudinary-laravel 1.0) I keep public_id in my database
and I want to get url(http or https), size, dimaentainal of this file by public_id
At reading this
/**
* You can also retrieve a url if you have a public id
*/
$url = Storage::disk('cloudinary')->url($publicId);
at
https://github.com/cloudinary-labs/cloudinary-laravel
I got ERROR:
Disk [CLOUDINARY] does not have a configured driver.
But I save images to cloudinary with storeOnCloudinaryAs method and in my .env I have
CLOUDINARY_URL=cloudinary://NNNNNNNNNNNN:AErjB_-XXXXXXXXX
CLOUDINARY_UPLOAD_PRESET=ml_default
and default file config/cloudinary.php
My config/filesystems.php has no any cloudinary parameters and can it be reason of my error?
Also it seems very strange for me that Storage::was used in this case, but I did not
find how get file url/other properties by public_id ?
Edited 1:
I added line
...
CloudinaryLabs\CloudinaryLaravel\CloudinaryServiceProvider::class,
...
in 'providers' block of ny config/app.php and cleared cach.
But still got
"Disk [CLOUDINARY] does not have a configured driver."
error.
applying changes into .env and clearing cache I try to debug from which line error is triggered in file vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php as :
protected function resolve($name)
{
$config = $this->getConfig($name);
\Log::info( varDump($name, ' -1 $name resolve::') ); // IT HAS ‘CLOUDINARY’ value
if (empty($config['driver'])) {
throw new InvalidArgumentException("Disk [{$name}] does not have a configured driver.");
}
But what is source for $config in this file?
config/filesystems.php ? In this file I have no any CLOUDINARY block. maybe I need to add it? But in which format ?
Thanks in advance!

Setting driver to lowercase
$cloudinaryUrl = Storage::disk(strtolower($imagesUploadSource))
fixed this error.

Related

Cannot get value from config in Lumen

I want to change the timezone in lumen, but I cannot get the value from config, it always give the default value UTC.
I've tried everything I know, to the point changing the default value to what I wanted. But still the timezone wont change
AppServiceProvider
public function register()
{
//set local timezone
date_default_timezone_set(config('app.timezone'));
//set local date name
setLocale(LC_TIME, $this->app->getLocale());
URL::forceRootUrl(Config::get('app.url'));
}
Bootstrap.app
(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
dirname(__DIR__)
))->bootstrap();
date_default_timezone_set(env('APP_TIMEZONE', 'Asia/Jakarta'));
$app->configure('app');
Config.app
'timezone' => env("APP_TIMEZONE", "Asia/Jakarta"),
.env
APP_TIMEZONE="Asia/Jakarta"
APP_LOCALE="id"
Also if I make a variable inside config.app such as:
'tes_var' => 'Test'
And using it like this:
\Log::info(config('app.tes_var'));
The result in Log is null, I can't get the value from tes_var.
I don't have any idea what's wrong here, if it's in Laravel maybe this is happened because cached config, but there's no cached config in Lumen. Maybe I miss some configuration here?
Thanks
First, you should create the config/ directory in your project root folder.
Then create a new file app.php under the config directory i.e. config/app.php
Now add whatever config values you want to access later in your application in the config/app.php file.
So, instead of creating a config.php file you should make a config directory and can create multiple config files under the config directory.
So final code will be like this:
config/app.php will have:
<?PHP
return [
'test_var' => 'Test'
];
Can access it anywhere like this:
config('app.tes_var');
Although Lumen bootstrap/app.php has already loaded the app.php config file (can check here: https://github.com/laravel/lumen/blob/9.x/bootstrap/app.php)
If not loaded in your case, you can add the below line in bootstrap/app.php file:
$app->configure('app');
Hope it will help you.
In order to use the env file while caching the configs, you need to create a env.php inside the config folder. Then, load all env variables and read as "env.VARIABLE_FROM_ENV". Example env.php:
<?php
use Dotenv\Dotenv;
$envVariables = [];
$loaded = Dotenv::createArrayBacked(base_path())->load();
foreach ($loaded as $key => $value) {
$envVariables[$key] = $value;
}
return $envVariables;
then read in your code:
$value = config('env.VARIABLE_FROM_ENV', 'DEFAULT_VALUE_IF_YOU_WANT');

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

Laravel - timezone - Config null

I installed laravel-timezone as described here: https://github.com/jamesmills/laravel-timezone
also added teh configuration file, and in the configuraiton file there is this:
timezone.php
'lookup' => [
'server' => [
'REMOTE_ADDR',
],
'headers' => [
],
],
After the login, the script is suposed to update the timezone automatically, but I get Invalid argument supplied for foreach() in the file \vendor\jamesmills\laravel-timezone\src\Listeners\Auth\UpdateUsersTimezone.php:111
the function is this:
/**
* #return mixed
*/
private function getFromLookup()
{
$result = null;
foreach (config('timezone.lookup') as $type => $keys) {
if (empty($keys)) {
continue;
}
$result = $this->lookup($type, $keys);
if (is_null($result)) {
continue;
}
}
return $result;
}
I tried to put a dd() there, and config('timezone.lookup') is null..
why does it return null, if the file exists, and a value is assigned in the file?
Is there anything I have to do to make this timezone.php accesisble?
When you are caching the configuration Laravel creates a file in bootstrap/cache/config.php with all the configuration. After this is done the individual cache files and the .env file are no longer read.
You therefore need to clear your config cache, and I recommend to not cache it while in development. Only cache the config in production.
Just run:
php artisan cache:clear
Sorry you’re having issues using the package. As far as I’m aware you don’t have to publish the config for the package to work after initial install. It should pick up defaults so you should be able to install it and go! However, like others have said, if you do publish the config then please try to flush your config cache php artisan cache:clear
Thank you #apokryfos for your help on this issue.
If you have any further issues please don’t hesitate to reply.
James

RuntimeException laravel 5.8 RuntimeException This driver does not support creating temporary URLs

RuntimeException This driver does not support creating temporary URLs.
I am trying to generating Temp Url for every request Laravel version 5.8 below code trying showing an error.
This driver does not support creating temporary URLs.
$url = "66.jpeg";
$url = Storage::disk('public')->url($url);
$url = Storage::disk('public')->temporaryUrl(
'66.jpeg', now()->addMinutes(5)
);
From my knowledge, temporaryUrl is a method used on a drivers such as s3 to create a temporary url for a privately stored asset.
If you would like to set a temporary url for a file, it may help to use Cache to temporarily store the path.
Cache can set a key/value for a set amount of time. A url can be create which links to an endpoint. Then endpoint can then be created which returns the contents of that file:
// Creating temp file index in cache
$image = '66.jpg';
Cache::put('/temp/' . $image, 300); // 5 minutes
Now in, for example, TempController.php (visiting http://example.com/temp/66.jpg):
public function show($image)
{
if (Cache::get('/temp/' . $image) && ! Storage::disk('public')->exists($image)) {
// not in cache or do not exist, maybe redirect...
};
return Storage::disk('public')->get($image);
}
This is a proof of concept however I hope this helps.

remove request file from laravel request

When I want upload file with form in laravel, I cant remove file value from request.
This is my full code
if($request->hasFile('image')){
$string_name = str_random(12);
$image = $request->file('image')->move(getcwd().'/image/original',$string_name.'.'.$request->file('image')->getClientOriginalExtension());
$request->request->remove('image');
}
if($request->hasFile('thumbnail')){
$string_name = str_random(12);
$thumbnail = $request->file('thumbnail')->move(getcwd().'/image/thumbnail',$string_name.'.'.$request->file('thumbnail')->getClientOriginalExtension());
}
Portfolio::create($request->all());
but image or thumbnail file do not remove from $request. This means this line of code not working :
$request->request->remove('image');
I've tried many ways but the file does not get removed from the request.
Instead of removing the image from your $request before saving, you can explicitly mention what you would like to save to your model by using the ->only([]) method on $request.
Portfolio::create($request->only(['title', ...]));
This will allow you to specify exactly what you would like saved from the $request data.
You can do the reverse and use the ->except() method to remove the image too:
Portfolio::create($request->except('image'));
use this to remove the file
$request->offsetUnset('input_file_name');
or
$request->except('filename');

Resources