如何解决此 Laravel 问题 - “/tmp/phpY14gRo”文件不存在或不可读?

How to resolve this Laravel issue - The "/tmp/phpY14gRo" file does not exist or is not readable?

每次通过 POST 方法加载时,我都需要创建 PDF 的缩略图。 一旦我在 Controller 中上传文件,它 运行s getThumb 函数使用 Imagick创建缩略图。问题是每次我这样做时,这个请求都会中断并显示此错误 - “/tmp/phpY14gRo”文件不存在或不可读。

Imagick 已正确安装。我使用 php-7.2-apache docker image.

但是如果我 运行 shell_excec 脚本做完全相同的事情,它就可以工作!这消除了所有对错误依赖安装的怀疑

这是我的控制器的功能:


    public function createThumb($source, $target, $size = 256, $page = 1)
    {

        if (file_exists($source) && !is_dir($source)): // source path must be available and not be a directory
            if (mime_content_type($source) != 'application/pdf'):
                return FALSE;                // source is not a pdf file returns a failure
            endif;

            $sepa = '/';                // using '/' as file separation for nfs on linux.
            $target = dirname($source) . $sepa . $target;
            $size = intval($size);            // only use as integer, default is 256
            $page = intval($page);            // only use as integer, default is 1

            $page--;                    // default page 1, must be treated as 0 hereafter
            if ($page < 0) {
                $page = 0;
            }            // we cannot have negative values


//It breaks exactly right here

            $img = new Imagick($source . "[$page]"); // [0] = first page, [1] = second page


            $imH = $img->getImageHeight();
            $imW = $img->getImageWidth();
            if ($imH == 0) {
                $imH = 1;
            }            // if the pdf page has no height use 1 instead
            if ($imW == 0) {
                $imW = 1;
            }            // if the pdf page has no width use 1 instead

            $sizR = round($size * (min($imW, $imH) / max($imW, $imH))); // relative pixels of the shorter side

            $img->setImageColorspace(255);        // prevent image colors from inverting
            $img->setImageBackgroundColor('white');    // set background color and flatten
            $img = $img->flattenImages();            // prevents black zones on transparency in pdf
            $img->setimageformat('jpeg');

            if ($imH == $imW) {
                $img->thumbnailimage($size, $size);
            }    // square page
            if ($imH < $imW) {
                $img->thumbnailimage($size, $sizR);
            }    // landscape page orientation
            if ($imH > $imW) {
                $img->thumbnailimage($sizR, $size);
            }    // portrait page orientation

            if (!is_dir(dirname($target))) {
                mkdir(dirname($target), 0777, true);
            } // if not there make target directory

            $img->writeimage($target);
            $img->clear();
            $img->destroy();

            if (file_exists($target)) {
                return $target;
            } // return the path to the new file for further processing
        endif;

        return FALSE;    // the source file was not available, or Imagick didn't create a file, so returns a failure
    }

我以为是权限问题,后来发现不是。

更新:

如果我在没有参数的情况下初始化 Imagick,它不会抛出错误,因此不会创建缩略图,因为它没有获取文件路径。因此,每当我添加文件路径并且 PHP 开始搜索该文件时,就会发生错误。在日志中,我注意到 InvalidArgumentException 异常是由 Symfony 框架抛出的。

这是错误的图片:

调试后发现imagick并没有导入到项目中。 所以,我刚刚在 Controller.

的顶部添加了 use Imagick

在我的例子中,我调用了 $validator->fails() 两次。

在我的例子中,在我的控制器操作中第一次调用 $validator->fails(),检查后删除了文件。第二次调用找不到此文件。