Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6147135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:03:39+00:00 2026-05-23T19:03:39+00:00

I have the following code. It’s used to combine various image attachments (and pdfs)

  • 0

I have the following code. It’s used to combine various image attachments (and pdfs) into one PDF. For some reason, when I take even a single PDF and put it through the code, the end result comes out looking very bad compared to the original PDF. In addition, I can select text in the source PDF, but in the generated one I cannot.

Any help would be greatly appreciated.

// PDF object
$pdf = new Imagick();
$max_resolution = array('x' => 100, 'y' => 100);

foreach($attachment_ids as $attachment_id) {
    $attachment = DAO_Attachment::get($attachment_id);
    $file = Storage_Attachments::get($attachment);
    // Temporarily store our attachment
    $im = new Imagick();
    $im->readImageBlob($file);
    // We need to reset the iterator otherwise only one page will be rotated
    $im->resetIterator();

    // Get the resolution
    $resolution = $im->getImageResolution();
    if($resolution['x'] > $max_resolution['x']) {
        $max_resolution['x'] = $resolution['x'];
    }
    if($resolution['y'] > $max_resolution['y']) {
        $max_resolution['y'] = $resolution['y'];
    }

    $num_pages = $im->getNumberImages();

    $rotation = array_shift($rotations);
    $degrees = $rotation > 0 ? 360 - $rotation : 0;
    $pages = array();

    if($degrees > 0) {
        // Rotate each page
        for($i = 1; $i <= $num_pages; $i++) {
            $im->nextImage();
            $im->rotateImage(new ImagickPixel(), $degrees);
        }
    }

    // We need to reset the iterator again so all of our pages will be added to the pdf
    $im->resetIterator();

    // If the image format isn't a pdf, convert it to a png
    if($im->getImageFormat !== 'pdf') {
        $im->setImageFormat('png');
        // Opacity
        if(method_exists($im, 'setImageOpacity'))
            $im->setImageOpacity(1.0);
    }

    $im->setImageCompression(imagick::COMPRESSION_LOSSLESSJPEG); 
    $im->setImageCompressionQuality(100);
    $im->stripImage();

    // Add the rotated attachment to the PDF
    $pdf->addImage($im);

    // Free
    $im->destroy();
}

// Create a composite
$pdf->setImageFormat('pdf');

// Compress output
$pdf->setImageCompression(imagick::COMPRESSION_LOSSLESSJPEG); 
$pdf->setImageCompressionQuality(100);
$pdf->stripImage();

// Set resolution
$pdf->setImageResolution($max_resolution['x'], $max_resolution['y']);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:03:40+00:00Added an answer on May 23, 2026 at 7:03 pm

    It turns out the answer to this is to set the DPI using setResolution(). We do this before using readImageBlob() to get read the file containing our image, as it will change the DPI of the image based on the current resolution (so setting it afterwards won’t work).

    You could also use some math and use resampleImage() to do it after the fact, but setResolution() seems to be working perfectly for us.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code <div class=img> <img src=image/1.jpg class=img_s style=display:none; /> <img src=image/2.jpg class=img_s
I have following code on my site: backgroundImages[bg_img_path_b]=new Image(); backgroundImages[bg_img_path_b].src = bg_img_path_b; backgroundImages[bg_img_path_b].loaded=loading; //jQuery(backgroundImages[lastImage]).unbind('load.backgroundImages');
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I'm trying to use opengl in C#. I have following code which fails with
I have the following code in a web.config file of the default IIS site.
I have the following code: $bind = new COM(LDAP://CN=GroupName,OU=Groups,OU=Division,DC=company,DC=local); When I execute it from
I have the following code snippet. $items['A'] = Test; $items['B'] = Test; $items['C'] =
I have the following code: SELECT <column>, count(*) FROM <table> GROUP BY <column> HAVING
I have the following code: String inputFile = somefile.txt; FileInputStream in = new FileInputStream(inputFile);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.