Вот мой код:
<?php
namespace App\Traits;
trait BlacknessCheck {
public static function fileIsBlack($filename, $throw_error = false) {
if (exif_imagetype($filename) === IMAGETYPE_JPEG) {
return self::imageIsBlack(imagecreatefromjpeg($filename));
} elseif ($isimage === IMAGETYPE_PNG) {
return self::imageIsBlack(imagecreatefrompng($filename));
} else {
if ($throw_error) {
throw new \Exception("Provided file is not an image.");
} else {
return false;
}
}
}
public static function imageIsBlack($image) {
$scaled_image = self::scaleImage($image);
$added_colors =
imagecolorat($scaled_image, 0, 0) +
imagecolorat($scaled_image, 0, 1) +
imagecolorat($scaled_image, 0, 2) +
imagecolorat($scaled_image, 1, 0) +
imagecolorat($scaled_image, 1, 1) +
imagecolorat($scaled_image, 1, 2) +
imagecolorat($scaled_image, 2, 0) +
imagecolorat($scaled_image, 2, 1) +
imagecolorat($scaled_image, 2, 2);
imagedestroy($scaled_image);
imagedestroy($image);
return ($added_colors === 0);
}
public static function scaleImage($image) {
$newimage = imagecreatetruecolor(3, 3);
imagecopyresampled(
$newimage, $image,
0, 0, 0, 0,
3, 3, imagesx($image), imagesy($image)
);
return $newimage;
}
}
Я использую его для удаления черных пальцев.
private function removeBlackness() {
$files = $this->glob_recursive(public_path('img/collection/*.jpg'));
foreach ($files as $filename) {
if (self::fileIsBlack($filename)) {
unlink($filename);
}
}
}