Files
ferramentas/document_processor.php
T

140 lines
4.1 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->detectEdges()
->autoRotate()
->enhanceImage()
->saveResult();
return $this->outputPath;
}
private function detectEdges() {
$width = imagesx($this->srcImage);
$height = imagesy($this->srcImage);
// Cria imagem temporária
$temp = imagecreatetruecolor($width, $height);
imagecopy($temp, $this->srcImage, 0, 0, 0, 0, $width, $height);
// Converte para escala de cinza
imagefilter($temp, IMG_FILTER_GRAYSCALE);
// Aplica detecção de bordas
imagefilter($temp, IMG_FILTER_EDGEDETECT);
$this->processedImage = $temp;
return $this;
}
private function autoRotate() {
if (!isset($this->processedImage)) {
$this->processedImage = $this->srcImage;
}
$width = imagesx($this->processedImage);
$height = imagesy($this->processedImage);
// Detecta ângulo baseado em bordas horizontais/verticais
$angle = $this->detectRotationAngle();
if ($angle != 0) {
$rotated = imagerotate($this->processedImage, $angle, 0);
if ($rotated !== false) {
$this->processedImage = $rotated;
}
}
return $this;
}
private function enhanceImage() {
if (!isset($this->processedImage)) {
$this->processedImage = $this->srcImage;
}
// 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 detectRotationAngle() {
// Implementação simplificada de detecção de ângulo
// Baseado na análise de bordas horizontais e verticais
return 0; // Por enquanto retorna 0 (sem rotação)
}
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, 90);
break;
case 'png':
imagepng($this->processedImage, $this->outputPath, 9);
break;
default:
throw new Exception('Unsupported output image type');
}
if (is_resource($this->srcImage)) {
imagedestroy($this->srcImage);
}
if (is_resource($this->processedImage)) {
imagedestroy($this->processedImage);
}
return $this;
}
}