PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 19, 2022

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

 September 19, 2022     docker, imagick, laravel, php     No comments   

Issue

I need to create PDF's thumbnail every time it gets loaded via POST method. Once I upload the file inside Controller, it runs getThumb function that uses Imagick to create thumbnail. The problem is that everytime I do that, this request breaks and shows this error - The "/tmp/phpY14gRo" file does not exist or is not readable..

Imagick is properly installed. I use php-7.2-apache docker image.

But if I run shell_excec script that does absolutely the same thing, it works! That eliminates all suspicions of the wrong dependency installation

Here's the function from my controller:


    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
    }

I thought that it was permission problems but found out that it's not.

Update:

If I initialize Imagick without parameters it won't throw errors and thus won't create thumbnail as it doesn't get file path. So, whenever I add file path and PHP starts searching for that file, and the error occurs. Inside the log, I noticed that the InvalidArgumentException exception was thrown by a Symfony framework.

Here's an image of the error: enter image description here


Solution

After debugging I found out that Imagick was not imported into the project. So, I just added use Imagick at the top of my Controller.



Answered By - Sanzhar Dan
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing