这是将 jpg 或 png 转换为 webp
的工作代码谷歌的新图像格式平均比 jpg 或 png 小 30-40%
1.用Chrome打开
2.设置质量
3.在页面内放置图片
4.等待(取决于大小..先尝试小图片)
5.悬停图片查看大小差异
6.要将其正确保存为 webp,只需单击它
基本上 chrome 添加了将图像/webp 和质量添加到 toDataURL 函数的可能性
canvas.toDataURL('image/webp',quality(0-1))
压缩很棒。但我有一个小问题...png 不透明..
它会是什么?也许将 Canvas 设置为透明?怎么办?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
html,body{
width:100%;
height:100%;
margin:0;
padding:0;
display:-webkit-flex;
display: flex;
}
a{margin: auto;}
.imG{
max-width:800px;
max-height:400px;
}
form{
position:fixed;
bottom:0px;left:0px;
}
</style>
<script>
(function(W){
W.URL=W.URL||W.webkitURL;
var D;
function init(){
D=W.document;
D.body.addEventListener('dragstart',pdrop,false);
D.body.addEventListener('dragenter',pdrop,false)
D.body.addEventListener('dragover',pdrop,false);
D.body.addEventListener('dragleave',pdrop,false);
D.body.addEventListener('dragend',pdrop,false);
D.body.addEventListener('drop',drop,false);
}
function readablizeBytes(bytes) {
var s=['bytes','kB','MB','GB','TB','PB'],m=Math,e=m.floor(m.log(bytes)/m.log(1024));
return (bytes/Math.pow(1024,e)).toFixed(2)+" "+s[e];
}
function pdrop(e){
e.stopPropagation();
e.preventDefault();
}
function drop(e){
e.stopPropagation();
e.preventDefault();
console.log(e.dataTransfer.files[0]);
var f=e.dataTransfer.files[0];
var i=document.createElement('img');
i.onload=function(e){
window.URL.revokeObjectURL(this.src);
var b=document.createElement('canvas');
b.width=this.width;b.height=this.height;
var c=b.getContext("2d");
c.drawImage(this,0,0);
this.onload=function(){
this.className='imG';
var d=document.createElement('a');
g=f.name.split('.');g.pop();
d.download=g.join('')+'.webp';
d.href=this.src;
d.title=readablizeBytes(atob(this.src.split(',')[1]).length)+' vs '+readablizeBytes(f.size);
d.appendChild(this);
D.body.appendChild(d);
}
this.src=b.toDataURL('image/webp',D.getElementsByName('o')[0].innerText*0.01);
};
i.src=window.URL.createObjectURL(f);
}
W.addEventListener('load',init,false);
})(window)
</script>
<title>Image To Google's webp format</title>
</head>
<body>
<form onsubmit="return false" oninput="o.value=v.valueAsNumber">
<label for="q">Choose the quality and then drop a image</label>
<input name="v" id="q" type="range" min="0" max="100" value="0">
<output for="q" name="o">0</output>/100
</form>
</body>
</html>
ps.:为了获得最佳效果,每次都使用 jpg 格式并重新加载页面,因为它只是附加每个新图像
最佳答案
这是一个 known issue with Chrome .
从上面链接的评论中可以看出:
WebCore discards the alpha channel in toDataURL("image/webp") and encodes the image from the RGB pixels because WEBP never supported alpha channel. toDataURL("image/webp") has been that way since http://trac.webkit.org/changeset/99319 (shipped over a year ago).
WEBP only recently added support for alpha channel and there is no way to select that in a toDataURL. We could force it one way or the other, I suppose, but should the default encoding be 1) no-alpha as now, or 2) alpha as requesting in this bug?
该问题目前尚未解决,但可能会在不久的将来得到解决。
对于可能的解决方法,您可以使用 WEBP 格式的自定义实现,声明它支持 alpha channel 。它可以将 PNG(和 JPEG)转换为 WEBP 格式。
解决方案称为WEBPJS and can be found here (和 examples can be found here )。
(但也有可能 Chrome 无法解码 webp 图像,即使它们包含 alpha channel ......)。
带有 alpha channel 的 webp 图像目前即使在谷歌(发明者)自己的 Chrome 中也没有得到很好的支持,并且在其他浏览器中也缺乏支持。我的建议是在支持得到改善之前将其搁置一旁,而是使用 PNG,因为它具有广泛的支持。
使用 tinypng.com 等工具以减小文件的大小。
关于javascript - 即时客户端图像到 webp 转换器......但 png 不透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18300226/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我正在编写一个方法,它将在一个类中定义一个实例方法;类似于attr_accessor:classFoocustom_method(:foo)end我通过将custom_method函数添加到Module模块并使用define_method定义方法来实现它,效果很好。但我无法弄清楚如何考虑类(class)的可见性属性。例如,在下面的类中classFoocustom_method(:foo)privatecustom_method(:bar)end第一个生成的方法(foo)必须是公共(public)的,第二个(bar)必须是私有(private)的。我怎么做?或者,如何找到调用我的cust
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
我收到格式为的回复#我需要将其转换为哈希值(针对活跃商家)。目前我正在遍历变量并执行此操作:response.instance_variables.eachdo|r|my_hash.merge!(r.to_s.delete("@").intern=>response.instance_eval(r.to_s.delete("@")))end这有效,它将生成{:first="charlie",:last=>"kelly"},但它似乎有点hacky和不稳定。有更好的方法吗?编辑:我刚刚意识到我可以使用instance_variable_get作为该等式的第二部分,但这仍然是主要问题。
我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path
2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p