发生重叠而不是在遮罩上上传图像

时间:2019-01-24 11:29:11

标签: image-masking

背景

我允许用户在蒙版图像中上传图像。...

蒙版图像

enter image description here

用户上传的图片

enter image description here

要求:我需要满足以下条件:

enter image description here

问题:我现在所得到的如下:上传的图像在蒙版图像外部而不是内部显示[叠加]。

enter image description here

JS小提琴:http://jsfiddle.net/uLfeaxck/

这里是website url

html

<h3>Upload Photo</h3>
<label id="btn_upload_from_computer" for="fileupload_image" class="button -primary -size-md -full-width">
<svg class="icon-computer" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="23px" height="20.031px" viewBox="0 0 23 20.031" enable-background="new 0 0 23 20.031" xml:space="preserve">
<path id="computer" fill="#FFFFFF" d="M21.793,0H1.207C0.539,0.002-0.002,0.545,0,1.213v14.42c-0.001,0.667,0.539,1.209,1.207,1.211
                h6.47v1.442c0,1-2.433,1-2.433,1v0.722l6.127,0.023h0.068l6.126-0.023v-0.722c0,0-2.434,0-2.434-1v-1.442h6.662
                c0.668-0.002,1.208-0.543,1.207-1.211V1.213C23.002,0.545,22.461,0.002,21.793,0z M21.235,15.11H1.765V1.735h19.47v13.378V15.11z" />
</svg>
From your computer
</label>
<input type="file" id="fileupload_image" style="position: absolute; left: -2000px;" accept="image/*" />

3 个答案:

答案 0 :(得分:2)

我希望下面的例子是您所需要的。

如果蒙版中的图像小于或大于蒙版的高度,则蒙版中的图像将被拉伸。

在测试之前将以下代码段 this image 上传到您的计算机,然后在代码段文件对话框中选择它。

function load(file)
{
    var img = new Image(),
        imgURL = URL.createObjectURL(file);

    //this onload function we need to get width + height of image.
    img.onload = function()
    {
        var width = img.naturalWidth,
            height = img.naturalHeight,
            maskedImage = document.getElementById('masked-image');
        
        maskedImage.setAttribute('xlink:href', imgURL);
        //you can also change this width + height of image before setting of following attribute
        //For ex. the height of #mask1 is 395 and we stretch it for this height like:
        maskedImage.setAttribute('height', 395);
        //in this case DO NOT set width attribute!
        //maskedImage.setAttribute('width', width);
        //maskedImage.setAttribute('height', height);
        //in this case you do not need this onload function.

        URL.revokeObjectURL(imgURL);
    };
    img.src = imgURL;
}
<input type="file" onchange="load(this.files[0])"/><br>

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="324" height="395">
    <mask id="mask1">
        <image xlink:href="https://i.stack.imgur.com/L5daY.png" width="324" height="395"></image>
    </mask>

    <image id="masked-image" xlink:href="" mask="url(#mask1)"></image>
</svg>

答案 1 :(得分:1)

您的蒙版元素

  

z-index:10

所以您需要在css中进行以下更改

.slot_photo.overlay_design{
    z-index:11
}

答案 2 :(得分:0)

就像我在您的other question上所说的那样,实际上您想要的就是所谓的“剪贴蒙版”

这里有些神奇。不客气!

codepen: https://codepen.io/anon/pen/zeZMLz