I have a few image styles set up. But they are all stored as "private" files. I want to be able to give anonymous users public access to these files, or find some other way by which to generate access to this file. My goal is to provide access to private files for certain anonymous users depending on certain conditions.
I am hiding all images on the server to avoid anyone accidentally gaining access to them, but some "private" images should be viewable if a certain style is applied. For example, I am selling images on my site. The "waterproof" image style should allow any user (including anonymous) user to see the private file preview. But only logged in users should be able to view it with the "high_res" thumbnail.
This is how I generate the URL:
$imageURL = file_create_url($badge->field_badge_image_image->entity->getFileUri());
This url can only be accessed by logged in users. If I try this code, I still get access denied.
$imageURL = ImageStyle::load('farm_logos')->buildUrl(
$badge->field_badge_image_image->entity->getFileUri()
);
Is there no way to generate a URL that is accessible via public?
I also tried the "hook_file_download":
function mymodule_file_download($uri) {
return array(
"Accept" => "Accept",
'Content-Type' => "image/jpg"
);
}
But I was hoping this would make all private files public, but unfortunately it did not.