Solved

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

Posted on 2008-06-12
5
583 Views
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';			
			$image->setImageFormat("jpg");	
			$image->writeImage($preview_jpeg);	
			// 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)
			pdf_end_page($pdf_handler);		
			// clean
			@unlink($preview_jpeg);
			//$image->clear();
			//$image->destroy();
		}
		// close and save file
		pdf_close($pdf_handler);
		//
		return $preview_file;
	}

Open in new window

0
Comment
Question by:SWB-Consulting
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 3

Expert Comment

by:Xorlev
ID: 21776893
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).

http://us.php.net/manual/en/function.imagick-blurimage.php
http://us.php.net/manual/en/function.imagick-addnoiseimage.php

When it comes to noise, I found the settings below the only workable ones for "noise". I liked the blur better.
imagick::NOISE_IMPULSE
imagick::NOISE_GAUSSIAN
imagick::NOISE_LAPLACIAN
imagick::NOISE_POISSON

In your case, you'd use something like this snippit right before these lines:
                        $image->setImageFormat("jpg");  
                        $image->writeImage($preview_jpeg);      
                        // start a new page (A4)

Enjoy.

// 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.
$im_overlay->blurImage(5,6);
//$im_overlay->addNoiseImage(imagick::NOISE_LAPLACIAN);
 
// Overlay images
$image->compositeImage($im_overlay, imagick::COMPOSITE_OVER, 0, $height - $o_height);
 
// Write out image, go on.
$image->setImageFormat("jpg");  
$image->writeImage($preview_jpeg);      
// start a new page (A4)
 
?>

Open in new window

0
 
LVL 3

Expert Comment

by:Xorlev
ID: 21776948
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
$draw->setFontSize(52);
 
// Align text to center
$draw->setTextAlignment(imagick::ALIGN_CENTER);
 
// Initialize color class
$white = new ImagickPixel();
$white->setColor('white');
 
// Set text color
$draw->setFillColor($white);
 
// Draw text in middle of image (within 66%)
$draw->annotation(floor($width / 2), floor($height / 2), 'PREVIEW');
 
// Overlay text
$image->drawImage($draw);

Open in new window

0
 

Author Comment

by:SWB-Consulting
ID: 21782373
excellent solution and comments, thanks
0
 
LVL 3

Accepted Solution

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 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