PHP code example of programster / image-lib

1. Go to this page and download the library: Download programster/image-lib library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

programster / image-lib example snippets




# Include the ImageLib library
ageFilepath = __DIR__ . '/input.png';
$image = \Programster\ImageLib\Image::createFromFilepath($imageFilepath);



# Crop an image
$rectangle = new Programster\ImageLib\Rectangle(100, 100, 400, 200);    
$alteredImage = Programster\ImageLib\ImageLib::crop($image, $rectangle);

# Crop a 400 x 300 image from the center of the image.
$dimensions = new Programster\ImageLib\Dimensions(400, 300);
$alteredImage = Programster\ImageLib\ImageLib::centerCrop($image, $dimensions);

# Crop an image to get it to a certain aspect ratio, not caring about resolution/size
$aspectRatio = new \Programster\ImageLib\Dimensions(4, 3);
$alteredImage = Programster\ImageLib\ImageLib::cropToAspectRatio($image, $aspectRatio);

# Create a 400 x 300 thumbnail of an image.
# This will shrink and crop as necessary in order to preserve as much of what
# the image shows as possible but will perform cropping instead of stretching
# in order to achieve the thumbnails aspect ratio.
$dimensions = new Programster\ImageLib\Dimensions(400, 300);
$alteredImage = Programster\ImageLib\ImageLib::thumbnail($image, $dimensions);

# Scale an image so that its size is as close to the number of pixels as possible
# This is good if you are taking user uploads and targeting a certain file size.
# E.g. you don't need 4k photos on a dataing app.
# However, this may end up expanding the image if the input is smaller.
$desiredNumPixels = 2000000; // two megapixel
$alteredImage = Programster\ImageLib\ImageLib::scaleToNumPixels($image, $desiredNumPixels);

# Change an image to have the specified dimensions. This will stretch/distort
# the image if necessary to achieve the aspect ratio
$dimensions = new Programster\ImageLib\Dimensions(200, 400);
$alteredImage = Programster\ImageLib\ImageLib::setDimensions($image, $dimensions);

# Shrink the image so that it would fit into a box specified by the dimensions.
# This will preserve aspect ratio and will not distort the image, so the
# resulting image may not fill the dimensions, unlike thumbnail()
$dimensions = new Programster\ImageLib\Dimensions(200, 400);
$alteredImage = Programster\ImageLib\ImageLib::shrinkToFit($image, $dimensions);


# Draw a red rectangle
$red = new Programster\ImageLib\Color(255, 0, 0);
$rectangle = new Programster\ImageLib\Rectangle(100, 100, 400, 200);    

$alteredImage = Programster\ImageLib\ImageLib::drawRectangle(
    $image,
    $rectangle,
    $red,
    $thickness=5
);


# After performing your actions, don't forget to save the edited copy to a file!
$alteredImage->save(__DIR__ . '/output.png');