Solution 1 :

first “src” called “attribute”

what you have to do

1-locate you image on server -> see where did you upload the file first

2-take the path of the file and put it inside the “src” “attribute”

ex:

1-you uploaded the image lets say project/images

2-your image path will be in project/images/image.jpg

3-take this part and but it inside the src attribute

 src="project/images/image.jpg"

Solution 2 :

You can try like this

<input type="file" id="docpicker" accept="image/*" onChange={updateImageDisplay} multiple></input>
<div class="previewContainer">
   <p>No files currently selected for upload</p>
</div>
const updateImageDisplay = ss => {
  const input = document.querySelector('input');
  const previewContainer = document.querySelector('.previewContainer');
  [...input.files].forEach(file => {
    const image = document.createElement('img');
    image.src = URL.createObjectURL(file);
    previewContainer.appendChild(image);
  })
}

Solution 3 :

Try using canvas to crop the image

const updateImageDisplay = () => {
  const input = document.querySelector('input');
  const previewContainer = document.querySelector('.previewContainer');
  [...input.files].forEach(file => {
      var canvas = document.createElement('canvas');
      var context = canvas.getContext('2d');
      var imageObj = new Image();
      imageObj.onload = function() {
        var sourceX = 150;
        var sourceY = 0;
        var sourceWidth = 150;
        var sourceHeight = 150;
        var destWidth = sourceWidth;
        var destHeight = sourceHeight;
        var destX = canvas.width / 2 - destWidth / 2;
        var destY = canvas.height / 2 - destHeight / 2;

        context.drawImage(imageObj, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight);
      };
      imageObj.src = URL.createObjectURL(file);
      previewContainer.appendChild(canvas);
  })
}

Problem :

How do I insert an uploaded image into an html < img > tag?
Is it possible to create an < img > tag as placeholder for uploaded images?

enter image description here
I can select an image with “< input type=”file” name=”fileToUpload” id=”fileToUpload” >” and after this i want to send that image to an img placeholder.

And is it possible to delete the uploaded image from the placeholder after the user closed the website?

Thanks a lot!

Comments

Comment posted by this

Please check

Comment posted by qws

Thanks @TatvasoftCorp it worked. Do you know how I can crop the image when the user upload it?

Comment posted by Mahesh

You can use Jcrop to crop the image

Comment posted by qws

@TatvasoftCorp Oh sorry, I dont need the crop function. I mean that the image just resize to witdh=”96px” after upload. I used this function for the upload: jsfiddle.net/LvsYc

Comment posted by Mahesh

I guess you want to resize the image just to show resized image in img tag, you could give width to img tag

Comment posted by qws

Thanks for your answer. Can you also tell me how to crop the image which gets uploaded (at the same time)?

Comment posted by stackoverflow.com/questions/1855996/crop-image-in-php

check this

Comment posted by jsfiddle.net/LvsYc

Oh I think i just need to set the image size of the uploaded img, no need for crop function. It should be in 96px width after upload. upload function:

Comment posted by qws

Thanks for your answer. Can you also tell me how to crop the image which gets uploaded (at the same time)?

Comment posted by Raj Kumar

you can crop using canvas. i attached the answer

Comment posted by qws

But did the code automaticly crop the original image size to 96px at upload? Or is this code for crop it after upload. It was my faul that I wanted to “crop” the image. I mean that I just need to resize the original size of the uploaded image to width 96px. Is it possible? If I add the width to the < img > tag and select that image to my graph, it just has the original size, not the 96px witdh.

Comment posted by cropperjs

if you want to crop before uploading the image to server, you can use some external library to help for cropping, something like

By