Search code examples
phpsymfonyimagick

Change permissions or change tmp directory for imagick on Php dev server win 10


I'm developing a php7.1 app on a windows 10 pc running symfony dev server but everytime i run a script to convert a svg to png i receive this error.

WriteBlob Failed `/tmp/5f49ac095abe6.png' @ error/png.c/MagickPNGErrorHandler/1630

I just bought this computer and the script works on my old windows laptops. I did research and suspect this is a permissions issue but I exhausted all means to change php and imagick tmp storage location.

If it is indeed a permissions issue then how can i update where php/imagick saves tmp files.

here is my entire method for reference:

private function createImageFromSVG(ConfigLogo $logoConfig)
{
    $svg = $logoConfig->getDefaultImage()->getContent();
    $idColorArray = [
        'first' => $logoConfig->getFirstColor(),
        'second' => $logoConfig->getSecondColor(),
        'third' => $logoConfig->getThirdColor(),
    ];

    foreach ($idColorArray as $state => $color) {
        if ($color != 'FFFFFF') {
            $svg = preg_replace(
                '/class="' . $state . '" style="fill:#([0-9a-fA-F]{6})/',
                'class="' . $state . '" style="fill:#' . $color,
                $svg
            );
        }
    }

    $fileName = sprintf('%s.png', uniqid());
    $filePath = sprintf('/tmp/%s', $fileName);
    $this->filePath = $filePath;

    $im = new \Imagick();
    $im->setBackgroundColor(new \ImagickPixel('transparent'));
    $im->readImageBlob($svg);
    $im->setImageFormat("png24");
    $im->writeImage($filePath);  //error !

    $file = new UploadedFile($filePath, $fileName .'.png');
    $im->clear();
    $im->destroy();

    $oldImages  = $logoConfig->getRenderedLogoImages();
    foreach ($oldImages as $image) {
        if ($image->getImageName() !== 'default_logo.png') {
            $this->uploadHandler->remove($image, 'imageFile');
        }
        $logoConfig->removeRenderedLogoImage($image);
    }

    $renderedImage = new ConfigLogoDefaultImage();
    $renderedImage->setImageFile($file);
    $renderedImage->setImageName($fileName);
    $logoConfig->addRenderedLogoImage($renderedImage);
    $this->operateImage($logoConfig, 'RenderedLogo');
}

The error occurs at line $im->writeImage($filePath);

Thanks in advance for any help with this.


Solution

  • Whoever might be facing this issue it was solved after I created the tmp directory at C:/tmp. Apparently imagick doesn’t use the systems tmp directory set in system properties or php.ini.