Speed up development with full-stack environments for every branch.

Learn More

Image Pixelation [gd-library, PHP and gd]

Forked from How to resize an image using GD library.

31 Runs 79 Views 24 Copies
Saved

Saved

typoworx-de 2

typoworx-de
published a year ago

<?php
//sending image header to proper display the image
header('Content-type: image/jpeg');

// Pixelation-Level
$pixelate_y = 15;
$pixelate_x = 15;

$imageFile = 'images/picture.jpg';
$image = imagecreatefromjpeg($imageFile); 

$size = getimagesize($imageFile);
$height = $size[1];
$width = $size[0];

// start from the top-left pixel and keep looping until we have the desired effect
for($y = 0;$y < $height;$y += $pixelate_y+1)
{
    for($x = 0;$x < $width;$x += $pixelate_x+1)
    {
        // get the color for current pixel
        $rgb = imagecolorsforindex($image, imagecolorat($image, $x, $y));

        // get the closest color from palette
        $color = imagecolorclosest($image, $rgb['red'], $rgb['green'], $rgb['blue']);
        imagefilledrectangle($image, $x, $y, $x+$pixelate_x, $y+$pixelate_y, $color);
    }
}

for($i = 0; $i < 20; $i++)
{
    // Apply Gauss-Blur
    imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
    imagefilter($image, IMG_FILTER_SMOOTH);
}

imagepng($image);
imagedestroy($image);
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).