Resize PHP images using nearest neighbor interpolation?

I have a 16x16 sprite in PNG or GIF format and you want to display it on a 64 x 64 website in all its pixel glory. In Firefox 3.6+ and IE, I can do this easily with CSS using image-rendering and -ms-interpolation-mode , but since this does not work in all browsers, I would like to resize the image on the fly using PHP. What is the best way to resize images using nearest neighbor interpolation in PHP?

+5
source share
5 answers

If you want to preserve pixelization during resizing, you will want to do something similar 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 checked 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 simple, since each of the GD gif functions also has corresponding png functions. Hope this helps.

+6
source

You can use the ImageMagick project if you also need to resize the GIF.

You can use GD, but it is possible that you will lose some EXIF ​​data.

KusabaX Project has an excellent function for image conversion. Check the file "/inc/func/posts.php" on line 58 .; -)

+1
source
+1

: . . . , , .

, . , . . . , , , 30 , .

0

Symphony - JIT-, !: P

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

All Articles