Skip to content

Instantly share code, notes, and snippets.

@langemike
Created February 22, 2017 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save langemike/8080968fa336b5c1aa196c6470c36633 to your computer and use it in GitHub Desktop.
Save langemike/8080968fa336b5c1aa196c6470c36633 to your computer and use it in GitHub Desktop.
Image scanner pixel colors in edge
<?php
$scan = new ImageScanner('plaatje2.png');
if ($scan->contains_something_in_edge()) {
echo '<font color="red">Image needs bleed</font>';
} else {
echo '<font color="green">Image doesn\'t need bleed</font>';
}
class ImageScanner {
/**
* Image resource
* @var resource $image
**/
protected $image = null;
/**
* Accepted colors within image edges
* @var array $accepted
**/
protected $accepted = ['#ffffff'];
/**
* Image scanner
* @param string $image Image path
**/
public function __construct($image)
{
$this->image = imagecreatefrompng($image);
}
/**
* Validation criteria for x, y coordinate
* @param int $x
* @param int $y
* @return bool
**/
protected function validates($x, $y)
{
$hex = $this->hex($x, $y);
return in_array($hex, array_map('strtolower', $this->accepted), true);
}
/**
* Detect if bleed is needed
* @return bool
**/
public function contains_something_in_edge()
{
return !empty($this->scan());
}
/**
* Scan image for irregularities on edges
* @param int $max_depth Pixel lines to scan from outside to inside
* @param int $max_tries Will fail after these tries
* @return array
**/
public function scan($max_depth = 5, $max_tries = 0)
{
$divergent = array();
$order = ['top', 'right', 'bottom', 'left'];
for ($distance = 0; $distance <= $max_depth; $distance++) {
foreach ($order as $edge) {
//$this->log(sprintf('======= %d | %s =======', $distance, $edge));
$settings = $this->settings($edge, $distance);
extract($settings);
$loop = true;
while($loop) {
if ($this->validates($x, $y) === false) {
$color = $this->hex($x, $y);
$divergent[] = compact('x', 'y', 'distance', 'color');
}
//$this->log($x . ',' . $y . ' = ' . $this->hex($x, $y));
$x += $increment_x;
$y += $increment_y;
// Break outer loop when reached maximum tries
if ($max_tries > 0 && count($divergent) === $max_tries) {
break 3;
}
// Decide switching to next edge
if ($increment_y > 0) {
$loop = $y <= $stop;
} else if ($increment_y < 0) {
$loop = $y >= $stop;
} else if ($increment_x > 0) {
$loop = $x <= $stop;
} else if ($increment_x < 0) {
$loop = $x >= $stop;
} else {
throw Exception('Invalid increment value');
}
}
}
}
//$this->log($divergent);
return $divergent;
}
/**
* Get settings for image scanning
* @param string $edge
* @param int $distance
* @param int $increment
* @return array
**/
protected function settings($edge, $distance, $increment = 1)
{
// -1 because of x, y starts at 0,0
$width = imagesx($this->image) - 1;
$height = imagesy($this->image) - 1;
switch($edge) {
case 'top' :
return array(
'x' => $distance,
'y' => $distance,
'increment_x' => $increment,
'increment_y' => 0,
'stop' => $width - $distance
);
case 'right' :
return array(
'x' => $width - $distance,
'y' => $distance,
'increment_x' => 0,
'increment_y' => $increment,
'stop' => $height - $distance
);
case 'bottom' :
return array(
'x' => $width - $distance,
'y' => $height - $distance,
'increment_x' => -$increment,
'increment_y' => 0,
'stop' => $distance
);
case 'left' :
return array(
'x' => $distance,
'y' => $height - $distance,
'increment_x' => 0,
'increment_y' => -$increment,
'stop' => $distance
);
}
return array();
}
/**
* Get HEX color
* @param int $x
* @param int $y
* @return string
**/
protected function hex($x, $y)
{
$color = $this->rgb($x, $y);
return sprintf("#%02x%02x%02x", $color['red'], $color['green'], $color['blue']);
}
/**
* Get RGB color
* @param int $x
* @param int $y
* @return array
**/
protected function rgb($x, $y)
{
$index = imagecolorat($this->image, $x, $y);
$color = imagecolorsforindex($this->image, $index);
return $color;
}
/**
* Write debug log
* @param mixed $data
**/
protected function log($data)
{
var_dump($data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment