composer require pixelfactory/psd-php
Create an instance of the 'Psd' class by passing the file path.
require_once '../vendor/autoload.php';
$psd = new \Psd\Psd('./image.psd');
Then you have two ways to use the library, 'simple' and 'professional'
Simple - way is suitable for those who are not familiar with the structure of the psd file and just want to get the necessary information
Professional - way can be used by more experienced developers to get access to a specific part of the file
$psd = new \Psd\Psd('./image.psd');
$psdSimpleMethods = $psd->getShortcuts();
echo $psdSimpleMethods->getWidth(); // Print file width
echo $psdSimpleMethods->getHeight(); // Print file height
$psd = new \Psd\Psd('./image.psd');
$psdSimpleMethods = $psd->getShortcuts();
var_dump($psdSimpleMethods->savePreview('./out.png')); // Print 'true' if file be saved
// TODO
// TODO
// TODO
// TODO
The psd class has the same structure as the psd file.
Name | Method | Examples |
---|---|---|
File header | getHeader | Link |
Color mode data | -¹ | |
Image resources | getResources | Link |
Layer and mask information | getLayers | Link |
Image data | getImage | Link |
1 - 'Color mode data' has no method because it is skipped and not processed by the library. This should not affect the work with most images because they have the "rgb" or "cmyk" color mode. This section is used only in the "Indexed" or "Duotone" color mode.
You can call the 'getHeader' method to get class implements HeaderInterface what contains methods for all fields image header section.
File header section | HeaderInterface methods |
---|---|
Signature | |
Version | getVersion |
Reserved | - |
Channels | getChannels |
height | getRows (Alias: getHeight) |
width | getCols (Alias: getWidth) |
Depth | getDepth |
Color mode | getMode (Convert mode number to text: modeName) |
- | parse |
- | getNumPixels |
- | getChannelLength |
- | getFileLength |
Example:
echo $psd->getHeader()->getMode(); // Return file mode (int)
echo $psd->getHeader()->modeName(); // Return file mode name
echo $psd->getHeader()->getChannels(); // Return file count channels
Image resources section store additional information. Such as guides, etc.
The library is working with resources:
- Guides(1032)
- Layer Comps(1065)
- Resolution Info(1005)
The full list of resources you can be found in the documentation
To find the necessary resource, you need to call the method getResources (this method return class what extends from ResourcesInterface).
Next, you can use the search by the resource name or resource id.
Example. Get guides:
/** @var \Psd\FileStructure\Resources\Resource\Guides\GuidesData[] $guides */
$guides = $psd
->getResources()
->getResourceById(\Psd\FileStructure\Resources\Resource\ResourceBase::RESOURCE_ID_GUIDES)
->getData();
foreach ($guides as $guide) {
printf("%s - %s\n", $guide->getDirection(), $guide->getLocation()); // Result: 'vertical - 100'
}
// TODO
This section stores the image. You can get a class for exporting an image using the method getExporter.
Now is available only png class for export image:
/* @var Psd\Image\ImageExport\Exports\Png $exporter */
$exporter = $psd->getImage()->getExporter(\Psd\Image\ImageExport\ImageExport::EXPORT_FORMAT_PNG);
All exporters classes implements interface: ImageExportInterface
You can export the image to the Imagick class or save it.
/** @var Imagick $image */
$image = $exporter->export();
/** @var bool $status */
$status = $exporter->save('./out.png');