create a pdf preview from images and add 66% noise each page

Posted on 2008-06-12
Last Modified: 2008-11-14
I have a php page where people can preview documents that they can purchase if they want to.

The preview should consist of one image file per document page. each image should show the top 33% of the page. below that there should be black noise with tex on there ("Preview")

Could someone provide a code example on how to do add the 66% noise ?
This is the code I have created so far :




	public static function createPreview($_file, $_noise_image) {

		// the preview

		$preview_file = dirname($_file) . "/" . "preview_" .  basename($_file);		

		// create handle for new PDF document

		$pdf_handler = pdf_new();		

		// open a file

		pdf_open_file($pdf_handler, $preview_file);

		// insert the previews

		$images = new Imagick($_file);


		foreach ($images as $k => $image) {

			$preview_jpeg =  dirname($_file) . "/" . "tmp_page" . $k . '_.jpg';			



			// start a new page (A4)

			pdf_begin_page($pdf_handler, 595, 842);

			$pdfimage = pdf_load_image($pdf_handler, "jpeg", $preview_jpeg, "");	

			pdf_place_image($pdf_handler, $pdfimage, 0, 0, 1);		


			// place the noise image : TODO


			// end page (A4)


			// clean





		// close and save file



		return $preview_file;


Open in new window

Question by:SWB-Consulting
  • 3

Expert Comment

Comment Utility
I've written up an example here of how to use ImageMagick to get, effect, and then combine two images with ImageMagick. The effect is actually quite neat. In my example I use a simple blur, but you can change it to add noise as well. I've included samples for both (one commented out).

When it comes to noise, I found the settings below the only workable ones for "noise". I liked the blur better.

In your case, you'd use something like this snippit right before these lines:
                        // start a new page (A4)


// Get original size of image

$height = $image->getImageHeight();

$width = $image->getImageWidth();

// We need an overlay ~66% of the image

$o_height = floor(2*($height) / 3);

// Grab region of the image and make into a new Imagick class

$im_overlay = $image->getImageRegion($width, $o_height, 0, $height - $o_height);

// Do obfuscating effect.



// Overlay images

$image->compositeImage($im_overlay, imagick::COMPOSITE_OVER, 0, $height - $o_height);

// Write out image, go on.



// start a new page (A4)


Open in new window


Expert Comment

Comment Utility
I almost forgot the text overlay. Just add this after:
$image->compositeImage($im_overlay, imagick::COMPOSITE_OVER, 0, $height - $o_height);

and before
// Write out image, go on.
// Do preview text

$draw = new ImagickDraw();

// Set font soze


// Align text to center


// Initialize color class

$white = new ImagickPixel();


// Set text color


// Draw text in middle of image (within 66%)

$draw->annotation(floor($width / 2), floor($height / 2), 'PREVIEW');

// Overlay text


Open in new window


Author Comment

Comment Utility
excellent solution and comments, thanks

Accepted Solution

Xorlev earned 500 total points
Comment Utility
Glad to help. If you found it useful, please accept the solution so I can stay on the site. :)

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
This article discusses four methods for overlaying images in a container on a web page
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now