Statamic GraphQL image Transforms
Working on a project we required the ability to have image transforms while using Statamic and GraphQL
In the AppServiceProvider.php boot method add the following code:
GraphQL::addField(
'AssetInterface', 'transform', function () {
return [
'type' => GraphQL::string(),
'args' => [
'width' => [
'type' => GraphQL::int(),
],
'height' => [
'type' => GraphQL::int(),
],
'orientation' => [
'type' => GraphQL::string()
]
],
'resolve' => function ($entry, $args) {
$image = $entry->id();
if (!$image) {
return null;
}
$url = Image::manipulate($image);
if($args['width'])
{
$url->width($args['width']);
}
if($args['height'])
{
$url->height($args['height']);
}
if($args['orientation'])
{
$url->orientate($args['orientation']);
}
return URL::makeAbsolute($url->build());
}];
}
);
You will now find you can add:
thumb: transform(width: 400)
We are planning on extending this a bit and unless added to the standard Statamic Asset GraphQL releasing it as a free package to include in your installation.