0

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?

Kebab Programmer
  • 1,213
  • 2
  • 21
  • 36
galdikas
  • 1,609
  • 5
  • 19
  • 43
  • What $width and $height are you using? I'd try lowering them and see if it makes a difference – Tudor Jan 21 '19 at 09:17
  • $width = 1059; $height = 1497; Yeah it does make a difference. But it kind of has to be about this size. But regardless of size... even at these dimensions why would it generate file of this size? I mean what kind of image should take up 14GIGS? – galdikas Jan 21 '19 at 09:18
  • How many pages does the PDF have? If it has hundreds of pages and Gmagick tries to load the entire file, it could create 14GB worth of data, even at that resolution. – Tudor Jan 21 '19 at 09:23
  • no... it's like 10 pages. – galdikas Jan 21 '19 at 10:10

0 Answers0