如何在php中创建像素图像

如何在php中创建像素图像,php,image,Php,Image,我试图做的是对一个url的图像进行像素化,在上面添加另一个图像,最后保存该图像 我已经获得了这个网络代码来对图像进行像素化,但我认为它不能正常工作。它不会向我显示像素化图像,也不会保存它。只剩下黑屏了 $pixel = 15; $getImagen = 'https://ep00.epimg.net/elpais/imagenes/2017/06/05/album/1496652756_562670_1496654035_album_normal.jpg'; $imagen = image

我试图做的是对一个url的图像进行像素化,在上面添加另一个图像,最后保存该图像

我已经获得了这个网络代码来对图像进行像素化,但我认为它不能正常工作。它不会向我显示像素化图像,也不会保存它。只剩下黑屏了

$pixel = 15;

$getImagen = 'https://ep00.epimg.net/elpais/imagenes/2017/06/05/album/1496652756_562670_1496654035_album_normal.jpg';


$imagen = imagecreatefromjpeg($getImagen); 
if(!$imagen) exit('ERROR');
list($ancho,$alto)=getimagesize($getImagen);
$superficieTotal = $ancho*$alto;    
//
$superficieRecorrida = 0;
$auxX=0;
$auxY=0;
while($superficieRecorrida <= $superficieTotal){
    $posX=0;$posY=0;$data = array();
    while($posX <= $pixel and (($auxX + $posX) < $ancho)){
        $posY=0;
        while($posY <= $pixel and (($auxY + $posY) < $alto)){
            $rgb = imagecolorat($imagen, ($auxX + $posX), ($auxY + $posY));
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            $data[] = array($r,$g,$b);
            $posY++;
        }
        $posX++;
    }

    // Busco promedio
    $r = 0; $g = 0; $b = 0;
    foreach($data as $d){
        $r+= $d[0];
        $g+= $d[1];
        $b+= $d[2];
    }
    $totalArray = count($data);
    if($totalArray == 0) $totalArray = 1;
    $r = $r/$totalArray;
    $g = $g/$totalArray;
    $b = $b/$totalArray;
    $colorPromedio = imagecolorallocate($imagen, $r, $g, $b);
    imagefilledrectangle($imagen, $auxX, $auxY, ($auxX + $pixel), ($auxY + $pixel), $colorPromedio);
    //
    $auxX+= $pixel;
    if($auxX >= $ancho){
        $auxX = 0;
        $auxY+= ($pixel+1);
    }       
    $superficieRecorrida+= $pixel*$pixel;

}
//
Header("Content-type: image/jpeg");
imagejpeg($imagen);
imagedestroy($imagen);
$pixel=15;
$getImagen=https://ep00.epimg.net/elpais/imagenes/2017/06/05/album/1496652756_562670_1496654035_album_normal.jpg';
$imagen=imagecreatefromjpeg($getImagen);
如果(!$imagen)退出('ERROR');
列表($ancho,$alto)=getimagesize($getImagen);
$supericietotal=$ancho*$alto;
//
$superfierecorrida=0;
$auxX=0;
$auxY=0;
而($superfierecorrida>8)&0xFF;
$b=$rgb&0xFF;
$data[]=数组($r、$g、$b);
$posY++;
}
$posX++;
}
//布斯科协议
$r=0$g=0$b=0;
foreach($d数据){
$r+=$d[0];
$g+=$d[1];
$b+=$d[2];
}
$totalArray=count($data);
如果($totalArray==0)$totalArray=1;
$r=$r/$totalArray;
$g=$g/$totalArray;
$b=$b/$totalArray;
$colorPromedio=IMAGECOLORDALLOCATE($imagen、$r、$g、$b);
imagefilledrectangle($imagen、$auxX、$auxY、($auxX+$pixel)、($auxY+$pixel)、$colorPromedio);
//
$auxX+=$pixel;
如果($auxX>=$ancho){
$auxX=0;
$auxY+=($pixel+1);
}       
$superfierecorrida+=$pixel*$pixel;
}
//
标题(“内容类型:图像/jpeg”);
图像jpeg($imagen);
图像销毁($imagen);
谢谢你以后的回答。问候