Converting PDF to JPEG generates ridiculously large /tmp files (using GMAGICK)

49 Views Asked by At

So I have a php script that takes a PDF and converts it to JPEG images. Everything was fine until today, but today I got this PDF, that for some reason cause the generation of very large files in my /tmp directory (like ~14 gigs, while the PDF file in itself is ~18MB).

Here is the PDF in question:

https://www.ciamarket.lt/sites/default/files//failai/leidinys_01.15-28-compressed.pdf

And here is relevant code:

<?php
$userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36';

    $response = $this
        ->httpClient
        ->request(
            'GET',
            $url,
            [
                'headers' => [
                    'User-Agent' => $userAgent,
                ],
            ]
        );

    $contents = $response->getBody()->getContents();

    $tmpFilePath = tempnam(sys_get_temp_dir(), 'brochure_s');

    $tmpFile = fopen($tmpFilePath, 'r+');
    fwrite($tmpFile, $contents);

while (true) {
try {
    $image = new \Gmagick();
    $image->setResolution($width, $height);

    $image->readimage($tmpFilePath . '[' . $i . ']');

    $image->setImageFormat('jpeg');
    $image->setCompressionQuality(75);
    $image->resizeImage($width, $height, \Gmagick::FILTER_LANCZOS, 1, false);

    $imageBlob = $image->getImageBlob();

    $this
       ->uploader
       ->uploadData(
                        $imageBlob,
                        $brochureId,
                        '',
                        'jpg'
    );

    $image->destroy();
} catch (\Throwable $t) {
    break;
}
}

I exctracted the relevant bits from my script.

I think line 28 is responsible for generating the huge file.

Any idea how this could be adressed?

0

There are 0 best solutions below