0

In Drupal 8 Manage display for the image field, I can assign only one image style.

I have two image styles: thumbnail and medium.

when I upload an image through image uploader, I need to view my uploaded image in those two image styles.

Is there a way to show the image in those two sizes from the single image I have uploaded?

If I selected thumbnail in the manage display on the image field it will save the image as thumbnail, but not as medium size as well. I need to save the image in both the sizes, or I need to get both the styled images from the single image I upload. The following code can get the image for the given style in manage display.

$imageStyle = ImageStyle::load('medium');
$galleryImage = $imageStyle->buildUri($image->entity->getFileUri());
2

By default, any file you upload via the file field goes into the public files directory /sites/default/files. And you can choose to apply any image style to the same image and configure it separately per display.

E.g. Let's say you upload an image with name my_beatiful_cat.jpg from a file field on a node page.

  1. The image by default will stay at sites/default/files/my_beatiful_cat.jpg.
  2. From manage display of the content type, in the default view, let's say you choose the image style to be medium and save.
  3. When you view the node, a new image will be created from the uploaded image at sites/default/files/styles/medium/public. So, you will have a file sites/default/files/styles/medium/public/my_beatiful_cat.jpg which will have the dimensions as per the image style applied.
  4. This is true for any other image style that you apply for any other display.

So, you only upload an image once. Image styles are responsible for generating images with respective sizes as per the configuration and the appropriate image file is rendered.

  • Nope. I want to know how to get multiple image styles for a single image. As per the above example I need to get both thumbnail and medium image sizes. But I can get only one. Either thumbnail or medium. How can I get two images from both image styles. @AjitS – i am batman Jan 3 '18 at 6:32
  • @iambatman Do you wish to render the same image twice on the same page? – AjitS Jan 3 '18 at 6:33
  • Yes. I have a gallery. So the thumbnail should be the gallery image and medium size should be the slider image. Practically they both should be in the same page. – i am batman Jan 3 '18 at 6:40
  • @iambatman That would depend on how do you implement the gallery image and slider. If you are using views to do it. You can add the same image field to the view multiple times and choose separate image styles from the field settings. – AjitS Jan 3 '18 at 6:43
  • 1
    Yes, you could add the original image any number of times to the view. I have added it to this view du6hi.ply.st/admin/structure/views/view/test/edit/page_1. (Username/password: admin/admin). And you could view the result at du6hi.ply.st/test @iambatman – AjitS Jan 3 '18 at 7:04
1

It is not mandatory to define image styles with actual image during upload. Once the image is uploaded we can assign the image style to any image field for display.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.