PHP
Real-time image resizing, automatic optimization, and file uploading in PHP using ImageKit.io.
ImageKit's PHP SDK provides comprehensive yet straightforward asset upload, transformation, optimization, and delivery capabilities that you can implement seamlessly in your existing PHP application.
This quick start guide shows you how to integrate ImageKit into your PHP application. The code samples covered here are hosted on Github - https://github.com/imagekit-developer/imagekit-php/tree/master/sample.
This guide walks you through the following topics:
Setting up ImageKit PHP SDK
Prerequisites
To use ImageKit PHP SDK, you must be using PHP version 5.6.0 or later with JSON PHP Extension and cURL PHP Extension enabled.
Create a new Project
Let's create a dummy project called sample using composer in a folder.
It will prompt a few options. Select defaults by pressing enter.
Install imagekit/imagekit using Composer
Quick Examples
Create an ImageKit Instance
publicKey
andprivateKey
parameters are required as these would be used for all ImageKit API, server-side upload, and generating token for client-side file upload. You can get these parameters from the developer section in your ImageKit dashboard - https://imagekit.io/dashboard/developer/api-keys.urlEndpoint
is also a required parameter. You can get the value of URL-endpoint from your ImageKit dashboard - https://imagekit.io/dashboard/url-endpoints.
URL Generation
File Upload
Response Structure
Following is the response for Server Side File Upload API
Generating URL for rendering images
ImageKit provides inbuilt media storage and integration with external origins. Refer to the documentation to learn more about URL endpoints and external Image origins supported by ImageKit.
Using Image path and image hostname or endpoint
This method allows you to create a URL using the image's path and the ImageKit URL endpoint (urlEndpoint) you want to use to access the image.
Example
Response
Using full image URL
This method allows you to add transformation parameters to an absolute ImageKit-powered URL. This method should be used if you have the absolute URL stored in your database.
Example
Response
The $imageKit->url()
method accepts the following parameters.
Option | Description |
---|---|
urlEndpoint | Optional. The base URL is to be appended before the path of the image. If not specified, the URL Endpoint specified at the time of SDK initialization is used. For example, https://ik.imagekit.io/your_imagekit_id/endpoint/ |
path | Conditional. This is the path on which the image exists. For example, |
src | Conditional. This is the complete URL of an image already mapped to ImageKit. For example, |
transformation | Optional. An array of objects specifying the transformation to be applied in the URL. The transformation name and the value should be specified as a key-value pair in the object. Different steps of a chained transformation can be specified as different objects of the array. The complete List of supported transformations in the SDK and some examples of using them are given later. If you use a transformation name that is not specified in the SDK, it gets applied as it is in the URL. |
transformationPosition | Optional. The default value is |
queryParameters | Optional. These are the other query parameters that you want to add to the final URL. These can be any query parameters and are not necessarily related to ImageKit. Especially useful if you want to add some versioning parameters to your URLs. |
signed | Optional. Boolean. The default value is |
expireSeconds | Optional. Integer. It is used along with the |
Applying Chained Transformations, Common Image Manipulations & Signed URL
This section covers the basics:
The PHP SDK gives a name to each transformation parameter e.g. height
for h
and width
for w
parameter. It makes your code more readable. See the Full list of supported transformations.
👉 If the property does not match any of the available options, it is added as it is.\ e.g
👉 Note that you can also use the h
and w
parameters instead of height
and width
.
For more examples check the demo application.
1. Chained Transformations as a query parameter
Example
Response
2. Image enhancement and color manipulation
Some transformations like Contrast stretch , Sharpen and Unsharp mask can be added to the URL with or without any other value. To use such transforms without specifying a value, specify the value as "-" in the transformation object. Otherwise, specify the value that you want to be added to this transformation.
Example
Response
3. Resizing images
Let's resize the image to a width of 400 and a height of 300. Check detailed instructions on Resize, Crop, and Other Common Transformations
Example
Response
4. Quality manipulation
You can use the Quality Parameter to change quality like this.
Example
Response
5. Adding overlays
ImageKit.io enables you to apply overlays to images and videos using the raw parameter with the concept of layers. The raw parameter facilitates incorporating transformations directly in the URL. A layer is a distinct type of transformation that allows you to define an asset to serve as an overlay, along with its positioning and additional transformations.
Text as overlays
You can add any text string over a base video or image using a text layer (l-text).
For example:
Sample Result URL
Output Image:
Image as overlays
You can add an image over a base video or image using an image layer (l-image).
For example:
Sample Result URL
Output Image:
Solid color blocks as overlays
You can add solid color blocks over a base video or image using an image layer (l-image).
For example:
Sample Result URL
Output Image:
6. Signed URL
Signed URL that expires in 300 seconds with the default URL endpoint and other query parameters. For a detailed explanation of the Signed URL refer to this Official Doc.
Example
Response
You can manage Security Settings from the dashboard to prevent unsigned URLs usage. In that case, if the URL doesn't have a signature ik-s
parameter or the signature is invalid, ImageKit will return a forbidden error instead of an actual image.
List of supported transformations
The complete list of transformations supported and their usage in ImageKit can be found here. The SDK gives a name to each transformation parameter, making the code simpler and readable. If a transformation is supported in ImageKit, but a name for it cannot be found in the table below, use the transformation code from ImageKit docs as the name when using it in the url
function.
Supported Transformation Name | Translates to parameter |
---|---|
height | h |
width | w |
aspectRatio | ar |
quality | q |
crop | c |
cropMode | cm |
x | x |
y | y |
focus | fo |
format | f |
radius | r |
background | bg |
border | b |
rotation | rt |
blur | bl |
named | n |
progressive | pr |
lossless | lo |
trim | t |
metadata | md |
colorProfile | cp |
defaultImage | di |
dpr | dpr |
effectSharpen | e-sharpen |
effectUSM | e-usm |
effectContrast | e-contrast |
effectGray | e-grayscale |
effectShadow | e-shadow |
effectGradient | e-gradient |
original | orig |
raw |
|
Server-side File Upload
The SDK provides a simple interface using the $imageKit->uploadFile()
or $imageKit->uploadFile()
method to upload files to the ImageKit Media Library.
Example
Response
Optional Parameters
Please refer to Server Side File Upload - Request Structure for detailed explanation about mandatory and optional parameters.
ImageKit Media API
The SDK provides a simple interface for all the following Media APIs to manage your files.
1. List and Search Files
This API can list all the uploaded files and folders in your ImageKit.io media library.
Refer to the List and Search File API for a better understanding of the Request & Response Structure.
Example
Applying Filters
Filter out the files with an object specifying the parameters.
Advance Search
In addition, you can fine-tune your query by specifying various filters by generating a query string in a Lucene-like syntax and providing this generated string as the value of the searchQuery
.
Detailed documentation can be found here for Advance Search Queries.
2. Get File Details
This API can get you all the details and attributes of the current version of the file.
Refer to the Get File Details API for a better understanding of the Request & Response Structure.
Example
3. Get File Version Details
This API can get you all the details and attributes for the provided version of the file.versionID
can be found in the following APIs as id
within the versionInfo
parameter:
Refer to the Get File Version Details API for a better understanding of the Request & Response Structure.
Example
4. Get File Versions
This API can get you all the versions of the file.
Refer to the Get File Versions API for a better understanding of the Request & Response Structure.
Example
5. Update File Details
Update file details such as tags, customCoordinates attributes, remove existing AITags, and apply extensions using Update File Details API. This operation can only be performed on the current version of the file.
Refer to the Update File Details API for better understanding about the Request & Response Structure.
Example
6. Add Tags (Bulk) API
Add tags to multiple files in a single request. The method accepts an array of fileIds
of the files and an array of tags
that have to be added to those files.
Refer to the Add Tags (Bulk) API for a better understanding of the Request & Response Structure.
Example
7. Remove Tags (Bulk) API
Remove tags from multiple files in a single request. The method accepts an array of fileIds
of the files and an array of tags
that have to be removed from those files.
Refer to the Remove Tags (Bulk) API for a better understanding of the Request & Response Structure.
Example
8. Remove AI Tags (Bulk) API
Remove AI tags from multiple files in a single request. The method accepts an array of fileIds
of the files and an array of AITags
that have to be removed from those files.
Refer to the Remove AI Tags (Bulk) API for a better understanding of the Request & Response Structure.
Example
9. Delete File API
You can programmatically delete uploaded files in the media library using delete file API.
If a file or specific transformation has been requested in the past, then the response is cached. Deleting a file does not purge the cache. You can purge the cache using Purge Cache API.
Refer to the Delete File API for better understanding about the Request & Response Structure.
Basic Usage
10. Delete File Version API
You can programmatically delete the uploaded file version in the media library using the delete file version API.
You can delete only the non-current version of a file.
Refer to the Delete File Version API for a better understanding of the Request & Response Structure.
Example
11. Delete Files (Bulk) API
Deletes multiple files and their versions from the media library.
Refer to the Delete Files (Bulk) API for a better understanding of the Request & Response Structure.
Example
12. Copy File API
This will copy a file from one folder to another.
If any file at the destination has the same name as the source file, then the source file and its versions (if
includeFileVersions
is set to true) will be appended to the destination file version history.
Refer to the Copy File API for a better understanding of the Request & Response Structure.
Basic Usage
13. Move File API
This will move a file and all its versions from one folder to another.
If any file at the destination has the same name as the source file, then the source file and its versions will be appended to the destination file.
Refer to the Move File API for a better understanding of the Request & Response Structure.
Example
14. Rename File API
You can programmatically rename an already existing file in the media library using Rename File API. This operation would rename all file versions of the file.
The old URLs will stop working. The file/file version URLs cached on CDN will continue to work unless a purge is requested.
Refer to the Rename File API for a better understanding of the Request & Response Structure.
Example
When purgeCache
is set to true
, response will return purgeRequestId
. This purgeRequestId
can be used to get the purge request status.
15. Restore File Version API
This will restore the provided file version to a different version of the file. The newly restored version of the file will be returned in the response.
Refer to the Restore File Version API for a better understanding of the Request & Response Structure.
Example
16. Create Folder API
This will create a new folder. You can specify the folder name and location of the parent folder where this new folder should be created.
Refer to the Create Folder API for a better understanding of the Request & Response Structure.
Example
17. Delete Folder API
This will delete the specified folder and all nested files, their versions & folders. This action cannot be undone.
Refer to the Delete Folder API for a better understanding of the Request & Response Structure.
Example
18. Copy Folder API
This will copy one folder into another.
Refer to the Copy Folder API for a better understanding of the Request & Response Structure.
Example
19. Move Folder API
This will move one folder into another. The selected folder, its nested folders, files, and their versions are moved in this operation.
If any file at the destination has the same name as the source file, then the source file and its versions will be appended to the destination file version history.
Refer to the Move Folder API for a better understanding of the Request & Response Structure.
Example
20. Bulk Job Status API
This endpoint allows you to get the status of a bulk operation e.g. Copy Folder API or Move Folder API.
Refer to the Bulk Job Status API for a better understanding of the Request & Response Structure.
Example
21. Purge Cache API
This will purge CDN and ImageKit.io's internal cache. In response, requestId
is returned, which can be used to fetch the status of the submitted purge request with Purge Cache Status API.
Refer to the Purge Cache API for a better understanding of the Request & Response Structure.
Example
You can purge the cache for multiple files. Check Purge Cache Multiple Files.
22. Purge Cache Status API
Get the purge cache request status using the requestId
returned when a purge cache request gets submitted with Purge Cache API
Refer to the Purge Cache Status API for a better understanding of the Request & Response Structure.
Example
23. Get File Metadata API (From File ID)
Get the image EXIF, pHash, and other metadata for uploaded files in the ImageKit.io media library using this API.
Refer to the Get image metadata for uploaded media files API for a better understanding of the Request & Response Structure.
Example
24. Get File Metadata API (From Remote URL)
Get image EXIF, pHash, and other metadata from ImageKit.io powered remote URL using this API.
Refer to the Get image metadata from remote URL API for a better understanding of the Request & Response Structure.
Example
Custom Metadata Fields API
Imagekit.io allows you to define a schema
for your metadata keys, and the value filled against that key will have to adhere to those rules. You can Create, Read and Update custom metadata rules and update your file with custom metadata value in File update API or File Upload API.
For a detailed explanation, refer to the Custom Metadata Documentaion.
1. Create Fields
Create a Custom Metadata Field with this API.
Refer to the Create Custom Metadata Fields API for a better understanding of the Request & Response Structure.
Example
Check for the Allowed Values In The Schema.
2. Get Fields
Get a list of all the custom metadata fields.
Refer to the Get Custom Metadata Fields API for a better understanding of the Request & Response Structure.
Example
3. Update Fields
Update an existing custom metadata field's label
or schema
.
Refer to the Update Custom Metadata Fields API for a better understanding of the Request & Response Structure.
Example
Check for the Allowed Values In The Schema.
4. Delete Fields
Delete a custom metadata field.
Refer to the Delete Custom Metadata Fields API for a better understanding of the Request & Response Structure.
Example
Utility Functions
We have included the following commonly used utility functions in this SDK.
Authentication parameter generation
If you want to implement client-side file upload, you will need a token
, expiry
timestamp, and a valid signature
for that upload. The SDK provides a simple method that you can use in your code to generate these authentication parameters for you.
The Private API Key should never be exposed in any client-side code. Instead, you must always generate these authentication parameters on the server side.
It will return
Both the token
and expire
parameters are optional. If not specified, the SDK internally generates a random token and a valid expiry timestamp. The value of the token
and expire
used to generate the signature is always returned in the response, whether it is provided as an input to this method or not.
Distance calculation between two pHash values
Perceptual hashing allows you to construct a hash value that uniquely identifies an input image based on the contents of an image. ImageKit.io metadata API returns the pHash value of an image in the response. You can use this value to find a duplicate (or similar) image by calculating the distance between the pHash value of the two images.
This SDK exposes the pHashDistance
function to calculate the distance between two pHash values. It accepts two pHash hexadecimal strings and returns a numeric value indicative of the level of difference between the two images.
Distance calculation examples
Last updated