andreas n
andreas n

Reputation: 1

imagick wrong color profile for eps to jpg

wrong colors, these become e.g. high contrast green. tried to use ghostscript to attach the colors, but then it leads to wrong canvas dimension.

enter image description here

enter image description here

// Function to generate thumbnail from EPS using Imagick
// this code works fine but generates wrong colors
function generateThumbnailFromEps($epsFile, $outputPath) {
    try {
        $imagick = new Imagick();

        // Set the resolution before reading the EPS file
        $imagick->setResolution(300, 300);

        // Read the EPS file
        $imagick->readImage($epsFile);

        // Flatten image to remove transparency and set background to white
        $imagick->setImageBackgroundColor('white');
        $imagick = $imagick->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);

        // Ensure colorspace is RGB to fix color issues
        $imagick->transformImageColorspace(Imagick::COLORSPACE_RGB);

        // Resize the image while maintaining the aspect ratio
        $originalWidth = $imagick->getImageWidth();
        $originalHeight = $imagick->getImageHeight();
        $newWidth = 1280;
        $newHeight = ($newWidth / $originalWidth) * $originalHeight;
        $imagick->resizeImage($newWidth, $newHeight, Imagick::FILTER_LANCZOS, 1);

        // Create a canvas with the same size as the resized image (no extra padding)
        $canvas = new Imagick();
        $canvas->newImage($newWidth, $newHeight, new ImagickPixel('white'));

        // Composite the resized image directly onto the canvas
        $canvas->compositeImage(
            $imagick,
            Imagick::COMPOSITE_OVER,
            0, 0  // Place image at the top-left corner (no padding)
        );

        // Set image format and save the output
        $canvas->setImageCompression(Imagick::COMPRESSION_JPEG);
        $canvas->setImageCompressionQuality(96);

if ($canvas->getImageColorspace() !== Imagick::COLORSPACE_SRGB) {
    // Convert to sRGB color space
$canvas->transformImageColorspace(Imagick::COLORSPACE_SRGB);
}

        $canvas->setImageFormat('jpg');
        $canvas->writeImage($outputPath);


        // Clean up resources
        $imagick->clear();
        $canvas->clear();
        $imagick->destroy();
        $canvas->destroy();

      //  echo "Generated thumbnail: $outputPath\n";
    } catch (Exception $e) {
      //  echo "Failed to generate thumbnail from EPS: " . $e->getMessage() . "\n";
    }
}

i tried it with ghostscript and imagick alone only. i have to use a canvas because some eps delivered are a mess and the reolution(s) are always different too.

// Step 1: Use Ghostscript to convert the EPS to a color-corrected PNG (RGB color profile)
// this code if set first, sets the correct colors but mess up the image dimension and generated white-space on top and right
        $tempFile = sys_get_temp_dir() . '/' . uniqid('temp_image_', true) . '.png';

        // Ghostscript command to convert EPS to PNG and apply color profile correction (RGB)
        $gsCommand = "gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m "
                   . "-dColorConversionStrategy=/sRGB -dOverrideICC "
                   . "-sOutputFile=" . escapeshellarg($tempFile) . " "
                   . "-r300 " . escapeshellarg($epsFile);

        // Execute Ghostscript command
        exec($gsCommand, $output, $returnCode);`

Upvotes: 0

Views: 20

Answers (0)

Related Questions