How can the orientation of images be corrected when converting PDF to JPG using Imagick in PHP?

When converting PDF to JPG using Imagick in PHP, the orientation of images may not be correct due to the way the PDF is structured. To correct this, you can use the setImageOrientation() method in Imagick to set the correct orientation before converting the PDF to JPG.

// Load the PDF file
$pdf = new Imagick('input.pdf');

// Set the correct orientation
$pdf->setImageOrientation(imagick::ORIENTATION_TOPLEFT);

// Convert the PDF to JPG
$pdf->setImageFormat('jpg');
$pdf->writeImages('output.jpg', false);

// Clear the Imagick object
$pdf->clear();
$pdf->destroy();