简体   繁体   English

我想在DIV中合并所有图像并允许用户下载(js powered)

[英]I would like to merge all images within DIV and allow user to download (js powered)

I'm working on a client's web design and development powered by Wordpress and Woocommerce, the plugin is great but there's a few functions I wish it had. 我正在开发一个由Wordpress和Woocommerce提供支持的客户端网页设计和开发,这个插件很棒但是我希望它有一些功能。 The main one is giving the ability for the user to save the preview of their configuration. 主要的是让用户能够保存其配置的预览。

The plug-in is 'Woocommerce Visual Products Configurator': 该插件是'Woocommerce Visual Products Configurator':

It allows buyers to build their own item by selecting from a series of different attributes for different parts of an item. 它允许买家通过从项目的不同部分的一系列不同属性中进行选择来构建他们自己的项目。 Ex, choosing different types of Soles for a shoe, as well as being able to choose from a series of laces for the same shoe. 例如,为鞋子选择不同类型的鞋底,以及能够为同一鞋子选择一系列鞋带。

My Issue: The configuration images are layered on top of each other as the user selects their options so any normal "save as" function will just save the top image. 我的问题:当用户选择他们的选项时,配置图像彼此叠加,因此任何正常的“另存为”功能都只会保存顶部图像。

I have succesfully managed to combine and save the images using html2canvas-Data URL() but the way it generates the screenshots means the quality becomes very poor. 我已经成功地使用html2canvas-Data URL()组合并保存图像,但它生成屏幕截图的方式意味着质量变得非常差。

My thoughts are to merge all the images within the "vpc-preview" DIV then force the download. 我的想法是合并“vpc-preview”DIV中的所有图像然后强制下载。

Hunting through the functions of the plugin I found this: 通过插件的功能进行搜索,我发现了这个:

function merge_pictures($images, $path = false, $url = false) {
$tmp_dir = uniqid();
$upload_dir = wp_upload_dir();
$generation_path = $upload_dir["basedir"] . "/VPC";
$generation_url = $upload_dir["baseurl"] . "/VPC";
if (wp_mkdir_p($generation_path)) {
    $output_file_path = $generation_path . "/$tmp_dir.png";
    $output_file_url = $generation_url . "/$tmp_dir.png";
    foreach ($images as $imgs) {
        list($width, $height) = getimagesize($imgs);
        $img = imagecreatefrompng($imgs);
        imagealphablending($img, true);
        imagesavealpha($img, true);
        if (isset($output_img)) {
            imagecopy($output_img, $img, 0, 0, 0, 0, 1000, 500);
        } else {
            $output_img = $img;
            imagealphablending($output_img, true);
            imagesavealpha($output_img, true);
            imagecopymerge($output_img, $img, 10, 12, 0, 0, 0, 0, 100);
        }
    }
    imagepng($output_img, $output_file_path);
    imagedestroy($output_img);
    if ($path)
        return $output_file_path;
    if ($url)
        return $output_file_url;
} else
    return false;
}

However the function isn't called anywhere. 但是,该功能不会在任何地方调用。 There's also a couple of "save" buttons that are commented out which makes me wonder if they removed from a previous build. 还有一些被注释掉的“保存”按钮让我想知道它们是否从之前的版本中移除了。

Ideally I'd like the user to be able to instantly share their creation to facebook but thought this would be a good start. 理想情况下,我希望用户能够立即将他们的创作分享到Facebook,但认为这将是一个良好的开端。

Any ideas would be greatly appreciated! 任何想法将不胜感激!

Thanks! 谢谢!

UPDATE: 更新:

I've managed to use the following code to output an alert of the url's of each of the config images. 我已设法使用以下代码输出每个配置映像的URL的警报。 Useless for the user but at least I know it's targeting exactly what I need. 对用户来说没用,但至少我知道它的目标正是我需要的。

function img_find() {
var imgs = document.querySelectorAll('#vpc-preview img');
var imgSrcs = [];

for (var i = 0; i < imgs.length; i++) {
    imgSrcs.push(imgs[i].src);
}

return alert(imgSrcs);
}

Any suggestions on how to manipulate this and merge the corresponding image of each URL then force the download? 有关如何操作这个并合并每个URL的相应​​图像然后强制下载的任何建议?

Cick to see Fiddle 小丑看小提琴

UPDATE 2: The following fiddle lets users upload images and then merges them into a single photo for download. 更新2:以下小提琴允许用户上传图像,然后将它们合并到一张照片中进行下载。 Unfortunately my coding skills aren't good enough to manipulate this into using the SRC urls of images within certain DIV and to Merge all the photos on top of each other. 不幸的是,我的编码技巧还不足以将其用于在某些DIV中使用SRC的图像网址并将所有照片合并到彼此之上。

Fiddle 小提琴

function addToCanvas(img) {
// resize canvas to fit the image
// height should be the max width of the images added, since we rotate -90        degree
// width is just a sum of all images' height
canvas.height = max(lastHeight, img.width);
canvas.width = lastWidth + img.height;

ctx.clearRect(0, 0, canvas.width, canvas.height);
if (lastImage) {
    ctx.drawImage(lastImage, 0, canvas.height - lastImage.height);
}

ctx.rotate(270 * Math.PI / 180); // rotate the canvas to the specified degrees
ctx.drawImage(img, -canvas.height, lastWidth);

lastImage = new Image();
lastImage.src = canvas.toDataURL();
lastWidth += img.height;
lastHeight = canvas.height;
imagesLoaded += 1;
}

基于这个问题/答案 ,我将看一下node-canvas

What I have done in the past is use a @print css file to only capture the divs needed. 我过去所做的是使用@print css文件来捕获所需的div。 Very simple concept but works well. 非常简单的概念,但运作良好。

After looking at your link, that is exactly the situations I use it in..online designers. 看了你的链接,这正是我在..在线设计师中使用它的情况。 It is a little more difficult keeping your layers responsively aligned, but in the end I feel your codes become cleaner and it works well across devices and OS'. 保持你的图层响应对齐有点困难,但最后我觉得你的代码变得更清晰,它在各种设备和操作系统上运行良好。

If you dont want PDF and a simple preview and download than html2image.js is perfect. 如果你不想要PDF和简单的预览和下载比html2image.js完美。

 $(document).ready(function(){ var element = $("#html-content-holder"); // global variable var getCanvas; // global variable $("#btn-Preview-Image").on('click', function () { html2canvas(element, { onrendered: function (canvas) { $("#previewImage").append(canvas); getCanvas = canvas; } }); }); $("#btn-Convert-Html2Image").on('click', function () { var imgageData = getCanvas.toDataURL("image/png"); // Now browser starts downloading it instead of just showing it var newData = imgageData.replace(/^data:image\\/png/, "data:application/octet-stream"); $("#btn-Convert-Html2Image").attr("download", "your_pic_name.png").attr("href", newData); }); }); 
 <script src="https://github.com/niklasvh/html2canvas/releases/download/0.4.1/html2canvas.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 500px; padding-left: 25px; padding-top: 10px;"> Place any code here </div> <input id="btn-Preview-Image" type="button" value="Preview"/> <a id="btn-Convert-Html2Image" href="#">Download</a> <br/> <h3>Preview :</h3> <div id="previewImage"> </div> 

For High quality you want to use rasterizeHTML.js 对于高质量,您希望使用rasterizeHTML.js

Found here: https://github.com/cburgmer/rasterizeHTML.js/blob/master/examples/retina.html 在此处找到: https//github.com/cburgmer/rasterizeHTML.js/blob/master/examples/retina.html

暂无
暂无

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

相关问题 我正在尝试创建一个机器人,我希望我的所有消息都包含在 JS 中的同一个 div 中 - I'm trying to create a bot and I would like all my messages to be contained with in the same div in JS 我想一种方法来从URL中获取单个锚变量,然后将特定链接内的所有链接附加到 <div> 与锚vars - I would like a way to get individual anchor vars from a url and then apend all links within a specific <div> with the anchor vars 我想在div中移动图像,以便包含div的图像准确显示其大小 - I would like to move images in a div so the containing div displays them exactly their size 我想以“波浪”形式加载图像。 - I would like to load images in a “wave”.. 如何允许多个按钮在具有不同文本的情况下占用 div 中的所有空间? - How can I allow multiple buttons to take up all the space within a div while having different text? 我只想将html5视频替换为div,仅适用于移动/平板电脑用户,而无需让他们开始下载视频 - I would like to replace an html5 video with a div only for mobile/tablet users, WITHOUT having them start the download of the video 将 React JS 中的 URL 的所有图像下载到客户端 - Download all images by their URL in React JS to Client 我想修改一个<tspan>用js</tspan> - I would like to modify a <tspan> with js 我想限制形状中的圆圈和该形状中的分区 - I would like to restrict circles within a shape & partitions within that shape 使用vanilla JS获取div中的所有图像 - Get all images in a div using vanilla JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM