简体   繁体   English

我可以在基于PHP的网站上使用npm安装Fabric.js吗?

[英]Can I install Fabric.js using npm for a PHP-based site?

I am currently working on a project with two other people. 我目前正在与另外两个人进行一个项目。 We need to create a small site that allows you to upload a photo and then enter custom text on top of it as an overlay. 我们需要创建一个小型网站,使您可以上传照片,然后在其顶部输入自定义文本作为覆盖。

According to my research, Fabric.js does exactly what we're looking for. 根据我的研究, Fabric.js完全可以满足我们的需求。 However, the main site and certain features (such as photo upload) have already been built in PHP because that is one of the primary skill sets of another person, and I can only find Node.js methods (npm or bower) of installing Fabric.js. 但是,主要站点和某些功能(例如照片上传)已经用PHP构建,因为这是另一个人的主要技能之一,我只能找到安装Fabric的Node.js方法(npm或bower)。 .js文件。 I'm already familiar with Node.js and have npm installed, but is it compatible with PHP if I create separate .js files and link to them in index.php ? 我已经熟悉Node.js并安装了npm,但是如果我创建单独的.js文件并在index.php链接到它们,它是否与PHP兼容? If not, what are my alternatives (in JavaScript or jQuery preferably, but i'm open to other options)? 如果没有,我有什么选择(最好是JavaScript或jQuery,但我愿意接受其他选择)?

So, you won't be able to directly call an npm module from PHP. 因此,您将无法直接从PHP调用npm模块。 That said, you can always run a small node program from PHP as a simple shell command, but I wouldn't recommend that. 也就是说,您始终可以从PHP作为一个简单的Shell命令运行一个小的节点程序,但是我不建议这样做。

I would recommend including fabric.js on the client side and not using node.js at all. 我建议在客户端包括fabric.js,而根本不使用node.js。 Simply let the user overlay text on it in his or her browser, then upload the image. 只需让用户在他或她的浏览器中在上面覆盖文本,然后上传图像即可。

PHP only runs on the server, so you can use it to serve any browser side js code you need to get the job done, but it won't interpret javascript code for you an execute it. PHP仅在服务器上运行,因此您可以使用它来提供完成工作所需的任何浏览器端js代码,但不会为您执行它解释javascript代码。

How to Run FabricJS 如何运行FabricJS

FabricJS can be run in two methods - client side or server side. FabricJS可以通过两种方法运行-客户端或服务器端。

On the client side it is a simple Javscript file you need to load into a webpage. 在客户端,这是一个简单的Javscript文件,您需要将其加载到网页中。 This way you can create a GUI which a user can manipulate the images and save the image as you'd like. 通过这种方式,您可以创建一个GUI,供用户操纵图像并根据需要保存图像。 Thats what the examples on the FabricJS website are doing. 这就是FabricJS网站上的示例所执行的操作。

On the client side, FabricJS needs to run on a NodeJS server. 在客户端,FabricJS需要在NodeJS服务器上运行。 You can find some basic instructions on the FabricJS website for how to run it within NodeJS. 您可以在FabricJS网站上找到一些基本说明,以了解如何在NodeJS中运行它。

Pros / Cons 优点缺点

There are pros / cons for each method. 每种方法都有优点/缺点。

A client side setup in my mind is easier to program in (though I am a web developer by trade) and allows you to build a GUI which creates an easy interface for your users to use. 在我看来,客户端设置更容易编程(尽管我是一名从事贸易的Web开发人员),并允许您构建一个GUI,该GUI为用户提供了易于使用的界面。 However, there are limitations that the browser and clients computer can run into. 但是,浏览器和客户端计算机可能会遇到一些限制。 For example, if you are rendering high resolution images it can have some limitations, particularly if you want to send the image to the server. 例如,如果要渲染高分辨率图像,则可能会有一些限制,特别是如果要将图像发送到服务器。 Mobile devices have issues with higher resolution images. 移动设备在高分辨率图像方面存在问题。

A server side setup takes away most of the limitations, but takes away any GUI option. 服务器端设置消除了大多数限制,但消除了任何GUI选项。 But it's really not that bad to work in. The biggest thing to remember that threw me off at first, is if you make any changes to your NodeJS code you need to restart your NodeJS server before they take effect. 但这确实不是一件容易的事。首先要记住的最重要的事情是,如果您对NodeJS代码进行了任何更改,则需要在其生效之前重新启动NodeJS服务器。 But if you're familiar with NodeJS that's nothing new. 但是,如果您熟悉NodeJS,这并不是什么新鲜事。

Server Setup 服务器设置

With that background said, your question is how can you run FabricJS on a PHP server. 说了这么多背景,您的问题是如何在PHP服务器上运行FabricJS。 As stated just above, FabricJS needs NodeJS to run. 如上所述,FabricJS需要NodeJS才能运行。 You could simply install NodeJS on your PHP server. 您可以简单地在PHP服务器上安装NodeJS。 Under this configuration, you now will have two webservers running at the same domain name/IP address. 在此配置下,您现在将有两个运行在相同域名/ IP地址的Web服务器。 For that reason, one of the two will need a different port. 因此,两者之一将需要不同的端口。 Example: 例:

http://www.myPHPserver.com (Apache/Nginx on port 80) http://www.myPHPserver.com (端口80上的Apache / Nginx)

http://www.myPHPserver.com:8080 (NodeJS on port 8080 or any open port) http://www.myPHPserver.com:8080 (端口8080或任何打开的端口上的NodeJS)

Alternatively, you could choose to install NodeJS with FabricJS on a different server and avoid the port conflict. 或者,您可以选择在另一台服务器上将NodeJS和FabricJS一起安装,并避免端口冲突。 Example: 例:

http://www.myPHPserver.com (Apache/Nginx on port 80) http://www.myPHPserver.com (端口80上的Apache / Nginx)

http://www.myNodeJSserver.com (NodeJS on port 80) http://www.myNodeJSserver.com (端口80上的NodeJS)

Cross Origin Scripting 跨源脚本

If you are using a webserver to work with your FabricJS script, you will run into cross-origin scripting issues that you'll need to handle. 如果您使用网络服务器来处理FabricJS脚本,则将遇到需要处理的跨域脚本问题。 This will happen under both server configuration examples provided above because the port or domain name results in a cross-origin setup. 在上面提供的两个服务器配置示例中都会发生这种情况,因为端口或域名会导致跨域设置。 But this can be overcome by doing the following: 但这可以通过执行以下操作来克服:

  • Server 服务器
    • Set the access-control-allow-origin header on images. 在图像上设置access-control-allow-origin头。
  • Browser/client 浏览器/客户端
    • Set the crossOrigin attribute of the image to Anonymous 将图像的crossOrigin属性设置为Anonymous

You can read more about how to do this from this article - Fabric JS Tainted Canvas – Cross-Origin Images . 您可以从这篇文章-Fabric JS Tainted Canvas –跨源图像中阅读更多有关如何执行此操作的信息。

Another method for working with cross-origin images is to use a server proxy as described here . 处理跨域图像的另一种方法是使用服务器代理,如此处所述

Installing FabricJS on NodeJS 在NodeJS上安装FabricJS

If you need help installing FabricJS on NodeJS, here are some installation guides. 如果您需要在NodeJS上安装FabricJS的帮助,请参阅以下安装指南。

Install FabricJS on Debian Squeeze 6.0.10 在Debian Squeeze 6.0.10上安装FabricJS

Install FabricJS on Ubuntu 12.04 precise 在Ubuntu 12.04 Precision上安装FabricJS

Run PHP Script from NodeJS 从NodeJS运行PHP脚本

It doesn't sound like this is needed based on your question, but I figured I'd mention it at least. 根据您的问题,这听起来好像不是必需的,但我认为至少我已经提到了。 If you need to call a PHP script from a NodeJS server it can be done with an exec command. 如果您需要从NodeJS服务器调用PHP脚本,可以使用exec命令来完成。

NodeJS Server NodeJS服务器

var runner = require("child_process");

var phpScriptPath = "path/to/your/php/script.php";
var argsString = "value1,value2,value3";
runner.exec("php " + phpScriptPath + " " +argsString, function(err, phpResponse, stderr) {
if(err) console.log(err); /* log error */
  console.log( phpResponse );
});

PHP Script PHP脚本

<?php
$params = explode(",", $argv[1]);
echo $params[0] ." - " . $params[1] . " - " . $params[2];
?>

Call PHP script from NodeJS 从NodeJS调用PHP脚本

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM