Skip to content

Commit 667e6a9

Browse files
committed
Initial Commit
0 parents  commit 667e6a9

21 files changed

+1574
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
1. Go to '...'
16+
2. Click on '....'
17+
3. Scroll down to '....'
18+
4. See error
19+
20+
**Expected behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Desktop (please complete the following information):**
27+
- OS: [e.g. iOS]
28+
- Browser [e.g. chrome, safari]
29+
- Version [e.g. 22]
30+
31+
**Smartphone (please complete the following information):**
32+
- Device: [e.g. iPhone6]
33+
- OS: [e.g. iOS8.1]
34+
- Browser [e.g. stock browser, safari]
35+
- Version [e.g. 22]
36+
37+
**Additional context**
38+
Add any other context about the problem here.

.github/ISSUE_TEMPLATE/custom.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
name: Custom issue template
3+
about: Describe this issue template's purpose here.
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

.github/workflows/lint.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Lint
2+
on: [push]
3+
jobs:
4+
lint:
5+
name: PHP Lint
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Setup PHP
9+
uses: shivammathur/setup-php@v2
10+
with:
11+
php-version: 8.0
12+
- uses: actions/checkout@master
13+
- name: lint
14+
run: ./vendor/bin/phpcs -s

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
/.idea
3+
.phpcs-cache
4+
index.php

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) [2021] [Devscast]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# Tinity PHP
2+
The Tinify API allows you to compress and optimize WebP, JPEG and PNG images. It is designed as a REST service. The client libraries in various languages make it very easy to interact with the Tinify API.
3+
4+
## installation
5+
You can use the PHP client by installing the Composer package and adding it to your application’s dependencies:
6+
7+
```bash
8+
composer require devscast/tinify
9+
```
10+
11+
## Authentication
12+
To use the API you must provide your API key.
13+
You can [get an API key](https://tinypng.com/developers) by registering with your name and email address.
14+
Always keep your API key secret!
15+
16+
```php
17+
use Devscast\Tinify\Client;
18+
19+
$tinify = new Client('yourtinifytoken');
20+
```
21+
All requests will be made over an encrypted [HTTPS](https://en.wikipedia.org/wiki/HTTPS) connection.
22+
23+
You can instruct the API client to make all requests over an HTTP proxy. Set the URL of your proxy server, which can optionally include credentials.
24+
25+
```php
26+
use Devscast\Tinify\Client;
27+
28+
$tinify = new Client(
29+
token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
30+
proxy: 'http://user:pass@192.168.0.1:8080'
31+
);
32+
```
33+
34+
## Compressing Images
35+
You can upload any WebP, JPEG or PNG image to the Tinify API to compress it. We will automatically detect the type of image and optimise with the TinyPNG or TinyJPG engine accordingly. Compression will start as soon as you upload a file or provide the URL to the image.
36+
You can choose a local file as the source and write it to another file.
37+
38+
```php
39+
<?php
40+
41+
use Devscast\Tinify\Client;
42+
43+
$tinify = new Client(token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
44+
45+
$tinify->toFile(
46+
source: $tinify->fromFile('/home/tinify/pictures/test.png'),
47+
path: '/home/tinify/pictures/test-compressed.png'
48+
);
49+
```
50+
51+
You can also upload an image from a buffer (a string with binary) and get the compressed image data.
52+
53+
```php
54+
<?php
55+
56+
$sourceBuffer = file_get_contents("unoptimized.jpg");
57+
$compressedBuffer = $tinify->toBuffer(source: $tinify->fromBuffer($data));
58+
```
59+
60+
You can provide a URL to your image instead of having to upload it.
61+
62+
```php
63+
<?php
64+
65+
$tinify->toFile(
66+
source: $tinify->fromUrl('https://tinypng.com/images/panda-happy.png'),
67+
path: '/home/tinify/pictures/test-compressed.png'
68+
);
69+
```
70+
71+
72+
## Resizing images
73+
Use the API to create resized versions of your uploaded images. By letting the API handle resizing you avoid having to write such code yourself and you will only have to upload your image once. The resized images will be optimally compressed with a nice and crisp appearance.
74+
75+
You can also take advantage of intelligent cropping to create thumbnails that focus on the most visually important areas of your image.
76+
77+
Resizing counts as one additional compression. For example, if you upload a single image and retrieve the optimized version plus 2 resized versions this will count as 3 compressions in total.
78+
79+
```php
80+
$tinify->toFile(
81+
source: $tinify->resize(
82+
source: $tinify->fromFile('/home/bernard-ng/Pictures/test.png'),
83+
method: 'fit',
84+
width: 500,
85+
height: 500
86+
),
87+
path: '/home/bernard-ng/tinify/test-compressed-resized.png'
88+
);
89+
```
90+
91+
The method describes the way your image will be resized. The following methods are available:
92+
* **scale** : Scales the image down proportionally. You must provide either a target width or a target height, but not both. The scaled image will have exactly the provided width or height
93+
94+
95+
* **fit** : Scales the image down proportionally so that it fits within the given dimensions. You must provide both a width and a height. The scaled image will not exceed either of these dimensions.
96+
97+
98+
* **cover** Scales the image proportionally and crops it if necessary so that the result has exactly the given dimensions. You must provide both a width and a height. Which parts of the image are cropped away is determined automatically. An intelligent algorithm determines the most important areas of your image.
99+
100+
101+
* **thumb** : A more advanced implementation of cover that also detects cut out images with plain backgrounds. The image is scaled down to the width and height you provide. If an image is detected with a free standing object it will add more background space where necessary or crop the unimportant parts.
102+
103+
If the target dimensions are larger than the original dimensions, the image will not be scaled up. Scaling up is prevented in order to protect the quality of your images.
104+
105+
# Preserving metadata
106+
You can request that specific metadata is copied from the uploaded image to the compressed version. Preserving copyright information, the GPS location and the creation date are currently supported. Preserving metadata adds to the compressed file size, so you should only preserve metadata that is important to keep.
107+
108+
Preserving metadata will not count as an extra compression. However, in the background the image will be created again with the additional metadata.
109+
110+
```php
111+
$tinify->toFile(
112+
source: $tinify->preserve(
113+
source: $tinify->fromFile('/home/bernard-ng/Pictures/test.png'),
114+
metadata: ['creation', 'copyright']
115+
),
116+
path: '/home/bernard-ng/dev/projects/tinify-php/data/test-compressed.png'
117+
);
118+
```
119+
120+
You can provide the following options to preserve specific metadata. No metadata will be added if the requested metadata is not present in the uploaded image.
121+
122+
* **copyright** : Preserves any copyright information. This includes the EXIF copyright tag (JPEG), the XMP rights tag (PNG) as well as a Photoshop copyright flag or URL. Uses up to 90 additional bytes, plus the length of the copyright data.
123+
124+
125+
* **creation** : Preserves any creation date or time. This is the moment the image or photo was originally created. This includes the EXIF original date time tag (JPEG) or the XMP creation time (PNG). Uses around 70 additional bytes.
126+
127+
128+
* **location (JPEG only)** : Preserves any GPS location data that describes where the image or photo was taken. This includes the EXIF GPS latitude and GPS longitude tags (JPEG). Uses around 130 additional bytes.
129+
130+
131+
## Saving to Amazon S3
132+
You can tell the Tinify API to save compressed images directly to [Amazon S3](http://aws.amazon.com/s3/).
133+
If you use S3 to host your images this saves you the hassle of downloading images to your server and uploading them to S3 yourself.
134+
135+
```php
136+
$source = $tinify->toCloud(
137+
source: $tinify->fromFile('/home/tinify/pictures/test.png'),
138+
bucket_path: 'tinify/images/test.png',
139+
storage: new Aws(
140+
region: 'ap-south-1',
141+
secret_access_key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
142+
access_key_id: 'xxxxxxxxxxxxxxx',
143+
option: ['headers' => ['Cache-Control' => 'max-age=31536000, public']] // optional
144+
)
145+
);
146+
```
147+
You need to provide the following options in order to save an image on Amazon S3:
148+
149+
* **aws_access_key_id**
150+
151+
152+
* **aws_secret_access_key** : Your AWS access key ID and secret access key. These are the credentials to an Amazon AWS user account. Find out how to obtain them in Amazon’s documentation. The user must have the correct permissions, see below for details.
153+
154+
155+
* **region** : The AWS region in which your S3 bucket is located.
156+
157+
158+
* **path** : The path at which you want to store the image including the bucket name. The path must be supplied in the following format: ```<bucket>/<path>/<filename>```.
159+
160+
161+
* **headers (experimental)** : You can add a Cache-Control header to control browser caching of the stored image, with for example: public, max-age=31536000. The full list of directives can be found in the MDN web docs.
162+
163+
164+
The user that corresponds to your AWS access key ID must have the **PutObject** and **PutObjectAcl** permissions on the paths of the objects you intend to create.
165+
166+
## Saving to Google Cloud Storage
167+
You can tell the Tinify API to save compressed images directly to [Google Cloud Storage](https://cloud.google.com/storage/).
168+
If you use GCS to host your images this saves you the hassle of downloading images to your server and uploading them to GCS yourself.
169+
170+
Before you can store an image in GCS you will need to generate an access token with a service account.
171+
172+
```php
173+
$tinify->toCloud(
174+
source: $tinify->fromFile('/home/bernard-ng/Pictures/test.png'),
175+
bucket_path: 'tinify/images/test.png',
176+
storage: new Gcs(
177+
access_token: 'XXXXXXXXXXXXXXXXXXXXXXXXX',
178+
option: ['headers' => ['Cache-Control' => 'max-age=31536000, public']] // optional
179+
)
180+
);
181+
```
182+
183+
You need to provide the following options in order to save an image on Google Cloud Storage:
184+
185+
* **gcp_access_token** : The access token for authenticating to Google's Cloud Platform. Find out how to generate these tokens with the example above.
186+
187+
188+
* **path** : The path at which you want to store the image including the bucket name. The path must be supplied in the following format: ```<bucket>/<path>/<filename>```.
189+
190+
191+
* **headers (experimental)** : You can add a Cache-Control header to control browser caching of the stored image, with for example: public, max-age=31536000. The full list of directives can be found in the MDN web docs.
192+
193+
## Compression count
194+
The API client automatically keeps track of the number of compressions you have made this month.
195+
You can get the compression count after you have validated your API key or after you have made at least one compression request.
196+
197+
```php
198+
<?php
199+
200+
$source = $tinify->fromUrl('https://tinypng.com/images/panda-happy.png');
201+
$source->getCompressionCount();
202+
203+
$tinify->toFile($source, path: '/home/tinify/pictures/test-compressed.png');
204+
```
205+
206+
## acknowledgement
207+
208+
this package is a reimplementation of the tinify/tinify-php library, supporting PHP 8, rewritten with a design that removes static calls for a more object-oriented approach
209+
210+
* [tinify/tinify-php](https://github.com/tinify/tinify-php)
211+
* [Tinify Documentation](https://tinypng.com/developers/reference)

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "devscast/tinify",
3+
"description": "Tinify PHP client",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Devscast\\Tinify\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "bernard-ng",
14+
"email": "bernard@devscast.tech"
15+
}
16+
],
17+
"minimum-stability": "stable",
18+
"require-dev": {
19+
"squizlabs/php_codesniffer": "^3.6",
20+
"symfony/var-dumper": "^5.3"
21+
},
22+
"require": {
23+
"symfony/http-client": "^5.3"
24+
}
25+
}

0 commit comments

Comments
 (0)