canvas在线生成宣传图片插件(帝国CMS7.2 UTF-8)
整合tx推出的一款AlloyImage,HTML5图片处理引擎
含滤镜,水印,文字
XML/HTML Code复制内容到剪贴板
- <script type="text/javascript" src="theme/js/alloyimage.js"></script>
- <ul id="effects" class="effects">
- <li class="e_item">
- <div class="imgWrapper">
- </div>
- </li>
- </ul>
- <script>
- // 初始化
- function init(){
- var EasyReflection = {
- "美肤" : "e1",
- "素描" : "e2",
- "自然增强" : "e3",
- "紫调" : "e4",
- "柔焦" : "e5",
- "复古" : "e6",
- "黑白" : "e7",
- "仿lomo" : "e8",
- "亮白增强" : "e9",
- "灰白" : "e10",
- "灰色" : "e11",
- "暖秋" : "e12",
- "木雕" : "e13",
- "粗糙" : "e14"
- };
- var effectModel = '<li class="e_item"><div class="imgWrapper" onmousedown="changeEffect(this)"><img src="theme/images/effect/{pic}.png" alt="" />{effect}</div></li>';
- var html = '<li class="e_item"><div class="imgWrapper" onmousedown="changeEffect(this)"><img src="theme/images/effect/e1.jpg" alt="" />原图</div></li>';
- for(var i in EasyReflection){
- html += effectModel.replace("{effect}",i.length < 3 ? i + "效果" : i).replace("{pic}", EasyReflection[i]);
- };
- // 获取各处理效果图填充html
- document.getElementById("effects").innerHTML = html;
- }
- // 滤镜处理
- function changeEffect(obj){
- var text = obj.childNodes[1].nodeValue.replace("效果","");
- if(text == "原图"){
- var a = obj.firstChild;
- var url = a.getAttribute("src");
- img.setAttribute("src", originalImage);
- }else{
- msgEle.style.display = "block";
- setTimeout(function(){
- ai.clone().ps(text).replace(img).complete(function(){
- msgEle.style.display = "none";
- });
- }, 2);
- }
- }
- </script>
1、上传:
XML/HTML Code复制内容到剪贴板
- <input type="file" name="file_input" id="file_input" class="file_input" value="打开文件" />
- <!-- 显示默认图片,也可以为空 -->
- <img src="theme/pic/demo/bulding.jpg" class="pic" id="pic" alt="" draggable="false"/>
2、javascript:
JavaScript Code复制内容到剪贴板
- img = document.getElementById("pic");
- uploadBtn = document.getElementById("file_input");
- bindButtonEvent(uploadBtn, "change", openFile);
- function bindButtonEvent(element, type, handler)
- {
- if(element.addEventListener) {
- element.addEventListener(type, handler, false);
- } else {
- element.attachEvent('on'+type, handler);
- }
- }
- // 打开图片文件,uploadBtn全局变量
- function openFile(){
- if(typeof FileReader==='undefined'){
- layer.msg("抱歉,你的浏览器不支持 FileReader");
- uploadBtn.setAttribute('disabled','disabled');
- }else{
- layer.load();
- var file = uploadBtn.files[0];
- if(!/image\/\w+/.test(file.type)){
- layer.closeAll('loading');
- layer.msg("上传的文件必须为图片格式~");
- return false;
- }
- var reader = new FileReader();
- reader.readAsDataURL(file);
- reader.onload = function(e){
- img.setAttribute("src",this.result);
- layer.closeAll('loading');
- }
- }
- }
canvas 两张图片绘图:
PHP Code复制内容到剪贴板
- // 绘图,backgroundPic和waterImg参数为指定的dom
- function composite(backgroundPic,waterImg){
- var canvas = document.getElementById("myCanvas");
- var ctx = canvas.getContext("2d");
- //绘制图片,无缩放
- //1、加载背景图
- if(backgroundPic){
- canvas.width = backgroundPic.width;
- canvas.height = backgroundPic.height;
- ctx.drawImage(backgroundPic, 0, 0);
- }
- // 如果有水印图的话
- if(waterImg){
- //2、加载水印图片
- var img2 = new Image(100, 300);
- img2.src = waterImg.getAttribute("src");
- ctx.drawImage(img2,20,20);
- }
- }
保存canvas为本地图片:
XML/HTML Code复制内容到剪贴板
- <canvas id="myCanvas" width="750" height="1334"></canvas>
- <a class="save-local-img">保存</a>
- <script>
- // 保存为本地图片
- $(".save-local-img").click(function(){
- downloadCanvas(this, 'myCanvas', 'download.png');
- });
- // canvas 保存为本地图片
- function downloadCanvas(link, canvasId, filename) {
- link.href = document.getElementById(canvasId).toDataURL();
- link.download = filename;
- }
- </script>
上一篇 帝国CMS7.2 机器人插件
下一篇 echarts图形统计