modxcms / revolution

MODX Revolution - Content Management Framework
https://modx.com/
GNU General Public License v2.0
1.36k stars 529 forks source link

Can't create images with phpthumb.class.php from PDF file #15814

Open Morlas805 opened 3 years ago

Morlas805 commented 3 years ago

Bug report

Summary

It's impossible to use extensions like pthumb to create images from a pdf

Step to reproduce

install pthumb and call the snippet like this exemple : [[pthumb? &input=[[+pdf-file]] &debug=1 ]]

Observed behavior

The snippet work with image but with a PDF return the original path of the PDF.

After replacing the folder /core/model/phpthumb with same from an old version of ModX, it works

Expected behavior

The snippet should return the path of an image of the PDF

Environment

MODX version 2.8.3, apache 2.4.48, php 7.4

Ibochkarev commented 3 years ago

@Morlas805 Please attach an example pdf file

Morlas805 commented 3 years ago

It's not with a specific file but with every PDF files

Mark-H commented 3 years ago

After replacing the folder /core/model/phpthumb with same from an old version of ModX, it works

From what version?

Do you have imagick installed on the server?

Morlas805 commented 3 years ago

I have imagick working on the server, I took the folder from a random release : 2.6.5

I tried everything on the server before replacing the folder.

Here is the logs I get in my core/cache/logs/error.log, I think the problem is here : getimagesize(path/to/my/file.pdf)

phpThumb debug output: [0] => phpThumb() v1.7.16-202012161640 in file "phpthumb.class.php" on line 234 [1] => setSourceFilename(path/to/my/file.pdf) set $this->sourceFilename to "path/to/my/file.pdf" in file "phpthumb.class.php" on line 299 [2] => $AvailableImageOutputFormats = array(text;ico;bmp;wbmp;gif;webp;png;jpeg) in file "phpthumb.class.php" on line 1012 [3] => $this->thumbnailFormat set to $this->config_output_format "jpeg" in file "phpthumb.class.php" on line 1023 [4] => $this->thumbnailFormat set to $this->f "jpeg" in file "phpthumb.class.php" on line 1030 [5] => $this->thumbnailQuality set to "75" in file "phpthumb.class.php" on line 1040 [6] => resolvePath: path/to/my/file.pdf (allowed_dirs: Array ( ) ) in file "phpthumb.class.php" on line 1183 [7] => resolvePath: iteration, path=path/to/my/file.pdf, base path = /path/to/my/modx in file "phpthumb.class.php" on line 1202 [8] => resolvePath: stop at component 10 in file "phpthumb.class.php" on line 1218 [9] => resolvePath: stop at path=path/to/my/file.pdf in file "phpthumb.class.php" on line 1221 [10] => resolvePath: path parsed, over in file "phpthumb.class.php" on line 1227 [11] => $this->sourceFilename set to "path/to/my/file.pdf" in file "phpthumb.class.php" on line 933 [12] => SetCacheFilename() _src set from md5($this->sourceFilename) "path/to/my/file.pdf" = "f8f09f2a6fdf596b60996903ff436948" in file "phpthumb.class.php" on line 3616 [13] => SetCacheFilename() _par set from md5(_farC_h300_w200_dpi72_q75) in file "phpthumb.class.php" on line 3655 [14] => starting ExtractEXIFgetImageSize() in file "phpthumb.class.php" on line 3409 [15] => getimagesize(path/to/my/file.pdf) failed in file "phpthumb.class.php" on line 3421 [16] => getimagesize("path/to/my/file.pdf") failed in file "phpthumb.class.php" on line 3451 [17] => $this->useRawIMoutput=true after checking $UnAllowedParameters in file "phpthumb.class.php" on line 1655 [18] => ImageMagickThumbnailToGD() aborting because ImageMagickCommandlineBase() failed in file "phpthumb.class.php" on line 2386 [19] => ImageMagickThumbnailToGD() failed in file "phpthumb.class.php" on line 3458 [20] => SetOrientationDependantWidthHeight() starting with ""x"" in file "phpthumb.class.php" on line 3388 [21] => SetOrientationDependantWidthHeight() setting w="200", h="300" in file "phpthumb.class.php" on line 3404 [22] => EXIF thumbnail extraction: (size=0; type=""; 0x0) in file "phpthumb.class.php" on line 3517 [23] => starting SourceImageToGD() in file "phpthumb.class.php" on line 3781 [24] => starting ImageCreateFromFilename(path/to/my/file.pdf) in file "phpthumb.class.php" on line 3696 [25] => empty $filename or getimagesize(path/to/my/file.pdf) failed in file "phpthumb.class.php" on line 3745 [26] => Populating $rawimagedata in file "phpthumb.class.php" on line 3753 [27] => attempting ImageCreateFromStringReplacement($rawimagedata (8577662 bytes), true) in file "phpthumb.class.php" on line 3767 [28] => ImageCreateFromStringReplacement() failed: unknown fileformat signature " 0x25 0x50 0x44" in file "phpthumb.class.php" on line 4396 [29] => Not using EXIF thumbnail data because $this->exif_thumbnail_data is empty in file "phpthumb.class.php" on line 3853 [30] => $this->gdimg_source is still empty in file "phpthumb.class.php" on line 3905 [31] => ImageMagickThumbnailToGD() failed in file "phpthumb.class.php" on line 3907 [32] => phpThumb() v1.7.16-202012161640

Mark-H commented 3 years ago

This stands out to be in that log:

[28] => ImageCreateFromStringReplacement() failed: unknown fileformat signature " 0x25 0x50 0x44" in file "phpthumb.class.php" on line 4396

Suggests it doesn't recognise the PDF after reading it directly... tried with a PDF from a different source?

Morlas805 commented 3 years ago

We have many PDF from different sources.

Found the code in phpthumb.class.php:4396

    switch (substr($RawImageData, 0, 3)) {
        case 'GIF':
            $ICFSreplacementFunctionName = 'imagecreatefromgif';
            break;
        case "\xFF\xD8\xFF":
            $ICFSreplacementFunctionName = 'imagecreatefromjpeg';
            break;
        case "\x89".'PN':
            $ICFSreplacementFunctionName = 'imagecreatefrompng';
            break;
        default:
            $this->DebugMessage('ImageCreateFromStringReplacement() failed: unknown fileformat signature "'.phpthumb_functions::HexCharDisplay(substr($RawImageData, 0, 3)).'"', __FILE__, __LINE__);
            return false;
            break;
    }

Looks like PDF are no longer supported in new version.

Jako commented 3 years ago

PDF is supported by phpThumb. But the method ImageCreateFromStringReplacement code looks like that it is used only with GD installations. So I think the ImageMagick detection of phpThumb seems not to work in your installation or there is a degression in the current code of phpThumb.class.php (https://github.com/JamesHeinrich/phpThumb/commits/5f84ff92fe675d5418caa1f669d0c6d8e3e2ace0/phpthumb.class.php)

JoshuaLuckers commented 3 years ago

@Morlas805 would it be possible to use the native phpThumb library to generate the PDF's and see if it yields the same results? This way we can rule out if it's something we can fix or that we need to fix it upstream.

Sandman89 commented 10 months ago

$commandline = $this->ImageMagickCommandlineBase(); add quick fix with the following line $commandline = $this->ImageMagickCommandlineBase(); if ($this->isPDFfile()){ $commandline = 'convert'; } isPDFfile() - return true if extension = pdf