-
-
Save slivorezka/925dff0369e8eddc7e4ffa4801ab0240 to your computer and use it in GitHub Desktop.
<?php | |
use Drupal\file\Entity\File; | |
use Drupal\image\Entity\ImageStyle; | |
// File ID. | |
$fid = 123; | |
// Load file. | |
$file = File::load($fid); | |
// Get origin image URI. | |
$image_uri = $file->getFileUri(); | |
// Load image style "thumbnail". | |
$style = ImageStyle::load('thumbnail'); | |
// Get URI. | |
$uri = $style->buildUri($image_uri); | |
// Get URL. | |
$url = $style->buildUrl($image_uri); |
Thank you.
Thanks for this @slivorezka!
Thank you!!!
That saved me some time. 👍
Thanks
Thank you this is great and pointed me in the right direction. I had to use the file_create_url
function however to get the correct URL (D9.2.9)
// ...
// Load image style "thumbnail".
$style = ImageStyle::load('thumbnail');
// Get URI.
$uri = $style->buildUri($image_uri);
// Get URL.
$url = file_create_url($image_uri);
And then I wanted the relative path instead of the URL so:
$url = parse_url($url)['path'];
Thanks for sharing this solution, it worked for me like a charm.
Thank you this is great and pointed me in the right direction. I had to use the
file_create_url
function however to get the correct URL (D9.2.9)// ... // Load image style "thumbnail". $style = ImageStyle::load('thumbnail'); // Get URI. $uri = $style->buildUri($image_uri); // Get URL. $url = file_create_url($image_uri);
And then I wanted the relative path instead of the URL so:
$url = parse_url($url)['path'];
don't use file_create_url , it's deprecated , use \Drupal::service('file_url_generator') instead
Thank you this is great and pointed me in the right direction. I had to use the
file_create_url
function however to get the correct URL (D9.2.9)// ... // Load image style "thumbnail". $style = ImageStyle::load('thumbnail'); // Get URI. $uri = $style->buildUri($image_uri); // Get URL. $url = file_create_url($image_uri);
And then I wanted the relative path instead of the URL so:
$url = parse_url($url)['path'];
don't use file_create_url , it's deprecated , use \Drupal::service('file_url_generator') instead
See https://www.drupal.org/node/2940031 for additional examples.
cool!
👍