Converting Images to Black n White and Grayscale in PHP

Aspose.Imaging - Binarization with Fixed Threshold

Following code snippet demonstrates how Fixed Threshold Binarization can be applied to an image using Aspose.Imaging Java for PHP, call binarization_with_fixed_threshold method of ConvertingRasterImages module.

PHP Code

 public static function binarization_with_fixed_threshold($dataDir=null){

$image=new Image();

$image = $image->load($dataDir."test.jpg");


\# Check if image is cached

if (!$image->isCached()) {

\# Cache image if not already cached

$image->cacheData();

}

\# Binarize image with predefined fixed threshold

$image->binarizeFixed(100);

\# Save the image to disk

$image->save($dataDir . "binarization_with_fixed_threshold.jpg");

\# Display Status.

print "Binarization image with Fixed Threshold successfully!".PHP_EOL;

}

Aspose.Imaging - Binarization with Otsu Threshold

Following code snippet demonstrates how Otsu Threshold Binarization can be applied to an image using Aspose.Imaging Java for PHP, call binarization_with_otsu_threshold method of ConvertingRasterImages module.

PHP Code

 public static function binarization_with_otsu_threshold($dataDir=null)

{

\# Load an existing image

$image = new Image();

$image = $image->load($dataDir."test.jpg");

\# Check if image is cached

if (!$image->isCached()) {

\# Cache image if not already cached

$image->cacheData();

}

\# Binarize image with Otsu Thresholding

$image->binarizeOtsu();

\# Save the image to disk

$image->save($dataDir."binarization_with_otsu_threshold.jpg");

\# Display Status.

print "Binarization image with Otsu Threshold successfully!".PHP_EOL;

}

Aspose.Imaging - Transform image to its grayscale representation

Following code snippet demonstrates how to apply grayscale to an image using Aspose.Imaging Java for PHP, call transform_image_to_grayscale method of ConvertingRasterImages module.

PHP Code

 public static function transform_image_to_grayscale($dataDir=null){

\# Load an existing image

$image=new Image();

$image = $image->load($dataDir."test.jpg");

\# Check if image is cached

if (!$image->isCached()) {

\# Cache image if not already cached

$image->cacheData();

}

\# Transform image to its grayscale representation

$image->grayscale();

\# Save the image to disk

$image->save($dataDir."transform_image_to_grayscale.jpg");

\# Display Status.

print "Transform image to its grayscale representation successfully!".PHP_EOL;

}

Download Running Code

Download Converting Images to Black n White and Grayscale (Aspose.Imaging) from any of the below mentioned social coding sites: