React - pixel ratio - set 2x 3x images for retina devices - reactjs

I am creating a react app.
I want the browser to server an image (twice bigger in resolution) to retina devices.
I am trying with 2x. I am trying in different ways:
The png is imported.
{retina} is the URL of the twice bigger image.
{logo} is the normal size image.
I have tried to implement it this way:
<picture>
<source srcset={`${retina} 2x, ${logo} 1x`} />
<img src={logo} alt="Flowers" />
</picture>
also this way:
<img srcset={`
${ratina} 2x,
${Logo} 1x,
`}
src={Logo}
></img>
and this way:
<picture>
<img src={logo} srcset={`${retina} 2x`} />
</picture>
and this way:
<img src={logo} srcset={`${retina} 2x`} />
For testing purposes I gave the retina png image a different color, just to notice it immediately when it works.
Problem 1:
In some of the above cases it shows twice the normal image, sometimes twice the retina images, but in none of the above cases it shows a normal image for 1x and 2x for retina devices. I am testing it on PC, iPhone, and with Chrome emulator with custom device set to pixel ratio 2.
Problem 2 (that's not really a problem, but...):
In all cases the images are loaded in the browser as:
Can
anybody point out what I am doing wrong so that the 2x image is not
showing up?
How can I import the image, so that the real image URL
will be rendered and not a base64 without the need to create a
.env file and set the IMAGE_INLINE_SIZE_LIMIT to 0? Any other way to achieve the same?
Thank you

Almost there. You just need to name the attribute according to JSX style, which is srcSet. Also, whatever you put in src is considered the default resolution load, so in srcSet you just need the 2x and 3x, if applicable.
Full Code Example using an image called logo:
import logo from '../path-to/logo.png'
import logo2x from '../path-to/logo#2x.png'
import logo3x from '../path-to/logo#3x.png'
<img
src={logo}
srcSet={`${logo2x} 2x, ${logo3x} 3x`}
/>

Related

Using windows file absolute path in react doesn't work

I tried using
<img src="file://C:/Users/JOHN-PC/Pictures/test123.png" alt="Logo"/>
in reactjs but the pic is not loading.
Add /// like below, this might help
<img src="file:///C:/Users/JOHN-PC/Pictures/test123.png" alt="Logo"/>

adding an image to react js

<CardMedia
className={classes.cardMedia}
image="https://source.unsplash.com/random"
title="Image title"
/>
i have this card component that has an image
this works perfectly but it generates an random image according ti the url "https://source.unsplash.com/random"
i need to replace the url with an other one from a google image
but when i put the link for example (https://www.google.com/search?q=credit+card+images&tbm=isch&source=iu&ictx=1&fir=yTCt2mYy4QLi2M%252C-uamK6tjDy_5gM%252C_&vet=1&usg=AI4_-kQyjcIQ9sXlfX8yIJxLD3J9BMHRUw&sa=X&ved=2ahUKEwiO48bludHqAhVI2KQKHXWzCskQ9QEwAXoECAYQMg&biw=1366&bih=657#imgrc=blCwDWRxIOqbxM)
nothing happens and no image shows
Right click on the desired image and choose Copy image address. That's the address you want to use in your app.

JPG vs JPEG2000 vs WebP

I'm building my website using React and have multiple images to display. After running an audit using the Google Chrome audit function, I've been getting the "Serve images in next-gen formats" opportunity message.
After reading about the various formats (WebP, JPEG2000, JPEGXR), it seems each is supported on only select browsers. For instance, I can't just convert all of my images to WebP because they won't show up on the Safari browser. So my issue is how to I "serve" each type of image depending on the browser being used? This is what I've tried:
I have 3 types of files, jpg, JPEG2000, and WebP. Each is being imported like:
import Imagejpg from './path/image.jpg'
import ImageJPEG2000 from './path/image.JPEG2000'
import ImageWebP from './path/image.webp'
Then in my class, I have an object array that contains the images. To use the images:
<picture>
<source>
srcSet={`
${project.Imagejpg},
${project.ImageJPEG2000},
${project.ImageWebP},
</source>
<img src={project.imageWebP} alt=""/>
</picture>
Now, if I only use the jpg image, it works fine on all browsers as most browsers can use jpg. But I'm trying to optimize my site and use the better types of image files. Is there a way to use the several types of files or is there something I am missing?
The solution is indeed in the <picture> element, but using multiple sources.
The code with correct syntax looks like this:
<picture>
<source srcSet={project.ImageWebP} type="image/webp" />
<source srcSet={project.ImageJPEG2000} type="image/jp2" />
<source srcSet={project.Imagejpg} type="image/jpeg" />
<img src={project.Imagejpg} alt="" />
</picture>
Explanation
Seeing the picture element, a browser will download the first source it can support. If it's an old browser that doesn't support <picture> at all, it will fall back to the <img /> tag which has a jpeg source.
This is a quick and easy win to improve your page's speed. The tiny overhead in extra HTML bytes does not negate the speed improvements except in extreme scenarios, like very small and simple images.

Replace the default block with figure

Currently, i'm buiding my rich text editor with quill and i need to create an embed for images. Before quill, i used to use redactor and i try to move to redactor. But, i already have 2 version of data from redactor before where the users is already uploaded images.
Version 1:
<p>
<img src="http://lorempixel.com/400/200/" data-image="5bb71bdc65465e0675ba" class="img-fluid img-responsive">
</p>
Version 2:
<figure>
<img src="http://lorempixel.com/400/200/" data-image="1bdc65465e0675ba1b2b" class="img-fluid img-responsive">
</figure>
Expected behavior:
I expect to replace the p with figure on initial parsing in quill. So, all the images have same parent figure
Actual behavior
figure is always deleted by default
I tried to register figure with blots/block/embed but i can't check and add the figure if the image doesn't have figure on its parent
Thank you

How to deal with spaces in image source

I have a lot of images that unfortunately have spaces in their URL's. These are submitted through user input in another system and saved to a database.
I'm having a problem with angular's management of image sources. It doesn't seem to like spaces or "%20" in the ng-src or src attributes.
This
<img ng-src="{{smallImg}}" alt="" />
Will output this
<img ng-src="" alt="" />
While this
HERE
will output this
HERE
Is there a way to do this without having to go back and rename all these folders?
I got the answer, it is working for me.
You can get this like:-
$scope.smallImg = "http://example.com/path to my/image with/spaces.jpg";
when you bind this do like
<img ng-src="{{smallImg .replace(' ','')}}" alt="" >
I haven't been able to make <img> src empty when using %20, but perhaps you're using an older version of Angular.
$scope.smallImg = "http://www.google.com/my images here/pic.jpg".replace(/ /g, "%20");
...
<img ng-src="{{smallImg}}" alt="Pic goes here" />
Here's a working demo: https://plnkr.co/edit/hTFw8rAg0CRxDGjROjjp
(tried to find an image on the web that had a space in the name, but no luck)
Well, as it turns out it was a secondary plugin, magic360 that was interfering with the image source

Resources