Having Multiple Image URL's Render Same Image - ruby-on-rails

I'm trying to create code that will allow me to use different image URL's but essentially render the same image.
For example, this would be across potentially thousands of pages:
<img src="https://www.website.com/image-path-unique-name-1.jpg" />
<img src="https://www.website.com/image-path-unique-name-2.jpg" />
<img src="https://www.website.com/image-path-unique-name-3.jpg" />
But instead of rendering a 404, an image would exist based on:
<img src="https://www.website.com/core-image.jpg" />
Is there some way I can do this? Either by systematically creating multiple copies of an image based on an initial image? Or maybe another solution?

Related

ASP.NET Core how to display images from www/images

I am new to ASP.Net Core and I have images in wwwroot/images dir and I want to display the images when i clicked them. I made a View page to view all the images (and data as well) and when i clicked the images it will go to the detail pages.
Here is the detail page without images displaying.
and here is when the full path of images https://localhost:44325/Catelog/Details/images/book2.jpg
All my images are in wwwroot/images dir.
Details.cshtml
<div class="col-md-4">
<div>
<img src="#Model.imageUrl" class="detailImage" />
</div>
</div>
So how can I show my images or say how do wwwroot/images share its resources (maybe ?)
If you model.imageURL equals that:
images/book2.jpg
just add / before #Model.imageUrl like :
<img src="/#Model.imageUrl" class="detailImage" />
If it's like :
book2.jpg
change code like this:
<img src="/images/#Model.imageUrl" class="detailImage" />

Edit the url in ERB to do server side scaling

The image is rendered on my blog of website using following erb code
<a href="<%= article_url(article)%>">
<img src="<%= article.main_image_url%>" alt="<%= article.title%>" /</a>
The main image url is actually stored in cloudfront on the url
https://s3.ap-south-1.amazonaws.com/mywebsite-dev/uploads/article/main_image/100/wonderful_image__1_.jpg
A scaled down version of size 500 X 250 pixel is stored at location
https://s3.ap-south-1.amazonaws.com/mywebsite-dev/uploads/article/main_image/100/large_wonderful_image__1_.jpg
My model of the article only saves the main image url. However, while uploading the image to s3, I do create a scaled down version of the image and add 'large_' prefix. This can be seen in the above url examples.
As of now, my erb code, renders the main image on the webpage and I need to edit this code, so that it renders the scaled down image instead. Basically, I need to edit this
<img src="<%= article.main_image_url%>"
so that erb changes
(The urls below are changed to blank client website'
https://s3.ap-south-1.amazonaws.com/mywebsite-dev/uploads/article/main_image/100/wonderful_image__1_.jpg
to this
https://s3.ap-south-1.amazonaws.com/mywebsite-dev/uploads/article/main_image/100/large_wonderful_image__1_.jpg
May be I need to split the article url by '/', take out the last part and add 'large_' to it.
Thanks in advance for the help. I am quite new to ROR and more into python.
Regards
I figured it out.It may be not a clean solution but it worked for me as of now.
<img src="<%= article.main_image_url.rpartition('/').first+'/large_'+article.main_image_url.rpartition('/').last%>" alt="<%= article.title%>" />
Thanks to everyone

Display desired url of webpage inline with replacement of html code like Google Translate

I want to make app which will switch vocabulary in desired url of webpage Japanese to English.
But firstable I want start form just simply display desired url of webpage inline lust like Google Translate.(See here)
I tried to use open-uri library, and get html from webpage, and display it with code below.
Controller:
def submit
require 'open-uri'
charset = nil
#html = open(params[:url]) do |f|
charset = f.charset
f.read
end
end
View:
<%= #html.html_safe %>
But I have problem with this, which simply won't load Images and some stylesheets in page.
Any way to display image and css?
If you are displaying HTML from another domain on your domain, then any relative URLs used in the HTML (links, scripts, styles, etc.) aren't going to correspond with files that exist on your domain. You are going to have to parse the HTML and either expand the URLs to full URLs that point to the original domain (easier) OR you are going to have to download all the content to your domain using the same path structure.
Example:
To display an image, a site (example.com) could use any of the following src attributes:
<img src="http://example.com/images/logo.jpg" alt="My Logo" />
<img src="/images/logo.jpg" alt="My Logo" />
<img src="../../images/logo.jpg" alt="My Logo" />
If the site always used the full URL like in the first example, you wouldn't have a problem displaying the image. If they used the second one, your site wouldn't be able to find "/images/logo.jpg" on your server. You would have to parse all the img elements and prepend the domain. The third example you would have to do even more work by determining the path to the image based on the current URL you were displaying.

Drupal 7: how to get image url to display image

In Drupal 7, I'm using the feeds module to pull in a bunch of books
That's all working perfectly. I am not setting up some views.
the feed pulls in an image url to on o my field like this [http://img.techbook.com/techwords/content/bk/blak/003834/full_image.jpg
but of course I need
<img src="http://img.techbook.com/techwords/content/bk/blak/003834/full_image.jpg" />
to display the image on the page
What is the best method of altering this fields' text, or is there a method so I can automatically format correctly. There are hundres of these, so manually do it is not an option
You can either use Feeds Tamper, which let's you manipulate the incoming feed data before it comes in (ie. wrap the url with img tags): http://drupal.org/project/feeds_tamper
OR
You can simply import the URLs into a field, and then render the URL with a template file that has the img tag wrapped around the field.
Like so: in node--content-type-name-here.tpl.php
<img src="<?php print render($content['field_url']); ?>" />
I would recommend the latter and only use Feeds Tamper for more serious manipulations.

How to have multiple links from one image in rails

I have one image from which I have to have 7 different links based on the part of the image clicked. How do I go about it using rails?
For future reference, you're talking about "image maps" - there's various tutorials for these, eg
http://www.onextrapixel.com/2009/04/30/how-to-create-multiple-links-on-a-single-image-with-image-map/
As far as i know this isn't really a rails question, as image maps are a totally front-end thing: like js, they only relate to the page as seen in the browser. As far as rails is concerned, when you click on a link it doesn't care whether that link was on an image map or not.
So, it's just some raw html that you would put in your view template.
I suppose you could make the image maps data-driven though, so that for example you store an image filename in the db and then save any map data in the db too, in an image_maps table. It's only worth doing this if you want to make the image maps user-editable, like flicker does for example. (flickr lets you draw a rectangle on an existing photo and tag it with some text)
just use url_for to generate the needed url
<img usemap="image_link" src="..." alt="" width="235" height="32" style="display: block;" />
<map name="image_link">
<area href="<%= url_for root_path %>" alt="Home" coords="0,0,189,19" shape="rect" />
<area href="<%= url_for :controller => "post", :action => "all" %>" alt="All posts" coords="190,0,386,22" shape="rect" />
<area href="<%= url_for new_resource1_path %>" alt="New resource1" coords="387,0,657,23" shape="rect" />
</map>
the root_path is defined as the home page and resource1 as a resource in config/routes.rb.
This is more of a javascript/flash type thing. You can absolutely position divs with links if you know exactly where the links need to go and you are not looking for an area for the user to click because this will just generate a link with text in a specific position.
Sounds to mee like you will need to make a flash app or do javascript workaround. This is not a Rails type issue so your solution won't come from Ruby/Rails.

Resources