Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom image sizes to the MediaUpload #7483

Closed
dseidl opened this issue Jun 22, 2018 · 4 comments
Closed

Add custom image sizes to the MediaUpload #7483

dseidl opened this issue Jun 22, 2018 · 4 comments
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Media Anything that impacts the experience of managing media [Type] Enhancement A suggestion for improvement.

Comments

@dseidl
Copy link

dseidl commented Jun 22, 2018

I need access to the custom image sizes url's from the MediaUpload component. Even after registration of those only full and thumbnail is returned.

Include all image sizes, like ACF did or like the /media api call does.

@danielbachhuber
Copy link
Member

I need access to the custom image sizes url's from the MediaUpload component. Even after registration of those only full and thumbnail is returned.

@dseidl Can you share more detail on what you're trying to do and how you're extending the MediaUpload component? A code snippet would be great too.

@danielbachhuber danielbachhuber added [Status] Needs More Info Follow-up required in order to be actionable. [Feature] Extensibility The ability to extend blocks or the editing experience labels Jun 27, 2018
@dseidl
Copy link
Author

dseidl commented Jun 28, 2018

@danielbachhuber sure, sorry for the limited information.

Basically i have a normal MediaUpload Component in my block.

<MediaUpload
  onSelect={ mediaChanged }
  type="image"
  value=""
  render={ ( { open } ) => (
    <Button onClick={ open } className="towa-button button button-large">
      <Dashicon icon="format-image" /> { __( 'Upload Image' ) }
    </Button>
  ) }
/>

I have also added a new custom image size via add_image_size in php.

In the onSelect event i get the image object i selected returned - but the custom sizes are missing. I need the custom Image size, actually multiple of them, because i have a picture element with multiple source sets :)

As a workaround i added that: (but thats just a workaround, of course it would be better if the custom image sizes are returned in the onSelect event):

const mediaChanged = (image) => {
  wpAPI.media().id( image.id )
    .then((response) => {
      const image = transformImage(response);
      setAttributes({image});
    });
};

The Rest API does return the custom image sizes, so i just transform the returned json so i only save the needed data (title, alt, url and the complete sizes object):

const transformImage = (image) => {
  return {
    id: image.id,
    alt: image.alt_text,
    url: image.source_url,
    sizes: image.media_details.sizes,
  }
};

@danielbachhuber danielbachhuber added the [Type] Help Request Help with setup, implementation, or "How do I?" questions. label Jun 28, 2018
@danielbachhuber
Copy link
Member

Cool, #7605 will do the trick for you.

@designsimply designsimply added [Type] Enhancement A suggestion for improvement. [Feature] Media Anything that impacts the experience of managing media and removed [Status] Needs More Info Follow-up required in order to be actionable. [Type] Help Request Help with setup, implementation, or "How do I?" questions. labels Nov 11, 2018
@florianbrinkmann
Copy link

Like described in #7605 (comment), one needs to use the image_size_names_choose filter to add custom image sizes to the img object. Is that the wanted behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Media Anything that impacts the experience of managing media [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

4 participants