online upload image and return image url on redirect page
How to handle loading of images that may not exist?
When yous are handling millions of images on your website or mobile application, it is quite possible that over a menses of time, a few images may cease to exist. And when you endeavour to deliver those images on your applications, the users end up seeing a cleaved image like the ane below.
Definitely, this is not something that y'all would desire showing upwards on your application. At that place are a few ways on how yous tin can fix this broken prototype problem to maintain the UX standards of your application.
ane. Periodic data cleanup
If an image does not exist, and so it should be removed from the database.
An prototype URL that is incorrect or points to an prototype that no longer exists, is most likely a information upshot. So the showtime matter that yous can practice to counter the cleaved epitome trouble is to periodically check all images for sanity. You would pick up a list of image URLs that have been added over allow'southward say the last week and check if you are able to retrieve those images (a 200 OK HTTP response). If yes, and so the image stays in your database, else you tin discard the paradigm.
Advantages — Solves the problem at the root level. I-fourth dimension try in writing the script
Disadvantages — Lengthy execution times (nearly impossible) for millions of images, waste matter of network bandwidth and cannot grab errors for the time between two consecutive runs of the cleanup task.
2. Handle it in your application
If an image does not exist, we can supervene upon it with a new 1.
Another way to handle this issue is by listening to the error event that gets fired when the image fails to load. In HTML, this tin can be washed with the onerror aspect of the <img> tag.
<img src="http://example.com/non-existent-image.jpg" onerror="this.onerror=nil;this.src='http://example.com/existent-image.jpg';" /> If the original paradigm fails to load, so it is replaced by the image mentioned in the onerror attribute handler. Like handling tin can exist washed in mobile apps besides.
Advantages — Can handle data problems in existent-time, no need for periodic checks
Disadvantages — Won't work for images loaded as background (at least for websites). Issues may keep in older versions of your website or awarding that cannot be updated with the new code. Different lawmaking for unlike platforms.
3. Handle information technology on your image server
If the image does not exist, and then the server should not ship the error at all.
Your epitome server knows if an image does not exist and sends a 404 Not Found HTTP status lawmaking to the client. Instead, the server itself could supercede information technology with a default image, correct the HTTP status code and then send it to the browser or the awarding.
Advantages— Works in existent time without any periodic checks. No handling needed on any application or any version of whatever application. Works for all kinds of images
Disadvantages — You lot need to build a server that can handle this rerouting for you. Difficult, if non incommunicable, to get it working with simple image delivery setups like CDN + S3 storage.
4. Handle it using a third-party paradigm server like ImageKit.io
ImageKit.io automatically provides server-side handling of non-existent images. Using ImageKit's URL-based transformations, you can specify the default epitome that should exist delivered, if the original image does not be, with the original paradigm URL itself. To give an example,
<!-- The non-existent image URL -->
<img src="https://ik.imagekit.io/demo/img/non_existent_image.jpg" /> <!-- Specifying the default epitome to be displayed in the URL -->
<img src="https://ik.imagekit.io/demo/img/tr:di-medium_cafe_B1iTdD0C.jpg/non_existent_image.jpg" />
The default image is specified using the di- parameter in the URL. In this case, the default epitome is from a cafe.
Being a URL-transformation, this gives you the flexibility to specify a different default image for different kinds of images without having to write any code to handle the error cases. You lot can read more about handling of non-real images or default images using ImageKit here.
Considerations for paradigm caching in error cases
Default images sent instead of a not-existent image should non exist buried at all or if it is cached, then the cache duration should exist small, preferably a few hours. This leaves an opportunity for the not-existent image to be "stock-still" or made available. When this happens, and so the right image would starting time getting delivered to the users automatically. However if the epitome is not expected to be stock-still, then the cache duration can be longer.
Considerations for response code in error cases
If yous wish to handle the mistake case in your application, and then the prototype request should be returned with a 404 condition lawmaking for the onerror to piece of work. If you are delivering the default image instead of a broken paradigm from the server, then the response lawmaking tin exist 302 Temporarily Moved (ideally to prevent caching on intermediate layers like CDN merely treatment with success and error handlers in awarding is impaired) or 200 OK (caching on intermediate layers can be controlled with caching headers)
Since images form a critical part of our application, ensuring that the UX is not broken because of data problems with images is as important. I hope the above techniques will assistance in ensuring a better experience for your users on your website and app.
Please share if you take any other ideas around handling images that no longer exist on a web application.
Source: https://blog.imagekit.io/how-to-handle-loading-images-that-may-not-exist-on-your-website-92e6c3c6ea63
Post a Comment for "online upload image and return image url on redirect page"