jQuery: there is a way to apply colour (tint) to an image?

有些话、适合烂在心里 提交于 2019-11-27 02:09:25

Simplest way I can think of is overlaying a semitransparent div over the image.

A little example:

HTML

<div id="overlay" class="overlay"></div>
<img id="myimg" src="img.jpg" />

CSS

.overlay
    {
    display: block;
    position: absolute;
    background-color: rgba(200, 100, 100, 0.5);
    top: 0px;
    left: 0px;
    width: 0px;
    height: 0px;
    }

JS (with JQuery)

overlay = $("#overlay");
img = $("#myimg");
overlay.width(img.css("width"));
overlay.height(img.css("height"));
overlay.css("top", img.offset().top + "px");
overlay.css("left", img.offset().left + "px");

nico's answer is great if you're after a simple tinge of a colour - however, if you're talking about desaturating an image and then applying a tint (so that the image is only in green for example) then you can have a look at image manipulation with <canvas>

After some googling, I found this library for canvas that focuses on photo manipulation operations: https://github.com/meltingice/CamanJS

Bojangles

I'm not sure if you're using PHP, but it's not possible with JavaScript/jQuery. With PHP, you can use the GD image library to tint the image before it's sent to the client. This thread should help :-)

Also, this forum thread talks about ImageMagick to tint the image as well.

I'm very sorry if you aren't/can't use PHP, however JavaScript can't do what you want.

James

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!