PHP:如何将斑点图片分成九部分?

发布于 2024-11-08 23:09:24 字数 176 浏览 3 评论 0原文

我现在有一个真正的难题:我的数据库中保存了一张 blob 图片。现在,我想将其分成 9 个部分(如井字游戏网格),然后我想将其隐藏在右侧。现在我想以随机顺序显示这8个部分,这样你就可以移动它们并解决谜题(移动可以用javascript解决)。

我怎样才能实现 php 功能?是否有现有的功能?

谢谢帮助;)

I got a real hard question now: I have a blob-Picture saved in my database. Now, I want to split it into 9 Parts (like the tic-tac-toe-grid) and then I want to hide the one down right. Now I want to display these 8 parts in a random order, so you can move them and solve the puzzle (the moving could be solved with javascript).

How can I make the php-functionality? Are there existing functions for that?

Thx for help ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽你眉间 2024-11-15 23:09:24

您可以使用 GD 库。有一个函数 imagecopy() http://www .php.net/manual/en/function.imagecopy.php,您可以在其中复制图像的部分内容。例如,如果您有 90x90 像素的图像,则必须使用 0,0,30,30 来调用它,以便您获得第一个正方形。将这些部分保存为单独的图像或直接放入浏览器中,然后您只需要以正确的方式操作它们[此处:解谜器]。

You can utilize GD library. There is a function imagecopy() http://www.php.net/manual/en/function.imagecopy.php within which you may copy the parts of image. For example if you have 90x90 pixels image you'll have to call it with 0,0,30,30 so that you'll have first square. Save that parts as separate images or put directly into browser and then you'll only need to manipulate them in a proper way [here: puzzle solver].

羅雙樹 2024-11-15 23:09:24

好吧,这将是 JavaScript 和 PHP 的组合,能够在 PHP 中分割图像并不难,但是您必须记住,您需要对文件进行一些组织,以便 JavaScript 知道它们应该进入的顺序成为一个完整的拼图。

与其分割图像本身,不如使用数学方法对其进行切片。

        1     2     3 
     -------------------
   1 |     |     |     |
     -------------------
   2 |     |     |     |
     -------------------
   3 |     |     |     |
     -------------------

好的,图像大小为 300 x 300 像素,您需要将宽度和高度除以所需图像数量除以 2,例如:

$ImagesRequired    = 9;
$GlobalImageWidth  = 900;
$GlobalImageHeight = 900;

$EachImageWidth  = $GlobalImageWidth / ($ImagesRequired / 2);
$EachImageHeight = $GlobalImageHeight / ($ImagesRequired / 2);

两个值都应为 66~ 像素,然后您将在图像周围移动并获取块使用裁剪的图像。

for($x=0;$x<=$GlobalImageWidth;$x = $x+=$EachImageWidth)
{
}

查看上图,您会按增量顺序拍摄图像,以便累积以下尺寸。

  • 1.1 = 0 x 66.6
  • 1.2 = 0 x 133.33
  • 1.3 = 0 x 196.66
  • 2.1 = 66.6 x 66.6
  • 2.2 = 66.6 x 133.33
  • 2.3 = 66.6 x 196.66
  • ...

所以现在您已经计算了每个图像应裁剪的尺寸,您将使用GD或 Imagik 库对图像进行切片,创建图像数组,然后将其存储在缓存中。

上面的内容可能不是 100% 正确,因为现在已经晚了,我不是数学或图像处理方面的专家,但希望这能让您开始,也可以看到 Imagik 和图像处理中完成的一些出色工作,请参阅以下链接,它应该有所帮助激励你。

丑陋的马赛克

Ok so this would be a combination of JavaScript and PHP, being able split the images in PHP would not be to hard, but you would have to remember that you need some organization with the files so that the JavaScript knows the order they should go in to be a complete puzzle.

Instead of splitting the image itself, your would be better of taking slices of it using mathematical methods.

        1     2     3 
     -------------------
   1 |     |     |     |
     -------------------
   2 |     |     |     |
     -------------------
   3 |     |     |     |
     -------------------

Ok so an image is 300 x 300 pixels you would need to divide the width and height by the amount of images required dived by 2, for example:

$ImagesRequired    = 9;
$GlobalImageWidth  = 900;
$GlobalImageHeight = 900;

$EachImageWidth  = $GlobalImageWidth / ($ImagesRequired / 2);
$EachImageHeight = $GlobalImageHeight / ($ImagesRequired / 2);

Both of the values should be 66~ pixels, you would then move around the image taking chunks of the image using crop.

for($x=0;$x<=$GlobalImageWidth;$x = $x+=$EachImageWidth)
{
}

Looking at the graph above you would you take images in incremental order so you would accumulative the following dimensions.

  • 1.1 = 0 x 66.6
  • 1.2 = 0 x 133.33
  • 1.3 = 0 x 196.66
  • 2.1 = 66.6 x 66.6
  • 2.2 = 66.6 x 133.33
  • 2.3 = 66.6 x 196.66
  • ...

So now you have calculated the dimensions that each image should be cropped at, you would use GD or Imagik libraries to slice the image, creating an array of images that you can then store in the cache.

The above may not be 100% correct as it's late here and im not an expert in maths or image manipulation but hopefully this will get you started, also to see some great work done in Imagik and image manipulation see the following link, it should help inspire you.

ugly Mosaic

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文