简体   繁体   English

上传img(点击 <image> ,不 <input type=file> ),在下面显示 <img> ,客户端调整大小

[英]upload img (by clicking on <image>, not <input type=file>), display it in following <img>,client-side resize

Goal: 目标:

Upload image by clicking on <img> instead of <input type="file"> (there will be 3 uploads, so its just 3 placeholders on page load, then once you click any placeholder, you choose a file, and this file gets to display in respective placeholder). 通过单击<img>而不是<input type="file">来上传图像(将上传3次,因此页面加载时仅占3个占位符,然后单击任何占位符后,选择一个文件,该文件将在相应的占位符中显示)。 What I have now: 我现在所拥有的:

HTML: HTML:

<div class="image-upload">
   <label for="file_input1">
       <img id="send_photo_img1" src="assets/images/index2.png"/>
   </label>
   <input id="file_input1" type="file"/>
</div>  

CSS: CSS:

.image-upload > input
{
    display: none;
}

.image-upload img
{
    display: inline-block;
    margin-right:6px;
    width: 90px;
    height: 90px;
    cursor: pointer;
}

Jquery: jQuery:

function readURL(input) {

if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function (e) {
        $('#send_photo_img1')
            .attr('src', e.target.result);
    }

    reader.readAsDataURL(input.files[0]);
}
}    

$("#send_photo_img1").change(function(){
    readURL("#file_input1");
});

While scripts like plupload.js or dropzone.js do that, but they also got lots of stuff I don't really need (ie UI that is not quite fitting my need or lots of code options not really needed, but taking space), so trying to implement this in scope required for my current needs. 尽管诸如plupload.jsdropzone.js类的脚本可以做到这一点,但它们还有很多我确实不需要的东西(即,UI不太适合我的需要,或者不是很多真正需要代码的选项,但占用空间),因此尝试在我当前需要的范围内实施此操作。

The current code only lets me choose the desired img, but not display it. 当前代码仅允许我选择所需的img,而不能显示它。 Currently, once the image is chosen, the loading sign is displayed for some time, but nothing happens afterward. 当前,一旦选择了图像,就会显示加载标志一段时间,但是此后什么也没有发生。 No error output in Chrome F12 also. Chrome F12中也没有错误输出。

Figured it out and turned out to be quite straightforward: 弄清楚了,结果非常简单:

HTML 的HTML

<div class="img_upload">
    <img class="send_photo_img" id="send_photo1_img" src="assets/images/index2.png"/>
    <input id="send_photo1" name="send_photo1" class="send_photo_input" type="file"/>
</div>
<div class="img_upload">
    <img class="send_photo_img" id="send_photo2_img" src="assets/images/index2.png"/>
    <input name="send_photo2" class="send_photo_input" type="file"/>
</div>  

Have as many img as you like. 拥有尽可能多的img。

Further on, with CSS you hide <input> and thereon trigger the click on <input> via JS, once it catches the click on <image> : 进一步,在CSS中,您隐藏了<input>并在它捕获到<image>的点击后,通过JS触发了<input>的点击:

CSS 的CSS

.img_upload input
{
    display: none;
}

JS (to trigger the click) JS (触发点击)

$(".img_upload img").click(function(){
        img = $(this);
        input = img.next(".send_photo_input");
        var newUrl;

        input.trigger("click");
        input.change(function(){
            newUrl = resize_and_draw(this, img.width());
            url.push(newUrl);
    });
});

and finally, you do the rest with HTML5 canvas (nowadays it's supported by almost anything except for Opera Mini (global share of users ~2,81% as of early Oct 2017)). 最后,其余部分由HTML5画布完成(如今,除了Opera Mini(截至2017年10月上旬,全球用户份额约为2,81%)之外,几乎所有内容都支持它)。

You may be also interested to resize the image at the client (to maintain bandwidth, decrease load speed etc), so below is the complete code: 您可能还想在客户端调整图像大小(以保持带宽,降低加载速度等),因此以下是完整的代码:

function resize_and_draw (input, targetH) { 函数resize_and_draw(input,targetH){

if (input.files && input.files[0] && /\.(jpe?g|png|gif)$/i.test(input.files[0].name)) {

    var reader = new FileReader();
    reader.onload = function (e) {

        var img = new Image();
        img.src = reader.result;
        img.onload = function() {

          canvas = document.createElement( 'canvas');
            canvas.width = img.naturalWidth;   
            canvas.height = img.naturalHeight;
            context = canvas.getContext('2d');
            context.drawImage( img, 0, 0, img.naturalWidth, img.naturalHeight );

                targetHeight = targetH;
            targetWidth = img.naturalWidth * targetHeight / img.naturalHeight;

            //resize
            resample_hermite(canvas, img.naturalWidth, img.naturalHeight, targetWidth*2, targetHeight*2);
            url = canvas.toDataURL("image/png");
            $(input).prev(".send_photo_img").attr('src', url);

        }     
    }

    reader.readAsDataURL(input.files[0]);

    return url;
}
}

Hope this will help your goals. 希望这会帮助您实现目标。 Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM