Search code examples
phpphp-7imagickphp-7.4

Imagick function that works on one server and doesn't work on another


I have a problem moving my script from one server to one other.

The PHP script read a PDF file and create an image for each page of the pdf. This script currently works in an environment that has these characteristics

  • PHP version: 7.4.8
  • Imagick version: 3.4.4

Now I have moved the script to an environment that has these characteristics:

  • PHP version: 7.4.28
  • Imagick version: 3.7.0

The code is:

$img = new Imagick($percorsoBaseDir); //LINE OF THE ERROR
$img->setResolution(600, 600);
$num_pages = $img->getNumberImages();
$img->setImageCompressionQuality(100);
$images = NULL;
for ($i; $i < $num_pages; $i++)
{
   ..... [My script] ....
}

And the error is:

AH01071: Got error 'PHP message: PHP Fatal error: Uncaught ImagickException: Failed to read the file in /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/custom_plugin/includes/woocommerce/Step_2.php:222\nStack trace:\n#0 /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/custom_plugin/includes/woocommerce/Step_2.php(222): Imagick->readImage()\n#1 /var/www/vhosts/mysite.com/httpdocs/wp-includes/shortcodes.php(356): step_2_function()\n#2 [internal function]: do_shortcode_tag()\n#3 /var/www/vhosts/mysite.com/httpdocs/wp-includes/shortcodes.php(228): preg_replace_callback()\n#4 /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/js_composer/include/helpers/helpers.php(247): do_shortcode()\n#5 /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/js_composer/include/templates/shortcodes/vc_column_text.php(31): wpb_js_remove_wpautop()\n#6 /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/js_composer/include/classes/shortcodes/core/class-wpbakeryshortcode.php(451): require('/var/www/...', 

I also tried throwing:

getcwd() //to verify that the file exists

and:

is_readable ($ pathBaseDir) //to verify that the file exist and is readable
is_writable ($ pathBaseDir) //to verify that the file exist and is writable

and they both return TRUE.

I also checked that the file loaded correctly in the file system and that I could open it from the browser.

I can't understand what can be the error, has something similar happened to you? How could I solve?

Thank you!


Solution

  • I find the problem: I have installed the Ghost Script in my server with this command:

    apt-get install ghostscript 
    

    And now it works!