PHP图像使用最近邻插值调整大小?

发布于 2024-10-09 03:22:32 字数 314 浏览 0 评论 0原文

我有一个 PNG 或 GIF 图像格式的 16x16 精灵,并希望以 64 x 64 的分辨率在网站上显示它的所有像素化荣耀。在 Firefox 3.6+ 和 IE 中,我可以使用 image 通过 CSS 轻松完成此操作-rendering 和 -ms-interpolation-mode,但由于这不适用于所有浏览器,我想使用 PHP 动态调整图像大小。在 PHP 中使用最近邻插值调整图像大小的最佳方法是什么?

I have a 16x16 sprite in PNG or GIF image format, and would like to display it on a website at 64 x 64 in all its pixelated glory. In Firefox 3.6+ and IE I can do this easily with CSS using image-rendering and -ms-interpolation-mode, but as this doesn't work in all browsers I'd like to resize the image on the fly using PHP instead. What's the best way to resize images using nearest-neighbor interpolation in PHP?

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

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

发布评论

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

评论(5

倒带 2024-10-16 03:22:32

如果您想在调整大小期间保持像素化,您将需要使用 GD 库执行类似的操作:

<?php
// create GD image resource from source image file
$src = imagecreatefromgif('test.gif');

// create new GD image resource with indexed color
$dest = imagecreate(64, 64);

// copy/resize image without resampling
imagecopyresized($dest, $src, 0, 0, 0, 0, 64, 64, 16, 16);

// output result
header('Content-type: image/gif');
imagegif($dest);
?>

我已经测试了代码,并且像素化保持不变。您必须调整代码以接受 png 文件作为输入,这应该相当容易,因为每个 GD gif 函数也有相应的 png 函数。希望这有帮助。

If you want to keep the pixelation during your resize, you will want to do something like this using the GD library:

<?php
// create GD image resource from source image file
$src = imagecreatefromgif('test.gif');

// create new GD image resource with indexed color
$dest = imagecreate(64, 64);

// copy/resize image without resampling
imagecopyresized($dest, $src, 0, 0, 0, 0, 64, 64, 16, 16);

// output result
header('Content-type: image/gif');
imagegif($dest);
?>

I have tested the code, and the pixelation remains in tact. You will have to adapt the code to also accept png files as input, which should be fairly easy since each of the GD gif functions also have corresponding png functions. Hope this helps.

阳光①夏 2024-10-16 03:22:32

如果您也需要调整 GIF 的大小,可以使用 ImageMagick 项目。

您可以使用 GD,但是,您可能会丢失一些 EXIF 数据。

KusabaX 项目有一个很棒的图像转换功能。检查文件“/inc/func/posts.php”第 58 行。;-)

You can use the ImageMagick project, if u need resize GIFs too.

You can use GD, but, it's possible that u lose some EXIF data.

The KusabaX Project has a great function to convert image. Check the file "/inc/func/posts.php" at line 58. ;-)

尘曦 2024-10-16 03:22:32

http://www.imagemagick.org/Usage/misc/#interpolate

http://www.francodacosta.com/blog/phmagick/examples/resizing-images

这是有关 imagemagick 中插值的一些信息。
第二个链接是一个名为 phMagick 的类,它是 imagemagick 库的全功能包装器。链接的示例用于基本调整大小操作,但没有为库进行的命令行调用设置插值标志。

确实可以通过该类完全访问命令行,并且如果缺少插值标志支持,则可以使用一种简单的机制通过插件扩展该类。它还支持 php5。

如果这不能为您指明正确的方向,我不知道还有什么可以。如果不出意外,phMagick 类应该为您提供一个良好的起点,为您的特定目的编写更精简的包装器。

http://www.imagemagick.org/Usage/misc/#interpolate

http://www.francodacosta.com/blog/phmagick/examples/resizing-images

This is a bit of information regarding interpolation in imagemagick.
The second link is to a class called phMagick which is a full featured wrapper around the imagemagick libraries. The example linked is for basic resize operations but doesn't have the interpolation flags set for the command line call that the library makes.

You do have full access to the command line through the class and if there is a lack of interpolation flag support there is an easy mechanism for extending the class via plugins. It is also php5 ready.

If this doesn't point you in the right direction I don't know what will. If nothing else the phMagick classes should give you a good starting point for writing a leaner wrapper for your specific purpose.

脱离于你 2024-10-16 03:22:32

答案是,没关系。缓存结果。保存结果的硬拷贝并输出。如果图像永远不会改变且识别信息也不会改变,则仅执行一次。

您可能会说这不是正确的答案。那不是你要求的。但这是最好的答案。随着时间的推移,它将节省最多的处理时间。到了图像调整大小的初始加载时间变得完全没有意义的程度,渲染可能需要 30 秒,而且只会影响一个人一次。

The answer is, it doesn't matter. Cache the result. Save down a hard-copy of the result and output that. Do it only once if the image never changes without identifying info changing too.

You might argue that this isn't the correct answer. That it isn't what you asked for. But it is the BEST answer. It will save the most processing time over time. To the point where the initial load time of the image resize in the first place becomes entirely moot, It could take 30 seconds to render, and it will only affect one person, once.

迷离° 2024-10-16 03:22:32

Symphony 是一个出色的 PHP 驱动框架,它利用 JIT 操纵器,这使得这一切变得小菜一碟! :P

你的情况下的图像是

<img src="http://yoursite.com/image/1/64/64/0/images/16pixels.jpg"/>

Symphony is a great PHP powered framework and it utilizes the JIT manipulator, which makes all of this a piece of cake! :P

An image in your case would be

<img src="http://yoursite.com/image/1/64/64/0/images/16pixels.jpg"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文