2017-01-20 84 views
1

所以我有一个3×3像素图像使用imagecreate。我想用imagescale放大图像,同时保持“像素”的3x3网格的外观。但是,右侧和底部边缘的像素大小不一样。如何使用图像和保留边缘“像素”的外观

这里是我的代码,并输出图像:

<?php 

$image = imagecreate(3, 3); 
imagecolorallocate($image, 0, 0, 255); 
$red = imagecolorallocate($image, 255, 0, 0); 
imagesetpixel($image, 0, 0, $red); 
imagesetpixel($image, 1, 1, $red); 
imagesetpixel($image, 2, 2, $red); 

imagepng(imagescale($image, 200, 200, IMG_NEAREST_NEIGHBOUR)); 

header("Content-Type: image/png"); 

这是我的输出:

enter image description here

注意右下角的像素是如何切断。我一直在玩新的尺寸的数字,并达到了256x256,在这一点上的像素都是相同的大小。

这是一个使用256×256后的输出:

enter image description here

我的问题是:我如何可以导出用来与我描述的影响调整后的图像尺寸是多少?

奖金问题:是一种替代方法,可以让我调整大小为任意大小并保持像素大小相同?

回答

1

我会使用imagecopyresampled来实现这一点。

http://php.net/manual/en/function.imagecopyresampled.php

<?php 
    $width = 3; 
    $height = 3; 
    $image = imagecreate($width, $height); 
    imagecolorallocate($image, 0, 0, 255); 
    $red = imagecolorallocate($image, 255, 0, 0); 
    imagesetpixel($image, 0, 0, $red); 
    imagesetpixel($image, 1, 1, $red); 
    imagesetpixel($image, 2, 2, $red); 

    $new_width = 200; 
    $new_height = 200; 
    $dst = imagecreatetruecolor($new_width, $new_height); 
    imagecopyresampled($dst, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
    imagepng($dst); 

    header("Content-Type: image/png");