Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage other units for print #159

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Converter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Converter resource
* User: Clem
* Date: 30/05/2019
* Time: 21:46
*/

namespace Gregwar\Image;

class Converter
{

/**
* Convert Cm to Pixels using resolution in DPI
* @param float $cm
* @param int $resolution in DPI (PPP)
* @return float
*/
static function cmToPixels($cm, $resolution = 96) {
return $resolution * $cm / 2.54;
}

/**
* Convert Inch to Pixels using resolution in DPI
* @param float $inch
* @param int $resolution in DPI (PPP)
* @return float
*/
static function inchToPixels($inch, $resolution = 96) {
return $resolution * $inch;
}

/**
* Convert Pixels to Cm using resolution in DPI
* @param int $pixels
* @param int $resolution in DPI (PPP)
* @return float|int|boolean
*/
static function pixelsToCm($pixels, $resolution = 96) {
if($resolution == 0) {
return false;
}
return $pixels * 2.54 / $resolution;
}

/**
* Convert Pixels to Inch using resolution in DPI
* @param int $pixels
* @param int $resolution in DPI (PPP)
* @return float|boolean
*/
static function pixelsToInch($pixels, $resolution = 96) {
if($resolution == 0) {
return false;
}
return $pixels / $resolution;
}

}
16 changes: 16 additions & 0 deletions Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,22 @@ public function inline($type = 'jpg', $quality = 80)
return 'data:image/'.$mime.';base64,'.base64_encode(file_get_contents($this->cacheFile($type, $quality, true)));
}

/**
* Return the resolution of resource
* @param string $type
* @return array|bool|mixed
*/
public function getResolution($type = 'both')
{
if(function_exists('imageresolution')) {
$res = imageresolution ($this->getAdapter()->getResource());
if(is_array($res)) {
return ($type === 'both') ? $res : (($type === 'Y') ? $res[1] : $res[0]);
}
}
return false;
}

/**
* Creates an instance, usefull for one-line chaining.
*/
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ The other methods available are:

* `inline($type = 'jpg')`: returns the HTML inlinable base64 string (see `demo/inline.php`)

* `getResolution($type = 'both')`: returns the resolution of picture in DPI. Only available for >= PHP 7. Other $type options: `X` for horizontal resolution, `Y` for vertical resolution, `both` for the both.

You can also create image from scratch using:

```php
Expand All @@ -138,6 +140,20 @@ You can save the image to an explicit file using `save($file, $type = 'jpg', $qu

You can also get the contents of the image using `get($type = 'jpg', $quality = 80)`, which will return the binary contents of the image

## Converter

Converter allow you to convert centimeters to pixels, inch to pixels... using defined resolution in DPI.

The following static method are available:

* `cmToPixels($cm, $resolution = 96)`: convert centimeters to pixels.

* `inchToPixels($cm, $resolution = 96)`: convert inch to pixels.

* `pixelsToCm($cm, $resolution = 96)`: convert pixels to centimeters.

* `pixelsToInch($cm, $resolution = 96)`: convert pixels to inch.

## Using cache

Each operation above is not actually applied on the opened image, but added in an operations
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@
"psr-0": {
"Gregwar\\Image": ""
}
},
"scripts": {
"test": "simple-phpunit --bootstrap vendor/autoload.php"
}
}
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<testsuite name="Image testing">
<file>./tests/ImageTests.php</file>
</testsuite>
<testsuite name="Converter testing">
<file>./tests/ConverterTests.php</file>
</testsuite>
</testsuites>

</phpunit>
66 changes: 66 additions & 0 deletions tests/ConverterTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Tests for Converter class
* User: Clem
* Date: 31/05/2019
* Time: 09:39
*/

use \Gregwar\Image\Converter;

class ConverterTests extends \PHPUnit\Framework\TestCase
{
public function testCmToPixels()
{
// Basic convertion
$this->assertEquals(37.795275591, Converter::cmToPixels(1), '', 0.001);

// 6cm convertion
$this->assertEquals(226.771653543, Converter::cmToPixels(6), '', 0.001);

// 6cm convertion with 300 dpi
$this->assertEquals(708.6614173, Converter::cmToPixels(6, 300), '', 0.001);
}

public function testInchToPixels()
{
// Basic convertion
$this->assertEquals(96, Converter::inchToPixels(1));

// 6inch convertion
$this->assertEquals(576, Converter::inchToPixels(6));

// 6inch convertion with 300 dpi
$this->assertEquals(1800, Converter::inchToPixels(6, 300));
}

public function testPixelsToCm()
{
// Basic convertion
$this->assertEquals(0.026458333, Converter::pixelsToCm(1), '', 0.001);

// 960 pixels convertion
$this->assertEquals(25.4, Converter::pixelsToCm(960), '', 0.001);

// 960 pixels with 300 dpi
$this->assertEquals(8.128, Converter::pixelsToCm(960, 300), '', 0.001);

// 960 pixels with 0 dpi
$this->assertEquals(false, Converter::pixelsToCm(960, 0));
}

public function testPixelsToInch()
{
// Basic convertion
$this->assertEquals(0.010416667, Converter::pixelsToInch(1), '', 0.001);

// 960 pixels convertion
$this->assertEquals(10, Converter::pixelsToInch(960), '', 0.001);

// 960 pixels with 300 dpi
$this->assertEquals(3.2, Converter::pixelsToInch(960, 300), '', 0.001);

// 960 pixels with 0 dpi
$this->assertEquals(false, Converter::pixelsToInch(960, 0));
}
}
Loading