Skip to content

Commit fd69a90

Browse files
committed
Update Raw Data
1 parent 2bea160 commit fd69a90

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ $instance = new PNGQuant();
4646
```
4747
# Example
4848

49+
Save the file directly into a directory with a file name.
50+
4951
```php
5052
// Include the class
5153
use ourcodeworld\PNGQuant\PNGQuant;
@@ -72,6 +74,43 @@ if(!$exit_code){
7274
}
7375
```
7476

77+
If you need the raw data of the image (store the binary data of the compressed image), you can use the `getRawOutput` function:
78+
79+
```php
80+
// Include the class
81+
use ourcodeworld\PNGQuant\PNGQuant;
82+
83+
$instance = new PNGQuant();
84+
85+
// Set the path to the image to compress
86+
$result = $instance->setImage("/a-folder/image-original.png")
87+
// Set the output filepath
88+
->setOutputImage("/a-folder/image-compressed.png")
89+
// Overwrite output file if exists, otherwise pngquant will generate output ...
90+
->overwriteExistingFile()
91+
// As the quality in pngquant isn't fixed (it uses a range)
92+
// set the quality between 50 and 80
93+
->setQuality(50,80)
94+
// Retrieve RAW data from pngquant
95+
->getRawOutput();
96+
97+
$exit_code = $result["statusCode"];
98+
99+
100+
// if exit code is equal to 0 then everything went right !
101+
if($exit_code == 0){
102+
103+
$rawImage = imagecreatefromstring($result["imageData"]);
104+
105+
// Example Save the PNG Image from the raw data into a file or do whatever you want.
106+
// imagepng($rawImage , 'newfile.png');
107+
108+
echo "Image succesfully compressed, do something with the raw Data";
109+
}else{
110+
echo "Something went wrong (status code $exit_code) with description: ". $instance->getErrorTable()[(string) $exit_code];
111+
}
112+
```
113+
75114
# Documentation
76115

77116
For further information and examples of the wrapper, please [visit the official documentation here](http://docs.ourcodeworld.com/projects/php-pngquant).

src/PNGQuant.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ function execute(){
163163
* Execute PNGQUANT with the providen commands and retrieve the generated image
164164
* directly into a variable
165165
*/
166+
function getRawOutput(){
166167
// Create a temporal file in the system
167168
$fileName = uniqid().'.png';
168169
$temp_file = tempnam(sys_get_temp_dir(), $fileName);

0 commit comments

Comments
 (0)