Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php gd像素太过锐利_Php_Gd_Pixel - Fatal编程技术网

php gd像素太过锐利

php gd像素太过锐利,php,gd,pixel,Php,Gd,Pixel,我有一个用于将图像像素化的脚本该脚本正在运行,但我希望边缘更平滑: $imgfile = 'batman.jpg'; $image = ImageCreateFromJPEG($imgfile); $imagex = imagesx($image); $imagey = imagesy($image); $pixelate_amount = 10; $tmpImage = ImageCreateTrueColor($imagex, $imagey); imagecopyresized($tmpI

我有一个用于将图像像素化的脚本该脚本正在运行,但我希望边缘更平滑:

$imgfile = 'batman.jpg';
$image = ImageCreateFromJPEG($imgfile);
$imagex = imagesx($image);
$imagey = imagesy($image);
$pixelate_amount = 10;
$tmpImage = ImageCreateTrueColor($imagex, $imagey);
imagecopyresized($tmpImage, $image, 0, 0, 0, 0, round($imagex / $pixelate_amount), round($imagey / $pixelate_amount), $imagex, $imagey);
$pixelated = ImageCreateTrueColor($imagex, $imagey);
imagecopyresized($pixelated, $tmpImage, 0, 0, 0, 0, $imagex, $imagey, round($imagex / $pixelate_amount), round($imagey / $pixelate_amount));
header("Content-Type: image/jpeg");
imageJPEG($pixelated, "", 100);
我有:

这将产生:


有什么遗漏吗?

使用
imagecopyresampled()
而不是
imagecopyresized()

以下是您需要的内容(我目前使用的脚本)。此脚本基于以下位置的脚本:

函数convertToPixel($im,$size){
$size=(int)$size;
$sizeX=imagesx($im);
$sizeY=imagesy($im);
如果($sizeX<3&&$sizeX<3){//或者您可以选择任何您想要的尺寸
返回;
}
对于($i=0;$i<$sizeX;$i+=$size){
对于($j=0;$j<$sizeY;$j+=$size){
$colors=Array('alpha'=>0,'red'=>0,'green'=>0,'blue'=>0,'total'=>0);
对于($k=0;$k<$size;++$k){
对于($l=0;$l<$size;++$l){
如果($i+$k>=$sizeX | |$j+$l>=$sizeY){
继续;
}
$color=imagecolorat($im、$i+$k、$j+$l);
imagecolordeallocate($im,$color);
$colors['alpha']+=($color>>24)&0xFF;
$colors['red']+=($color>>16)和0xFF;
$colors['green']+=($color>>8)&0xFF;
$colors['blue']+=$color&0xFF;
++$colors['total'];
}
}
$color=imagecolorallocatealpha($im,$colors['red']/$colors['total'],$colors['green']/$colors['total'],$colors['blue']/$colors['total'],$colors['alpha']/$colors['total']);
imagefilledrectangle($im、$i、$j、($i+$size-1)、($j+$size-1)、$color);
}
}
}
标题('Content-type:image/jpg');
$im=imagecreatefromjpeg($imgfile);
convertToPixel(15英镑);
图像JPEG($im,,,100);
这将产生:

您还可以更改在
convertToPixel
中传递的值以修改像素大小。 )

function convertToPixel($im, $size) {
  $size = (int)$size;
  $sizeX = imagesx($im);
  $sizeY = imagesy($im);

  if($sizeX < 3 && $sizeX < 3) { // or you can choose any size you want
    return;
  }
  for($i = 0;$i < $sizeX; $i += $size) {
    for($j = 0;$j < $sizeY; $j += $size) {
      $colors = Array('alpha' => 0, 'red' => 0, 'green' => 0, 'blue' => 0, 'total' => 0);
      for($k = 0; $k < $size; ++$k) {
        for($l = 0; $l < $size; ++$l) {
          if($i + $k >= $sizeX || $j + $l >= $sizeY) {
            continue;
          }
          $color = imagecolorat($im, $i + $k, $j + $l);
          imagecolordeallocate($im, $color);
          $colors['alpha'] += ($color >> 24) & 0xFF;
          $colors['red'] += ($color >> 16) & 0xFF;
          $colors['green'] += ($color >> 8) & 0xFF;
          $colors['blue'] += $color & 0xFF;
          ++$colors['total'];
        }
      }
      $color = imagecolorallocatealpha($im,  $colors['red'] / $colors['total'],  $colors['green'] / $colors['total'],  $colors['blue'] / $colors['total'],  $colors['alpha'] / $colors['total']);
      imagefilledrectangle($im, $i, $j, ($i + $size - 1), ($j + $size - 1), $color);
    }
  }
}
header('Content-type: image/jpg');
$im = imagecreatefromjpeg($imgfile);
convertToPixel($im, 15);
imagejpeg($im, '', 100);