How to set existing image to file upload field?

I am trying to fetch favicon from url and update file upload field with fetched icon but preview is not working.
TextInput::make('url')->lazy()
->afterStateUpdated(function (Closure $set, $state) {
$domain = UrlHelper::getDomain($state);
$faviconName = str($domain)->replace('.', '-');
$faviconPath = public_path('images/favicons/' . $faviconName . '.png');
try {
$ic = Image::make('https://api.faviconkit.com/' . $domain . '/16')
->encode('png')
->resize(16, 16)
->save($faviconPath);
$ic->destroy();
} catch (Exception) {
copy(public_path('images/default-favicon.png'), $faviconPath);
}

// How to do this?
$set('favicon', [$faviconPath]);
}),

FileUpload::make('favicon')->image(),
TextInput::make('url')->lazy()
->afterStateUpdated(function (Closure $set, $state) {
$domain = UrlHelper::getDomain($state);
$faviconName = str($domain)->replace('.', '-');
$faviconPath = public_path('images/favicons/' . $faviconName . '.png');
try {
$ic = Image::make('https://api.faviconkit.com/' . $domain . '/16')
->encode('png')
->resize(16, 16)
->save($faviconPath);
$ic->destroy();
} catch (Exception) {
copy(public_path('images/default-favicon.png'), $faviconPath);
}

// How to do this?
$set('favicon', [$faviconPath]);
}),

FileUpload::make('favicon')->image(),
When I dump data I can see 'favicon' field with correct image path but preview is not working on file upload component.
2 Replies
Dan Harrin
Dan Harrin3y ago
file uploads read from a storage disk, if the favicon is not in a storage disk it wont work so what youre trying to do wont work
Darpan
DarpanOP3y ago
Thanks, its working after storing the fetched favicon in pubilc storage...

Did you find this page helpful?