PixelCraft is a lightweight PHP library designed for easy image processing using the GD lib

Nightfly 44e400e214 rotate, setBrush, drawLineBrush, drawArc methods implemented, few new examples 1 week ago
assets 7d09bc3c0c Image filters method implemented 10 months ago
examples 44e400e214 rotate, setBrush, drawLineBrush, drawArc methods implemented, few new examples 1 week ago
src 44e400e214 rotate, setBrush, drawLineBrush, drawArc methods implemented, few new examples 1 week ago
.gitignore 3bbdaea485 few examples 1 year ago
LICENSE 892fbdbec3 Initial commit 1 year ago
README.md 116a4d8aa2 updated doc 2 weeks ago
composer.json 1e59bbbedf tags update 3 weeks ago

README.md

pcraftlogo

PixelCraft

PixelCraft is a lightweight PHP library designed for easy image processing using the GD lib. With PixelCraft, you can perform basic image operations such as resizing, cropping, drawing of watermarks, and format conversion. The library is characterized by its user-friendly interface and minimal code footprint, allowing for quick and efficient image processing in PHP projects.

Requirements

  • PHP >=5.3 (PHP 7.4, 8.2, 8.3, 8.4 also compatible)
  • GD Library
  • Mbstring extension

Few usage examples

Minimal example / drawing from scratch


$pc=new PixelCraft();

$pc->createImage(640, 480);
$pc->fill('yellow');

$pc->addColor('sky', 34, 61, 216);
$pc->drawRectangle(0, 0, 640, 240, 'sky');

$pc->renderImage('png');

drawfromscratch

Image downscale and convertation from PNG to JPEG

$pc=new PixelCraft();

$pc->loadImage('someimage.png');
$pc->scale(0.5);

$pc->saveImage('resizedimage.jpg','jpeg');

downscale

Adding watermarks to images

$pixelCraft = new PixelCraft();

$pixelCraft->loadImage('yourimage.jpg');
$pixelCraft->loadWatermark('watermark.png');

$watermarkX=$pixelCraft->getImageWidth()-150;
$watermarkY=20;

$pixelCraft->drawWatermark(false,$watermarkX,$watermarkY);

$originalFileType=$pixelCraft->getImageType();
$pixelCraft->renderImage($originalFileType);

watermark

Instagram-like filters

$pixelCraft = new PixelCraft();

$pixelCraft->loadImage('yourimage.jpg');

$filtersSet = array();
$filtersSet[] = array(IMG_FILTER_BRIGHTNESS => -15);
$filtersSet[] = array(IMG_FILTER_GRAYSCALE => -5);
$filtersSet[] = array(IMG_FILTER_COLORIZE => array(80, 0, 60));
$filtersSet[] = array(IMG_FILTER_GAUSSIAN_BLUR => '');

$pixelCraft->imageFilters($filtersSet);

$pixelCraft->renderImage('jpeg');

instafilters

Adding text to image

$pixelCraft = new PixelCraft();

$pixelCraft->loadImage('yourimage.jpg');

$labelPosition = ($pixelCraft->getImageHeight()) - 10;
$pixelCraft->drawTextAutoSize($labelPosition, 10, 'Text at image bottom', 'white', 'black');

$pixelCraft->renderImage('jpeg');

textatbottom

Drawing pixel-art

$pixelCraft = new PixelCraft();

$pixelMap = '
1111111111111111111111111
1000000000000000000000001
1000000000001000000000001
1000000000011100000000001
1001000000011100000001001
1001100000011100000011001
1001110000011100000111001
1001111000011100001111001
1001101100011100011011001
1001101100011100011011001
1001100110011100110011001
1001100110011100110011001
1001100110011100110011001
1001100110011100110011001
1001100110011100110011001
1001100110011100110011001
1001100110011100110011001
1001111100010100011111001
1001111000110110001111001
1001101101100011011011001
1001100111110111110011001
1001100011011101100011001
1001100011001001100011001
1001111111111111111111001
1001111111111111111111001
1000000011001001100000001
1000000001101011000000001
1000000000111110000000001
1000000000001000000000001
1000000000000000000000001
1111111111111111111111111
';

$pixelCraft->createImage(25,31);
$pixelCraft->addColor('foreground', 227, 209, 54);
$pixelCraft->addColor('background', 73, 140, 204);

$pixelMap=trim($pixelMap);
$pixelMap=explode(PHP_EOL,$pixelMap);
foreach ($pixelMap as $y=>$xAxis) {
    $xAxis=str_split($xAxis);
    foreach ($xAxis as $x=>$pixel) {
        if ($pixel) {
            $pixelCraft->drawPixel($x,$y,'foreground');
        } else {
            $pixelCraft->drawPixel($x,$y,'background');
        }
    }
}

$pixelCraft->scale(16);


$pixelCraft->renderImage();

pixelart

Random values visualization

$pixelCraft = new PixelCraft();

$width=100;
$height=100;
$pixelCraft->createImage($width,$height);

for ($x=0;$x<$width;$x++) {
    for ($y=0;$y<$height;$y++) {
        $randomR=rand(0,255);
        $randomG=rand(0,255);
        $randomB=rand(0,255);
        $colorName='C_'.$randomR.$randomG.$randomB;
        $pixelCraft->addColor($colorName,$randomR,$randomG,$randomB);
        $pixelCraft->drawPixel($x,$y,$colorName);
    }
}

$pixelCraft->scale(8);
$pixelCraft->renderImage();

drawrandom

Image region crop

$pixelCraft = new PixelCraft();

$pixelCraft->loadImage('../assets/fox.jpg');

$pixelCraft->cropRegion(80, 20, 220, 220);
$pixelCraft->drawString(180,20,$pixelCraft->getImageWidth().'x'.$pixelCraft->getImageHeight(),'black');

//saving original image type
$originalFileType=$pixelCraft->getImageType();

$pixelCraft->renderImage($originalFileType);

regioncrop

Image resize and region crop

$pixelCraft = new PixelCraft();

$pixelCraft->loadImage('../assets/fox.jpg');

$pixelCraft->crop(256, 256);
$pixelCraft->resize(128, 128);

//saving original image type
$originalFileType=$pixelCraft->getImageType();

$pixelCraft->renderImage($originalFileType);

resizeandcrop

Converting an image to ASCII-art based on pixel brightness

$pixelCraft = new PixelCraft();

$charMap = '@#W$9876543210?!abc;:+=-,._       ';
$len = strlen($charMap);

$result = '';
$pixelCraft->loadImage('../assets/horse.png');

$filterSet = array();
$filterSet []= array(IMG_FILTER_NEGATE =>'');
$pixelCraft->imageFilters($filterSet);
$pixelCraft->pixelate(3, true);
$pixelCraft->resize(64, 64);


$colorMap = $pixelCraft->getColorMap(false);

foreach ($colorMap as $x => $ys) {
    foreach ($ys as $y => $color) {
        $brightness = $pixelCraft->rgbToBrightness($color);
        $charIndex = floor(($brightness * $len) / 255);
        $result .= $charMap[$charIndex].' ';
    }
    $result .= PHP_EOL;
}

print($result);

pcimg2ascii

Full PixelCraft class documentation

Installation with composer

The recommended method of installing this library is via Composer

Terminal

composer require pixelcraft/pixelcraft

License

MIT