Skip to content

Commit

Permalink
[UPD] Graphics.php
Browse files Browse the repository at this point in the history
  • Loading branch information
robmachado committed May 14, 2015
1 parent 52f72b4 commit f6d3092
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
local
vendor
nbproject
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
3 changes: 3 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

include __DIR__ . '/vendor/autoload.php';
6 changes: 3 additions & 3 deletions composer.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
"require": {
"php": ">=5.3.0",
"hyperthese/php-serial": "dev-master",
"intervention/image": "dev-master"
"endroid/qrcode": "1.*@dev"
},
"require-dev": {
"phpunit/phpunit": "4.8.*@dev"
},
"autoload": {
"psr-4": {
"POSPrint\\": "src/"
"Posprint\\": "src/"
},
"psr-0": {
"": ["src/", "test/"]
"Posprint\\": ["src/", "test/"]
},
"classmap": [
"./",
Expand Down
98 changes: 46 additions & 52 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 75 additions & 33 deletions src/Common/Graphics.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Posprinter\Common;
namespace Posprint\Common;

use Endroid\QrCode\QrCode;
use Intervention\Image\ImageManagerStatic as Image;
use Exception;

class Graphics
{
Expand All @@ -12,11 +12,11 @@ class Graphics
public static $imgWidth;

/**
* Seta o uso da extensão Imagick ao invés de GD
*
*/
public function __construct()
public function __construct($filename = '', $width = null, $height = null)
{
Image::configure(array('driver' => 'imagick'));
self::loadImage($filename, $width, $height);
}

/**
Expand All @@ -25,30 +25,34 @@ public function __construct()
* @param type $width
* @param type $height
*/
public static function loadImage($resource, $width = null, $height = null)
public static function loadImage($filename, $width = null, $height = null)
{
$img = Image::make($resource);
if ($width != null && $height != null) {
$img->fit($width, $height);
} elseif ($width != null || $height != null) {
$img->resize(
$width,
$height,
function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
}
);
if (! is_file($filename)) {
return;
}
if (!is_readable($filename)) {
throw new Exception("Não é possivel ler esse arquivo '$filename' Permissões!!");
}
$tipo = self::zIdentifyImg($filename);
$func = 'imagecreatefrom' . strtolower($tipo);
if (! function_exists($func)) {
throw new Exception("Não é possivel usar ou tratar esse tipo de imagem, com GD");
}
self::$img = $func($filename);
if (! self::$img) {
throw new Exception("Falhou ao carregar a imagem '$filename'.");
}
self::zLoadDimImage();
if ($width != null || $height != null) {
self::resizeImage($width, $height);
}
self::$img = $img->getCore();
self::zWHimg();
}

/**
*
* @return type
*/
public static function getRaster()
public static function getImageRaster()
{
return self::zGetBinaryImage(self::$img);
}
Expand All @@ -60,9 +64,18 @@ public static function getRaster()
*/
public static function resizeImage($width = null, $height = null)
{
self::loadImage(self::$img, $width, $height);
if ($width != null && $height == null) {
$razao = $width / self::$imgWidth;
$height = (int) round($razao * self::$imgHeight, 0);
} elseif ($width == null && $height != null) {
$razao = $height / self::$imgHeight;
$width = (int) round($razao * self::$imgWidth, 0);
}
$tempimg = imagecreatetruecolor($width, $height);
imagecopyresampled($tempimg, self::$img, 0, 0, 0, 0, $width, $height, self::$imgWidth, self::$imgHeight);
self::$img = $tempimg;
self::zLoadDimImage();
}


/**
*
Expand All @@ -72,7 +85,7 @@ public static function resizeImage($width = null, $height = null)
* @param string $imageType PNG, GIF, JPEG, WBMP
* @param string $dataText dados do QRCode
*/
public function createQRCodeImg(
public static function getImageQRCode(
$width = 200,
$padding = 10,
$errCorretion = 'low',
Expand All @@ -92,22 +105,24 @@ public function createQRCodeImg(
->setLabel('')
->setLabelFontSize(8);
self::$img = $qrCode->getImage();
self::zWHimg();
self::zLoadDimImage();
}

public static function getImage()
{
return self::$img;
}

/**
*
* @param type $im
* @return type
*/
protected static function zGetBinaryImage($objImg)
protected static function zGetBinaryImage()
{
$data = "";
$yMax = imagesx($objImg); // image width
$xMax = imagesy($objImg); // image height
for ($xPos = 0; $xPos < $xMax; $xPos++) {
for ($yPos = 0; $yPos < $yMax; $yPos++) {
$rgb = imagecolorat($objImg, $yPos, $xPos);
for ($yPos = 0; $yPos < self::$imgHeight; $yPos++) {
for ($xPos = 0; $xPos < self::$imgWidth; $xPos++) {
$rgb = imagecolorat(self::$img, $xPos, $yPos);
$red = ($rgb >> 16) & 0xFF;
$green = ($rgb >> 8) & 0xFF;
$blue = $rgb & 0xFF;
Expand All @@ -121,9 +136,36 @@ protected static function zGetBinaryImage($objImg)
/**
*
*/
private static function zWHimg()
private static function zLoadDimImage()
{
self::$imgHeight = imagesy(self::$img);
self::$imgWidth = imagesx(self::$img);
}

/**
* zIdentifyImg
* @param string $filename
* @return string
*/
private static function zIdentifyImg($filename)
{
$imgtype = exif_imagetype($filename);
switch ($imgtype) {
case 1:
$typo = 'GIF';
break;
case 2:
$typo = 'JPEG';
break;
case 3:
$typo = 'PNG';
break;
case 15:
$typo = 'WBMP';
break;
default:
$typo = 'none';
}
return $typo;
}
}
4 changes: 2 additions & 2 deletions tests/Common/GraphicsTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Class Graphics
* @author Roberto L. Machado <linux.rlm at gmail dot com>
*/
namespace Posprinter\tests\Common;
namespace Posprint\tests\Common;

use Posprinter\Common\Graphics;
use Posprint\Common\Graphics;

class GraphicsTest extends PHPUnit_Framework_TestCase
{
Expand Down
Empty file modified tests/fixtures/.gitkeep
100644 → 100755
Empty file.

0 comments on commit f6d3092

Please sign in to comment.