110 lines
3.4 KiB
PHP
110 lines
3.4 KiB
PHP
<?php
|
|
class DocumentProcessor {
|
|
private $srcImage;
|
|
private $processedImage;
|
|
private $outputPath;
|
|
|
|
public function __construct() {
|
|
if (!extension_loaded('gd')) {
|
|
throw new Exception('GD extension is required');
|
|
}
|
|
}
|
|
|
|
public function processDocument($imagePath, $outputPath) {
|
|
$this->outputPath = $outputPath;
|
|
|
|
// Detecta o tipo da imagem e carrega
|
|
$imageInfo = getimagesize($imagePath);
|
|
if ($imageInfo === false) {
|
|
throw new Exception('Failed to load image');
|
|
}
|
|
|
|
switch ($imageInfo[2]) {
|
|
case IMAGETYPE_JPEG:
|
|
$this->srcImage = imagecreatefromjpeg($imagePath);
|
|
break;
|
|
case IMAGETYPE_PNG:
|
|
$this->srcImage = imagecreatefrompng($imagePath);
|
|
break;
|
|
default:
|
|
throw new Exception('Unsupported image type');
|
|
}
|
|
|
|
if ($this->srcImage === false) {
|
|
throw new Exception('Failed to create image resource');
|
|
}
|
|
|
|
// Pipeline de processamento
|
|
$this->enhanceImage()
|
|
->saveResult();
|
|
|
|
return $this->outputPath;
|
|
}
|
|
|
|
private function enhanceImage() {
|
|
// Obtém dimensões originais
|
|
$width = imagesx($this->srcImage);
|
|
$height = imagesy($this->srcImage);
|
|
|
|
// Cria nova imagem
|
|
$this->processedImage = imagecreatetruecolor($width, $height);
|
|
|
|
// Preserva transparência para PNGs
|
|
imagealphablending($this->processedImage, false);
|
|
imagesavealpha($this->processedImage, true);
|
|
|
|
// Copia imagem original
|
|
imagecopy($this->processedImage, $this->srcImage, 0, 0, 0, 0, $width, $height);
|
|
|
|
// Ajusta contraste
|
|
imagefilter($this->processedImage, IMG_FILTER_CONTRAST, -10);
|
|
|
|
// Ajusta brilho
|
|
imagefilter($this->processedImage, IMG_FILTER_BRIGHTNESS, 10);
|
|
|
|
// Ajusta nitidez
|
|
imagefilter($this->processedImage, IMG_FILTER_SMOOTH, -7);
|
|
|
|
// Ajusta saturação
|
|
imagefilter($this->processedImage, IMG_FILTER_COLORIZE, 0, 0, 0, 10);
|
|
|
|
return $this;
|
|
}
|
|
|
|
private function saveResult() {
|
|
if (!isset($this->processedImage)) {
|
|
throw new Exception('No processed image to save');
|
|
}
|
|
|
|
$extension = strtolower(pathinfo($this->outputPath, PATHINFO_EXTENSION));
|
|
|
|
switch ($extension) {
|
|
case 'jpg':
|
|
case 'jpeg':
|
|
imagejpeg($this->processedImage, $this->outputPath);
|
|
break;
|
|
case 'png':
|
|
imagepng($this->processedImage, $this->outputPath);
|
|
break;
|
|
default:
|
|
throw new Exception('Unsupported output image type');
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
private function orderPoints($pts) {
|
|
// Ordena pontos para transformação de perspectiva
|
|
$rect = array_fill(0, 4, [0, 0]);
|
|
$s = array_map(function($p) { return $p[0] + $p[1]; }, $pts);
|
|
$d = array_map(function($p) { return $p[0] - $p[1]; }, $pts);
|
|
|
|
$rect[0] = $pts[array_search(min($s), $s)]; // top-left
|
|
$rect[2] = $pts[array_search(max($s), $s)]; // bottom-right
|
|
$rect[1] = $pts[array_search(min($d), $d)]; // top-right
|
|
$rect[3] = $pts[array_search(max($d), $d)]; // bottom-left
|
|
|
|
return $rect;
|
|
}
|
|
}
|