PHP code example of html-css-to-image / client
1. Go to this page and download the library: Download html-css-to-image/client library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
html-css-to-image / client example snippets
use HtmlCssToImage\HtmlCssToImageClient;
use HtmlCssToImage\Request\CreateHtmlCssImageRequest;
use HtmlCssToImage\Response\CreateImageSuccessResponse;
st(
html: '<h1>Hello, world!</h1>',
css: 'h1 { color: royalblue; }',
),
);
if ($result instanceof CreateImageSuccessResponse) {
echo $result->id . ' ' . $result->url;
} else {
echo $result->error;
}
$client = HtmlCssToImageClient::fromEnvironment();
use HtmlCssToImage\Request\CreateTemplatedImageRequest;
use HtmlCssToImage\Request\CreateUrlImageRequest;
use HtmlCssToImage\ImageFormat;
$urlResult = $client->createImage(
new CreateUrlImageRequest(
url: 'https://example.com',
viewportWidth: 1200,
viewportHeight: 630,
transparentBackground: true,
format: ImageFormat::WEBP,
),
);
$templateResult = $client->createImage(
new CreateTemplatedImageRequest(
templateId: 'your-template-id',
templateVersion: 3,
templateValues: ['title' => 'Hello from PHP'],
),
);
$result = $client->createImageBatch(
variations: [
new CreateHtmlCssImageRequest(css: 'h1 { color: crimson; }'),
new CreateHtmlCssImageRequest(css: 'h1 { color: royalblue; }'),
],
defaultOptions: new CreateHtmlCssImageRequest(
html: '<h1>Shared HTML</h1>',
viewportWidth: 600,
viewportHeight: 315,
),
);
$templateUrl = $client->generateTemplatedImageUrlFromValues(
'your-template-id',
['title' => 'Rendered on demand'],
templateVersion: 2,
);
$renderUrl = $client->generateCreateAndRenderUrl(
new CreateUrlImageRequest(
url: 'https://example.com/card/42',
viewportWidth: 1200,
viewportHeight: 630,
),
);
use HtmlCssToImage\ImageFormat;
use HtmlCssToImage\Request\CreateTemplatedImageRequest;
use HtmlCssToImage\Render\RenderImageOptions;
$options = new RenderImageOptions(format: ImageFormat::WEBP, width: 1200, height: 630);
$templateUrl = $client->generateTemplatedImageUrl(
new CreateTemplatedImageRequest(
templateId: 'your-template-id',
templateValues: ['title' => 'Rendered on demand'],
templateVersion: 2,
),
$options,
);
$url = $client->imageUrl(
'image-id',
new RenderImageOptions(format: ImageFormat::JPG, dpi: 96, width: 1200),
);
use HtmlCssToImage\Render\RenderImageCrop;
use HtmlCssToImage\Render\RenderImageCropPosition;
use HtmlCssToImage\Render\RenderImageCropSpan;
$crop = RenderImageCrop::rectangle(
horizontal: RenderImageCropSpan::between(
RenderImageCropPosition::percent(10),
RenderImageCropPosition::percent(90),
),
);
$url = $client->imageUrl('image-id', new RenderImageOptions(crop: $crop));
$single = $client->deleteImage('image-id');
$batch = $client->deleteImageBatch(['image-id-1', 'image-id-2']);
use GuzzleHttp\Client as GuzzleClient;
$httpClient = new GuzzleClient([
'timeout' => 90,
'http_errors' => false,
]);
$client = new HtmlCssToImageClient(
apiId: 'your-api-id',
apiKey: 'your-api-key',
httpClient: $httpClient,
);
use Psr\Http\Client\NetworkExceptionInterface;
use HtmlCssToImage\Response\ApiErrorResponse;
try {
$result = $client->createImage($request);
} catch (NetworkExceptionInterface $exception) {
// Apply application-specific policy.
}
if (isset($result) && $result instanceof ApiErrorResponse) {
echo "HTTP {$result->statusCode}: {$result->error}";
foreach ($result->validationErrors ?? [] as $error) {
echo "{$error->path}: {$error->message}";
}
}