简体   繁体   English

是否可以对网站的特定部分进行图片制作?

[英]Is it possible to make a picture of a specific part of your website?

My apologies if this is a stupid question, but is there a way to 'take a picture' from a certain part of the website and save it on the computer? 如果这是一个愚蠢的问题,我表示歉意,但是是否有办法从网站的某个部分“拍照”并将其保存在计算机上?

HTML HTML

<div id="red" color="red" width="100px" height="100px"></div>
<div id="green" color="green" width="100px" height="100px"></div>
<div id="blue" color="blue" width="100px" height="100px"></div>

For example I have a webpage with 3 divs next to each other and I want to take a picture of only the green div, and save it as a jpeg or something. 例如,我有一个3 div相邻的网页,我只想拍一张绿色div的照片,并将其另存为jpeg或其他内容。

Is this at all possible with JQuery/Javascript for example? 例如,使用JQuery / Javascript完全可以吗?

As grigno said, Phantom.js would be a good choice since it has screen capture capabilities. 正如grigno所说, Phantom.js具有屏幕捕获功能,将是一个不错的选择。

Here's an example of how you can take pictures of a specific element using getBoundingClientRect . 这是一个示例,说明如何使用getBoundingClientRect拍摄特定元素的图片。

JavaScript JavaScript的

var page = require('webpage').create();

address = 'https://stackoverflow.com/q/18466303/2129835';
page.open(address, function (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
    } else {
        window.setTimeout(function () {
            page.clipRect = page.evaluate(function() {
                return document.querySelector('.question .post-text').getBoundingClientRect(); 
            });
            page.render('output.png');
            phantom.exit();
        }, 200);
    }
});

Result: output.png 结果:output.png

在此处输入图片说明

Not sure how this plays out, but you may be able to take advantage of the <canvas> element and draw the <div> there . 不确定如何播放,但是您可以利用<canvas>元素并在其中绘制<div> Then canvas has export options to convert to jpg, png, etc. 然后画布具有导出选项以转换为jpg,png等。

It is not easily done, but there are some projects which can help you out. 这并非易事,但有些项目可以为您提供帮助。 Have a look at: 看一下:

Basically, they use <canvas> to draw each DOM element on it, and then save it as bitmap. 基本上,他们使用<canvas>在其上绘制每个DOM元素,然后将其另存为位图。 Due to this mechanics, the output image may not be 100% accurate though. 由于这种机制,输出图像可能不是100%准确。

If you opt to use html2canvas, it accepts a list of DOM elements to render. 如果您选择使用html2canvas,它将接受要呈现的DOM元素列表。 Have a look at this example for usage. 看一下此示例的用法。

Other approach would be to create Chrome extension - it's easy then - if that's feasible in your use case. 如果在您的用例中可行的话,另一种方法是创建Chrome扩展程序-这很容易。 If so, have a look at chrome.tabs.captureVisibleTab() . 如果是这样,请查看chrome.tabs.captureVisibleTab()

I used phantomjs with screen capture to take a picture of a site. 我使用phantomjs屏幕截图为网站拍照。 After saving the image on the web server you can cut the image file to get a specific area of your page. 将图像保存在Web服务器上后,您可以剪切图像文件以获取页面的特定区域。

暂无
暂无

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

相关问题 Html / CSS制作网站的实时图片 - Html/CSS make a live picture of a website 如何让html/css/js中的视差效果只影响网站的特定部分 - How to make only a specific part of a website be affected by the parallax effect in html/css/js 在 Discord 中,是否可以通过控制台更改您的个人资料图片? - In Discord, is it possible to change your profile picture via the console? 如何让用户上传的图片在我的 PHP 网站上自动调整到特定的宽度和高度而不会破坏尺寸? - How can I make a picture a user uploaded to automatically adjust to a specific width and height on my PHP website without spoiling the dimentions? 是否可以使 getElementById 成为 function 的一部分? - Is it possible to make getElementById part of a function? 您如何在 Javascript 中链接到您网站的不同部分 - How do you link to a different part of your website in Javascript Automator - 网站某些部分的屏幕截图。 可能吗? - Automator - a screenshot of a some part of a website. Is it possible? 可以匹配并返回一部分文本的正则表达式,同时删除 rest 以使性别特定文本成为可能 - Regex that can match and return a part of text while remove the rest to make gender specific text possible 上传页面的一部分或页面中的一张图片-第一张(尽快) - Upload part of a page or one picture from the page - the first (as soon as possible) 图片的特定部分可以用作使用jQuery或Javascript的链接吗? - Can a specific part of a picture be used as a link using jQuery or Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM