Skip to content
WJunction - Webmaster Forum

Pixelar image with PHP

Status
Not open for further replies.
The next script he does is pixelate an image with PHP. Use the PHP GD library to pixelate the image.

PHP:
<?php
// Tamaño del pixel
    $pixel = 15;
/* Pixelar una imagen */
        $getImagen = 'mona-lisa.jpg';
        //Creamos una variable imagen a partir de la imagen original
#        $imagen = imagecreatefrompng($getImagen); // Descomentar si es PNG
        $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);
    // Fin Pixelar una imagen
?>

source: https://www.marcofbb.com.ar/pixelear-imagenes-con-php/
 
Status
Not open for further replies.

About the author

M
Member · Joined
18
Messages
9
Reactions
3
Points

Tags

php

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom