如何在PHP中绘制交互式图像?
我知道如何在 PHP 中(使用 GD)绘制矩形,但是我们如何使它们具有交互性?我的意思是,有没有办法在用户点击的时间和地点收到通知?
最终,我想让用户通过单击它来选择一组矩形,然后单击其他任何地方来移动它。
预先感谢。
问候,
神秘先生。
I know how to draw rectangles in PHP (with GD), but how do we make them interactive ? I mean, is there a way to be notified when and where the user clicks ?
Eventually, I would like to enable the user to select a rectangle among a set by clicking on it, and clicking anywhere else to move it.
Thanks aforehand.
Regards,
Mister Mystère.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
GD 中的矩形是如下例所示的图像:
要使其具有交互性,您可以使用 CSS,例如将图像作为锚标记的背景:
然后在 CSS 中(假设有 2 个图像):
您可以使用 CSS 分配更多内容或采取查看 http://jquery.com/ 使用 JavaScript
A rectangle in GD is an image like the following example:
To make it interactive you can use CSS like placing the image as a background for a anchor tag:
then in CSS (assuming 2 images):
You can do allot more with CSS or take a look at http://jquery.com/ to use JavaScript
您可以使用图像映射来完成此操作。无论您如何创建图像,这都适用。基本语法(来自维基百科)是:
您可以拥有任意数量的
area
元素。您可以使用如上所示的常规 href URL,也可以使用 JavaScript 事件。支持所有典型的鼠标事件(onclick、onmouseover、onmouseout、onmousedown、onmouseup 等)。如果您只有简单的矩形,则不使用地图的替代方法是仅连续放置多个图像,没有边距或填充。
You can do this with image maps. This applies however you create the image. The basic syntax (from Wikipedia) is:
You can have any number of
area
elements. You can either use a regular href URL as shown above, or use JavaScript events. All the typical mouse events are supported (onclick, onmouseover, onmouseout, onmousedown, onmouseup, etc.)If you only have simple rectangles, an alternative without maps is to just position several images contiguously with no margin or padding.
对于您所描述的内容,您可能会考虑使用 HTML5 画布功能。也就是说,不是在 PHP 中使用 GD 创建静态图像,而是在浏览器中使用 Javascript 动态创建图像。然后,您可以提供全方位的交互性并对鼠标移动、点击做出反应。
看一下这个绘画应用程序的示例:
http://mugtug.com/sketchpad/
不过,这将要求您的用户拥有支持的浏览器画布(firefox、chrome),或者对于 IE,使用“explorercanvas”之类的东西。我相信 IE9 推出时将支持画布,这样应该可以解决这个问题。
For what you are describing, you might consider instead using the HTML5 canvas capabilities. That is, instead of creating a static image using GD in PHP, create the image on the fly using Javascript in the browser. Then you can offer the full range of interactivity and react to mouse movement, clicks.
Take a look at this painting application for an example:
http://mugtug.com/sketchpad/
This will, though, require your users have a browser that supports Canvas (firefox, chrome), or for IE, to use something like "explorercanvas". IE9 when it comes out will support canvas I believe, so that should remove that problem.
输入 type="image" 将为您提供在图像上单击的坐标,但这就是“普通”HTML 和 HTML 中的内容。服务器端 PHP。
HTML:
PHP:
要获得更多交互,您必须查看 javascript,为了获得更多可能性,我衷心推荐 Raphael 作为一个跨浏览器(IE 为 VML,其余为 SVG)工具,能够处理更复杂的图像和图像。该图像(部分)上的事件。
An input type="image" will give you the coordinates clicked on the image, but that's about it in 'plain' HTML & server-sided PHP.
HTML:
PHP:
For more interaction, you'll have to look at javascript, and for even more possibilities I heartily recommend Raphael as a cross-browser (VML for I.E, SVG for the rest) tool capable of more complex images & events on (parts of) that image.