Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add images in an html file?

Anytime I have read an article on html and images, I have seen an anchor tag like this:

<img src="http://www.tizag.com/pics/htmlT/sunset.gif" />

However, in my case I have stored the image in AWS-S3 and I am reading image from S3. This, I do not upfront have a path like "http://www.tizag.com/pics/htmlT/sunset.gif"

So what is the most common technique to embed image in the html page when image is stored in S3, and the path to image is not known ?

In case my question is confusing, I will ask it differently.

I am building a project, which is simple. Whenever user is logged in he gets a page saying "Welcome" and below welcome note is is profile picture.

But, assume I have 10 users, each of these 10 users will have a different URL to the image.

eg:

<img src = "http:bucket.amazonaws.com/USER1'> for user 1

<img src = "http:bucket.amazonaws.com/USER2'> for user 2

and so on.

So the image I will display is not known until run-time and path to image is dependent on who logs in. How to make my HTML page smart so that the image src is not a constant and can be made flexible depending on who logs in ?

SOLUTION IN JSP, WHICH I COULD DO, THANKS TO SO MANY ANSWERS:

<body>

<% String url = (String)request.getAttribute("url"); %>

<img src = <%= url %>></img> 

</body>

This JSP code is called from the servlet.

request.setAttribute("url", "URL to image.");

RequestDispatcher view = request.getRequestDispatcher("URLImage.jsp");
view.forward(request, response);
like image 354
JavaDeveloper Avatar asked Nov 11 '15 18:11

JavaDeveloper


People also ask

Can you add JPEG to HTML?

Use the image tag and the specific URL where the picture is stored like this: img scr="URL of the JPG file". Put the image code in where you want and the JPG file will be embedded into the HTML document. The image will display once the user opens that document in a web browser.

Where do you put images in HTML?

To insert image in an HTML page, use the <img> tags. It is an empty tag, containing only attributes since the closing tag is not required. Just keep in mind that you should use the <img> tag inside <body>… </body> tag.

Why can't I add image in HTML?

Incorrect File Paths When you add images to a site's HTML or CSS file, you must create a path to the location in your directory structure where those files reside. This is code that tells the browser where to look for and fetch the image from. In most cases, this would be inside a folder named images.


2 Answers

You require some kind of server side programming language to generate the the paths for the image dynamically for each user. Anyway you do have a setup for the users to login. For that, I believe you have some kind of framework in some server side scripting language like PHP(Laravel, Wordpress, CodeIgniter), Java(Spring), Ruby(Rails), Python(Django).

So when a user logs in, your login script should validate the user and you will render a particular html page. And you should show the name of the user and a particular image. In PHP or Ruby on Rails you can embed the actual PHP code or ruby code in HTML.

In PHP, like this :

<html>
    <head> </head>
    <body>
    <!-- say PHP Session varibale contains the logged in user's name & bucket name -->
        <img src="<?php echo "https://"+ $_SESSION["bucket_name"] +".amazonaws.com/"+$_SESSION["username"] ?>" />
    </body>
</html>

In Ruby on Rails, like this:

<html>
    <head> </head>
    <body>
    <!-- say Ruby - Rails controller passes @username & @bucketname to view -->
        <img src="<%= "https://"+ @bucketname +".amazonaws.com/" + @username %>" />
    </body>
</html>

In Python - Django, like this :

<html>
    <head> </head>
    <body>
    <!-- In Python - Django Suppose you pass context variables username & bucketname to template -->
        <img src="https://{{ bucketname }}.amazonaws.com/{{ username }}" />
    </body>
</html>

So what I would suggest is, you get familiar with the server side scripting language that you intend to use or are already using and manipulate the url to be generated based on logged in user accordingly.

like image 186
Sony Mathew Avatar answered Oct 05 '22 20:10

Sony Mathew


If your bucket was named my-bucket and the image file was named my-image.png, then the format for the url would be http://my-bucket.s3.amazonaws.com/my-image.png.

In order to access the file though, you need to have a policy attached to your bucket that allows anyone to access the file. Below is a policy that works for this example.

{
    "Id": "some-policy-id",
    "Version": "2012-10-17",
    "Statement": [
    {
        "Sid": "some-statement-id",
        "Principal": "*",
        "Action": [
        "s3:GetObject"
        ],
        "Effect": "Allow",
        "Resource": "arn:aws:s3:::my-bucket/my-image.png" 
   }]
}

See the docs for more information about accessing a bucket.

Also note that the bucket name must be DNS compatible for this to work. See here under Rules for Bucket Naming.

like image 29
David Morales Avatar answered Oct 05 '22 18:10

David Morales