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 5964433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:25:53+00:00 2026-05-22T19:25:53+00:00

I have a PHP script to create a PNG thumbnail of a PDF file

  • 0

I have a PHP script to create a PNG thumbnail of a PDF file as follows:

<?php
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("png");
$im->resizeImage(200,200,1,0);
// start buffering
ob_start();
$thumbnail = $im->getImageBlob();
$contents =  ob_get_contents();
ob_end_clean();    
echo "<img src='data:image/jpg;base64,".base64_encode($thumbnail)."' />";    
?>

Which returns a thumbnail, but the background is transparent. I want to set white background color (change the alpha layer to white). How can I do this?

  • 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-22T19:25:54+00:00Added an answer on May 22, 2026 at 7:25 pm

    I’m doing something similar, although I’m actually writing the image to the disk –
    When I used your direct output, it worked and I got the actual color from the PDF.

    Through a bit of debugging, I figured that the issue was actually related to the

    imagick::resizeImage()
    

    function.
    For whatever reason, when you set all of your colors, compression, etc. the resizeImage adds the black background.
    My solution is to use GD for the resizing, so that I can have a full dynamic resize – Since you’re not interested in such thing, I would simply use the image sampling functionality. Your code should be like this:

    <?php
    $file ="test.pdf";
    $im = new imagick(realpath($file).'[0]');
    $im->setCompression(Imagick::COMPRESSION_JPEG);
    $im->setCompressionQuality(80);
    $im->setImageFormat("jpeg");
    $im->sampleImage(200,200);
    // start buffering
    ob_start();
    $thumbnail = $im->getImageBlob();
    $contents =  ob_get_contents();
    ob_end_clean();    
    echo "<img src='data:image/jpg;base64,".base64_encode($thumbnail)."' />";    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script where I create a new process using proc_open().It executes
I have a very simple PHP script which creates favicon.ico form jpg/gif/png uploaded file.
I have a script that parse the database and create php classes to work
I have a PHP script that creates a thumbnail and lists an image gallery.
I have a PHP script that uses GD2 to create an image. It uses
I have a PHP script that creates an xml file from the db. I
I have a PHP script that processes file uploads. The script tries to organise
I have a php script, test.php that has the contents <?php require_once(classes/user.php); echo test;
hi guys how to create a png image for inserting in pdf file by
I have php script that creates a temporary watermark image for users that are

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.