chillerlan/php-imagetiler

An image tile generator. PHP 7.2+

2.0.0 2019-01-30 13:08 UTC

README

A script for PHP 7.2+ to cut images (maps) into pieces (tiles). Based on the map tiler script by Fedik. This script will keep the proportions of the input image and generate only necessary tiles - no need for square input files!

Packagist version License Travis CI CodeCov Scrunitizer CI Packagist downloads PayPal donate

Documentation

Requirements

Installation

requires composer

composer.json (note: replace dev-master with a version boundary)

{
	"require": {
		"php": ">=7.2.0",
		"ext-imagick": "*",
		"chillerlan/php-imagetiler": "dev-master"
	}
}

Manual installation

Download the desired version of the package from master or release and extract the contents to your project folder. After that:

  • run composer install to install the required dependencies and generate /vendor/autoload.php.
  • if you use a custom autoloader, point the namespace chillerlan\Imagetiler to the folder src of the package

Profit!

Usage

Use the example for live testing.

// invoke an options instance
$options = new ImagetilerOptions([
	'zoom_min'             => 0,
	'zoom_max'             => 8,
	'zoom_normalize'       => 6,
	'fill_color'           => 'transparent',
	'fast_resize'          => true,
	'optimize_output'      => true,
	// ... whatever you need
]);

// see https://github.com/psliwa/image-optimizer#configuration
$optimizer = (new OptimizerFactory([]))->get();

// invoke and run the tiler
$tiler  = new Imagetiler($options, $optimizer);
$tiler->process('/path/to/image.png', '/path/to/output/');

That's it!

API

Imagetiler public methods

method return description
__construct(ContainerInterface $options = null, LoggerInterface $logger = null) - see SettingsContainerInterface and LoggerInterface. Invokes an empty ImagetilerOptions object and a Psr\NullLogger if the respective parameters aren't set.
setOptions(ContainerInterface $options) Imagetiler set options on-the-fly, called internally by the constructor
setOptimizer(Optimizer $optimizer) Imagetiler set an optimizer instance on-the-fly, called internally by the constructor
process(string $image_path, string $out_path) Imagetiler processes the given image from $image_path and dumps the output to $out_path

ImagetilerOptions properties

property type default allowed description
$tile_size int 256 positive int width/height of a single tile
$zoom_min int 0 positive int minimum zoom level
$zoom_max int 8 positive int maximum zoom level
$zoom_normalize int null positive int this zoom level represents the size of the original image. zoom levels higher than this will be upscaled, which may take some time and resources depending on the size of the input image.
$tms bool false * if set to true - the origin will be set to bottom left, +y upwards, according to Tile Map Service Specification, otherwise the origin is on the top left, +y downwards, like described by the Google Maps specification
$fill_color string '#000000' * the fill color for leftover space, can be transparent for png
$memory_limit string '-1' * see php.ini settings
$store_structure string '%1$d/%2$d/%3$d' * storage structure - can be anything. %1$d = zoom, %2$d = x, %3$d = y. see sprintf()
$fast_resize bool false * determines whether to use fast Imagick::scaleImage() (true) or slow Imagick::resizeImage() (false)
$resize_filter int Imagick::FILTER_ROBIDOUXSHARP Imagick::FILTER_* see Imagick::resizeImage() and Imagick filter constants
$resize_blur float 1.0 positive float see Imagick::resizeImage()
$tile_format string 'png' png, jpg see Imagick formats
$tile_ext string null * tile image extension - autodetected from format if none given.
$quality_jpeg int 80 0-100 quality of the saved image in jpeg format
$imagick_tmp string null * ImageMagick tmp folder
$overwrite_base_image bool false *
$overwrite_tile_image bool false *
$clean_up bool true * whether or not to delete temp images
$optimize_output bool false * enable image optimization (requires Optimizer instance)