【问题标题】:How to use imagescale and retain appearance of edge "pixels"如何使用图像缩放并保留边缘“像素”的外观
【发布时间】:2017-06-04 21:21:01
【问题描述】:

所以我有一个使用 imagecreate 的 3x3 像素图像。我想用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");

这是我的输出:

注意右下角的像素是如何被截断的。我一直在玩新尺寸的数字,最终达到了 256x256,此时像素的大小都相同。

这是使用 256x256 后的输出:

我的问题是:如何根据我描述的效果导出尺寸以用于调整大小的图像?

额外问题:是否有另一种方法可以让我调整到任意大小并保持像素大小大致相同?

【问题讨论】:

    标签: php gd image-scaling


    【解决方案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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 2013-09-29
      相关资源
      最近更新 更多