Page 1 of 1

One-time project to code PHP merging multiple images into single PDF

Posted: 2015-03-18T13:49:01-07:00
by banjoy
I am working on a personal project for my website which I am coding myself using PHP and need some help writing PHP code for a specific task using the ImageMagick module already installed on my Apache server (hosted by Namecheap). I am unfamiliar with ImageMagick syntax and online documentation is ... lacking.

The specific help I need is this:

From my form, uploading multiple image files (most likely jpg, but could include png, gif or even tiff formats) then merge them together and create a single hi-res pdf file (most likely to be letter size) and save back to the server. (Most likely save images to the server then create the pdf.) Within the pdf file, also include the embedded information (title, author, etc) and set copy and edit permissions within the pdf.

All of this is do-able with ImageMagick (as I understand?) so someone who is very familiar with the module should be able to work with me and write up the code relatively quickly, probably a few hours or half a day or so. My site is live and testing can be done on the live site in a test directory I'll set-up, or I can provide you my raw form to do the testing on your own server, then hand me the code when done. You'd stick with me until I get it stitched into my site.

This is a one-time turn-key project for someone who is thoroughly familiar with PHP and the ImageMagick module.

There is no timeframe and this is *not* a time-sensitive project.

Many thanks in advance.

Re: One-time project to code PHP merging multiple images into single PDF

Posted: 2015-03-18T14:19:19-07:00
by Bonzo
someone who is thoroughly familiar with PHP and the ImageMagick module.
When you say "ImageMagick module" are you talking about Imagick?

Re: One-time project to code PHP merging multiple images into single PDF

Posted: 2015-03-18T16:49:52-07:00
by banjoy
No, I am referring to ImageMagick installed on the Apache Server.

Here is what is on the Apache server, and the info I have on the version installed:

ImageMagick version: ImageMagick 6.7.1-7 2012-03-18 Q16 http://www.imagemagick.org

... which led me to this website.

Re: One-time project to code PHP merging multiple images into single PDF

Posted: 2015-03-18T17:24:15-07:00
by banjoy
I must add that I am *not* familiar with these packages. So I am unclear on the terminology for ImageMagick ...

Here is everything installed:

imagick
imagick module enabled
imagick module version 3.1.2
imagick classes Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator
ImageMagick version ImageMagick 6.7.1-7 2012-03-18 Q16 http://www.imagemagick.org
ImageMagick copyright Copyright (C) 1999-2011 ImageMagick Studio LLC
ImageMagick release date 2012-03-18
ImageMagick number of supported formats: 193


I am searching a a solution to create simple pdf's on the fly...

Thanks!!

Re: One-time project to code PHP merging multiple images into single PDF

Posted: 2015-03-19T01:01:18-07:00
by Bonzo
Imagick is an API built into later versions of php and has limited options and support.

Imagemagick can be run through exec( ) and has access to all the options in your Imagemagick version.

Re: One-time project to code PHP merging multiple images into single PDF

Posted: 2015-03-19T02:53:39-07:00
by banjoy
I am aware of that. As stated in my opening post, I am unfamiliar with the syntax of ImageMagick. The online documentation is sorely lacking. And, I could not find a single online example of coding this with PHP. If I could see an example, I wouldn't have posted here because it would already be done.

If no one is interested in doing this simple code for payment, that's fine. I can move on no hard feelings. But isn't that what this section of the board is for? Consulting? Paid consulting? Perhaps I misunderstood the intent of this section of the forum.

"Do you need consulting from ImageMagick experts and are willing to pay for their expertise? Or are you well versed in ImageMagick and offer paid consulting? If so, post here otherwise post elsewhere for free assistance. "

I wouldn't have posted here if I wasn't willing to pay someone.

I'm looking at fpdf as a solution, the online documentation is great. If someone is interested in doing the simple project with ImageMagick, let me know. If not, that's cool too, I'll have fpdf installed and experimenting with it within a few days as time opens up for me. But I remain open to this solution as well, but am not stuck on ImageMagick. There are other ways to skin this cat LOL.

Again, thanks.

Re: One-time project to code PHP merging multiple images into single PDF

Posted: 2015-03-19T03:42:38-07:00
by Bonzo
I was just trying to clarify your question as if you wanted to use IMagick that could have put people off- it would me anyway.
I have some imagemagick/php examples on my website you can look at to get an idea how to write Imagemagick code with php.

You may have a problem with the "Within the pdf file, also include the embedded information (title, author, etc) and set copy and edit permissions within the pdf.". I do not know if this could be done within Imagemagick and may require using Ghostscript directly. Imagemagick also uses raster images etc. and I do not think you will get a true pdf out; that is one that would scale properly.

If you look at the forum there are about six regular posters and the others just come and go. Those six all tend to have their specialist areas which may not coincide with your requirements.

Good luck with your project anyway.

Re: One-time project to code PHP merging multiple images into single PDF

Posted: 2015-03-19T04:59:06-07:00
by snibgo
I don't know PHP, so can't help there.

Using IM to create PDFs would involve creating one raster image per page. Each raster image could be constructed from JPEGs, text, and so on. Any images would be resampled to fit the page dimensions.

For high-quality PDFs, you would want images to be resampled as little as possible, and text would be retained as text (ie vector) within each PDF page. ImageMagick can't create vector text in PDFs.

I expect other tools are available that can create vector text in PDFs, do automatic pagination, etc. So, although IM may be useful in this project, I doubt that it would be the major tool.

Re: One-time project to code PHP merging multiple images into single PDF

Posted: 2015-03-19T17:37:23-07:00
by banjoy
Many thanks for the replies.

I was able to spend a few hours today with fpdf and got everything going exactly as I envisioned for this portion of my project. I did some experiments with ImageMagick and never could get it to even fire the basic exec commands.

So today I wrote the entire thing with fpdf, which does include the ability to include metadata information.

I think this was just a case where the ImageMagick capabilities are far beyond what I really needed (for now) for my simple implementation and the syntax kind of throws me, but I'll keep it in the back of my mind for future things that may pop up. It does seem to offer very cool effects which I was unaware of until the last few days.

@Bonzo, very cool examples you've posted, nice stuff, I'll remember that for future things as they pop in this crazy mind of mine LOL.

So FWIW, here's the code I wrote today using fpdf. It may be useful to some folks here in that the code will constrain the images to the maximum size based on the largest dimension that fits. Bascially, it will fill the page with the image without clipping w or h. So that part of this code may be useful for someone who needs to do that too. Of course, it would need to be re-tooled for ImageMagick/PHP syntax which I never could wrap my head around...

I've tested this code and it works fine (as written with fpdf I mean).

Thanks again for the replies...

==============
This code reads images from the server, and merges them into a single pdf keeping the image as large as possible within set margins:

Code: Select all

<?php
require($_SERVER['DOCUMENT_ROOT'].'/banjo-glossary/fpdf/fpdf.php');

$pdf = new FPDF('P','pt','Letter');
// THESE VARS WILL BE SET DYNAMICALLY
$pdf->SetTitle('Create PDF test using FPDF',1);
$pdf->SetAuthor('The Banjo Glossary Project',1);
$pdf->SetSubject('Ongoing tests to create pdf files',1);
$pdf->SetCompression(1);

// LETTER size pages
// UNIT IS POINTS, 72 PTS = 1 INCH
$pageW = 612 - 36; // 8.5 inches wide with .25 margin left and right
$pageH = 792 - 36; // 11 inches tall with .25 margin top and bottom
$fixedMargin = 18; // .25 inch
$threshold = $pageW / $pageH;

// IF IMAGE W÷H IS UNDER THRESHOLD, CONSTRAIN THE HEIGHT
// IF IMAGE W÷H IS OVER THRESHOLD, CONSTRAIN THE WIDTH

$readPath = $_SERVER['DOCUMENT_ROOT'].'/banjo-glossary/_temp_/';
$writePath = $_SERVER['DOCUMENT_ROOT'].'/banjo-glossary/';

function sizeImage($thisImage) {
	global $pageW,$pageH,$fixedMargin,$threshold;

	list($thisW,$thisH) = getimagesize($thisImage);
    
    if($thisW<=$pageW && $thisH<=$pageH){
    	// DO NOT RESIZE IMAGE, JUST CENTER IT HORIZONTALLY
        $newLeftMargin = centerMe($thisW);
        $leftMargin = $newLeftMargin;
		return array('leftMargin' => $leftMargin, 'width' => $thisW);
    } else {
        $thisThreshold = $thisW / $thisH;
		if($thisThreshold>=$threshold) {
			$width = $pageW;
			$leftMargin = $fixedMargin;
		} else {
			$thisMultiplier = $pageH / $thisH;
			$width = $thisW * $thisMultiplier;
			$width = round($width, 0, PHP_ROUND_HALF_DOWN);
			// CENTER ON PAGE IF NOT FULL WIDTH
			$newLeftMargin = centerMe($width);
			$leftMargin = $newLeftMargin;
		}
		return array('leftMargin' => $leftMargin, 'width' => $width);
	}
}

function centerMe($thisWidth){
	global $pageW;
	$newMargin = ($pageW - $thisWidth) / 2;
	$newMargin = round($newMargin, 0, PHP_ROUND_HALF_DOWN);
	return $newMargin;
}

// THIS VAR WILL BE POPULATED DYNAMICALLY BUT HARD CORDED FOR THIS EXAMPLE
$imageLIST = array('tab-angeline-the-ba-12739-5552112112010.jpg','tab-at-the-end-of-t-11988-541238102009.jpg','tab-blue-night-(lam-12956-2337161222010.jpg','tab-eighth-of-janua-12698-2650161012010.jpg','tab-foggy-mountain--19894-3131218122013.jpg','tallThin.jpg');

foreach ($imageLIST as $value) {
	$currentImage = $readPath.$value;
	$reSized = sizeImage($currentImage);
	$width = $reSized['width'];
    $leftMargin = $reSized['leftMargin'];
	$pdf->AddPage();
	$pdf->Image($currentImage,$leftMargin,18,$width);

} // LOOP
 
$pdf->Output($writePath.'/TEST-PDFwrite99.pdf','F');

echo 'All done.';
?>