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?
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