include the Zebra_Image library
require 'path/to/Zebra_Image.php';
instantiate the class
$img = new Zebra_Image();
a source image
$img->source_path = 'path/to/source.ext';
path to where should the resulting image be saved
note that by simply setting a different extension to the file will
instruct the script to create an image of that particular type
$img->target_path = 'path/to/target.ext';
crop a rectangle of 100x100 pixels, starting from the top-left corner
$img->crop(0, 0, 100, 100);
public crop ( integer $start_x, integer $start_y, integer $end_x, integer $end_y ) : boolean | ||
$start_x | integer | x coordinate to start cropping from @param integer $start_y y coordinate to start cropping from @param integer $end_x x coordinate where to end the cropping @param integer $end_y y coordinate where to end the cropping @since 1.0.4 @return boolean Returns TRUE on success or FALSE on error. If FALSE is returned, check the {@link error} property to see the error code. |
$start_y | integer | |
$end_x | integer | |
$end_y | integer | |
return | boolean |
public function crop($start_x, $start_y, $end_x, $end_y)
{
// this method might be also called internally
// in this case, there's a fifth argument that points to an already existing image identifier
$args = func_get_args();
// if fifth argument exists
if (isset($args[4]) && is_resource($args[4])) {
// that it is the image identifier that we'll be using further on
$this->source_identifier = $args[4];
// set this to true so that the script will continue to execute at the next IF
$result = true;
// if method is called as usually
// try to create an image resource from source path
} else {
$result = $this->_create_from_source();
}
// if image resource was successfully created
if ($result !== false) {
// prepare the target image
$target_identifier = $this->_prepare_image($end_x - $start_x, $end_y - $start_y, -1);
// crop the image
imagecopyresampled($target_identifier, $this->source_identifier, 0, 0, $start_x, $start_y, $end_x - $start_x, $end_y - $start_y, $end_x - $start_x, $end_y - $start_y);
// write image
return $this->_write_image($target_identifier);
}
// if script gets this far, return false
// note that we do not set the error level as it has been already set
// by the _create_from_source() method earlier
return false;
}
function cropImage(&$image, $request) { if (!$image['result']) { return; } $cropWidthCount = array_key_exists('cropWidthCount', $request) ? $request['cropWidthCount'] : false; $cropHeightCount = array_key_exists('cropHeightCount', $request) ? $request['cropHeightCount'] : false; if ($cropWidthCount === false && $cropHeightCount === false) { return; } list($width, $height) = getimagesize($image['url']); for ($h = 0; $h < $cropHeightCount; ++$h) { for ($w = 0; $w < $cropWidthCount; ++$w) { $zebraImage = new Zebra_Image(); $zebraImage->source_path = $image['url']; $zebraImage->target_path = dirname($image['url']) . DIRECTORY_SEPARATOR . basename($image['url'], '.' . pathinfo($image['url'], PATHINFO_EXTENSION)) . "_{$w}_{$h}" . '.' . pathinfo($image['url'], PATHINFO_EXTENSION); $zebraImage->jpeg_quality = 100; $image['result'] = $zebraImage->crop(($w + 0) * $width / $cropWidthCount, ($h + 0) * $height / $cropHeightCount, ($w + 1) * $width / $cropWidthCount, ($h + 1) * $height / $cropHeightCount); $image['status'] = extractZebraError($zebraImage); if (!array_key_exists('crop', $image)) { $image['crop'] = array(); } if (!array_key_exists($h, $image['crop'])) { $image['crop'][$h] = array(); } $image['crop'][$h][$w] = array('url' => $zebraImage->target_path); } } }