2017-01-20 86 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");