Skip to content

Commit cf5987e

Browse files
committed
init
1 parent 2174354 commit cf5987e

File tree

6 files changed

+305
-2
lines changed

6 files changed

+305
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

ProgressiveImages.php

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
3+
namespace Izica;
4+
5+
require_once 'ProgressiveImagesResponse.php';
6+
7+
class ProgressiveImages
8+
{
9+
private $sFileSource = null;
10+
private $sFileName = null;
11+
private $sDestinationFolder = '';
12+
private $fWebp = true;
13+
private $fJpeg2000 = true;
14+
private $fJpegXr = true;
15+
16+
function __construct($sFileSource)
17+
{
18+
$this->sFileSource = $sFileSource;
19+
}
20+
21+
public static function fromFileSource($sFileSource)
22+
{
23+
return new ProgressiveImages($sFileSource);
24+
}
25+
26+
public function setDestinationFolder($sDestinationFolder)
27+
{
28+
if (!is_dir($sDestinationFolder)) {
29+
if (!mkdir($sDestinationFolder, 0755, true)) {
30+
$this->responseError("Can't create destination folder");
31+
}
32+
}
33+
$this->sDestinationFolder = $sDestinationFolder;
34+
return $this;
35+
}
36+
37+
public function setFileName($sFileName)
38+
{
39+
$this->sFileName = $sFileName;
40+
return $this;
41+
}
42+
43+
public function withoutWebp()
44+
{
45+
$this->fWebp = false;
46+
return $this;
47+
}
48+
49+
public function withoutJpegXr()
50+
{
51+
$this->fJpegXr = false;
52+
return $this;
53+
}
54+
55+
public function withoutJpeg2000()
56+
{
57+
$this->fJpeg2000 = false;
58+
return $this;
59+
}
60+
61+
public function convert()
62+
{
63+
$sFilepath = $this->getFilePath($this->sFileSource);
64+
65+
if (!$sFilepath) {
66+
return $this->responseError('Source file not found');
67+
}
68+
69+
$sFilename = $this->sFileName !== null ? $this->sFileName : $this->getFileName($sFilepath);
70+
71+
return new ProgressiveImagesResponse(
72+
str_replace($_SERVER['DOCUMENT_ROOT'], '', $sFilepath),
73+
$this->toWebp($sFilepath, $sFilename),
74+
$this->toJpeg2000($sFilepath, $sFilename),
75+
$this->toJpegXr($sFilepath, $sFilename)
76+
);
77+
}
78+
79+
private function toWebp($sSource, $sFilename)
80+
{
81+
$sDest = $this->sDestinationFolder . $sFilename . '.webp';
82+
$sDestWeb = str_replace($_SERVER['DOCUMENT_ROOT'], '', $sDest);
83+
84+
if (file_exists($sDest)) {
85+
return $sDestWeb;
86+
}
87+
88+
if (!$this->fWebp) {
89+
return null;
90+
}
91+
92+
exec("convert {$sSource} {$sDest}");
93+
94+
return $sDestWeb;
95+
}
96+
97+
private function toJpegXr($sSource, $sFilename)
98+
{
99+
$sDestBuffer = $this->sDestinationFolder . $sFilename . '.tif';
100+
$sDest = $this->sDestinationFolder . $sFilename . '.jxr';
101+
$sDestWeb = str_replace($_SERVER['DOCUMENT_ROOT'], '', $sDest);
102+
103+
if (file_exists($sDest)) {
104+
return $sDestWeb;
105+
}
106+
107+
if (!$this->fJpegXr) {
108+
return null;
109+
}
110+
111+
exec("convert {$sSource} -compress none {$sDestBuffer}");
112+
exec("JxrEncApp -i {$sDestBuffer} -o $sDest", $sResponse);
113+
exec("rm {$sDestBuffer}");
114+
115+
return $sDestWeb;
116+
}
117+
118+
private function toJpeg2000($sSource, $sFilename)
119+
{
120+
$sDest = $this->sDestinationFolder . $sFilename . '.jp2';
121+
$sDestWeb = str_replace($_SERVER['DOCUMENT_ROOT'], '', $sDest);
122+
123+
if (file_exists($sDest)) {
124+
return $sDestWeb;
125+
}
126+
127+
if (!$this->fJpeg2000) {
128+
return null;
129+
}
130+
131+
exec("convert {$sSource} {$sDest}");
132+
133+
return $sDestWeb;
134+
}
135+
136+
private function getFileName($sFilePath)
137+
{
138+
$sBasename = basename($sFilePath);
139+
140+
$arBasename = explode('.', $sBasename);
141+
if (count($arBasename) > 1) {
142+
$arBasename = array_slice($arBasename, 0, -1);
143+
}
144+
return implode('', $arBasename);
145+
}
146+
147+
private function getFilePath($sFilename)
148+
{
149+
if (file_exists($sFilename)) {
150+
return $sFilename;
151+
}
152+
153+
if (file_exists($_SERVER['DOCUMENT_ROOT'] . $sFilename)) {
154+
return $_SERVER['DOCUMENT_ROOT'] . $sFilename;
155+
}
156+
157+
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $sFilename)) {
158+
return $_SERVER['DOCUMENT_ROOT'] . '/' . $sFilename;
159+
}
160+
161+
return false;
162+
}
163+
164+
private static function responseError($sText = 'file not found')
165+
{
166+
return $sText;
167+
}
168+
}

ProgressiveImagesResponse.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Izica;
4+
5+
class ProgressiveImagesResponse
6+
{
7+
public $source = null;
8+
public $webp = null;
9+
public $jpeg2000 = null;
10+
public $jpegxr = null;
11+
12+
function __construct($source, $webp, $jpeg2000, $jpegxr)
13+
{
14+
$this->source = $source;
15+
$this->webp = $webp;
16+
$this->jpeg2000 = $jpeg2000;
17+
$this->jpegxr = $jpegxr;
18+
}
19+
20+
public function toArray()
21+
{
22+
return [
23+
'source' => $this->source,
24+
'webp' => $this->webp,
25+
'jpeg2000' => $this->jpeg2000,
26+
'jpegxr' => $this->jpegxr,
27+
];
28+
}
29+
30+
}

README.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "izica/php-progressive-images",
3+
"description": "Php library for converting and caching images to Webp .webp, JpegXr .jxr, Jpeg 2000 .jp2",
4+
"authors": [
5+
{
6+
"name": "Golovarchuk Artyom",
7+
"email": "artemiztomska@gmail.com"
8+
}
9+
],
10+
"keywords": ["php", "imagick", "imagemagick", "webp", "jpeg2000", "jpegxr", "progressive", "tools", "utilities", "images"],
11+
"license": "MIT",
12+
"require": {
13+
"php": ">=5.6.0"
14+
},
15+
"autoload": {
16+
"classmap": [
17+
"ProgressiveImages.php"
18+
]
19+
}
20+
}

readme.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
## Desription
2+
Library converts your images to progressive images formats
3+
4+
* Webp
5+
* Jpeg 2000
6+
* Jpeg XR
7+
8+
## Warning
9+
Currently works only on Unix systems, you can help me achieve cross-platform waiting for your pull requests.
10+
11+
## Requirements
12+
```shell script
13+
imagemagick
14+
webp
15+
libjxr-tools
16+
```
17+
18+
## Installation
19+
```
20+
sudo apt-get install imagemagick webp libjxr-tools
21+
composer require izica/php-progressive-images
22+
```
23+
24+
## Usage(Examples)
25+
```php
26+
<?php
27+
$obData = \Izica\ProgressiveImages::fromFileSource('/upload/iblock/fe7/fe7728c5f2c6763693eb1d9ef105c46c.png')
28+
->setFileName('custom-file-name')
29+
->setDestinationFolder($_SERVER['DOCUMENT_ROOT'] . '/test/cache/')
30+
->convert();
31+
/*
32+
33+
Izica\ProgressiveImagesResponse Object
34+
(
35+
[source] => /upload/iblock/fe7/fe7728c5f2c6763693eb1d9ef105c46c.png
36+
[webp] => /test/cache/custom-file-name.webp
37+
[jpeg2000] => /test/cache/custom-file-name.jp2
38+
[jpegxr] => /test/cache/custom-file-name.jxr
39+
)
40+
*/
41+
?>
42+
43+
<picture>
44+
<source srcset="<?=$obData->jpegxr;?>" type='image/vnd.ms-photo'>
45+
<source srcset="<?=$obData->jpeg2000;?>" type='image/jp2'>
46+
<source srcset="<?=$obData->webp;?>" type="image/webp">
47+
<img src="<?=$obData->source;?>" alt="">
48+
</picture>
49+
50+
```
51+
52+
```php
53+
$arData = \Izica\ProgressiveImages::fromFileSource('/upload/iblock/fe7/fe7728c5f2c6763693eb1d9ef105c46c.png')
54+
->setFileName('custom-file-name')
55+
->setDestinationFolder($_SERVER['DOCUMENT_ROOT'] . '/test/cache/')
56+
->convert()
57+
->toArray();
58+
/*
59+
Array
60+
(
61+
[source] => /upload/iblock/fe7/fe7728c5f2c6763693eb1d9ef105c46c.png
62+
[webp] => /test/cache/custom-file-name.webp
63+
[jpeg2000] => /test/cache/custom-file-name.jp2
64+
[jpegxr] => /test/cache/custom-file-name.jxr
65+
)
66+
*/
67+
```
68+
69+
70+
```php
71+
$arData = \Izica\ProgressiveImages::fromFileSource('/upload/iblock/fe7/fe7728c5f2c6763693eb1d9ef105c46c.png')
72+
->setFileName('custom-file-name')
73+
->setDestinationFolder($_SERVER['DOCUMENT_ROOT'] . '/test/cache/')
74+
->withoutJpegXr()
75+
->withoutJpeg2000()
76+
->convert();
77+
/*
78+
Izica\ProgressiveImagesResponse Object
79+
(
80+
[source] => /upload/iblock/fe7/fe7728c5f2c6763693eb1d9ef105c46c.png
81+
[webp] => /test/cache/custom-file-name.webp
82+
[jpeg2000] =>
83+
[jpegxr] =>
84+
)
85+
*/
86+
```

0 commit comments

Comments
 (0)