Page 1 of 1

Converting PDF and -path variable!

Posted: 2011-05-18T08:16:52-07:00
by fRAiLtY-
Hi guys,

Currently I'm using ImageMagick to convert PDF's into jpegs with the aim of creating 2 jpegs on the fly, 1 a regular sized jpeg and 1 a thumbnail.

Here's where I am currently. This code below successfully creates jpegs of all the pages in the pdf ($targetFile) and renames them with the jpeg extension. Instead of saving those outputted files in the same directory I'd like to put them in an already created directory within the working directory called "thumbs".

Code: Select all

$image = exec("convert " . $targetFile . "  " . substr_replace($targetFile , 'jpg', strrpos($targetFile , '.') +1));
This creates one jpeg of each page in the pdf and saves it correctly beside the PDF. I want it saved in a subdirectory, I've tried using "-path" as per the manual but it has no effect in my use, and there's no example! That's the first step!

The next step is to get it to create 2 jpegs on the fly, one large one small, saving the large ones alongside the PDF and the small in "thumbs" directory.

Can anyone help please? I'm relatively familiar with PHP and I'm pretty sure this an ImageMagick "problem" more so than PHP as it's working, just not going where I want it!

Cheers.

Re: Converting PDF and -path variable!

Posted: 2011-05-18T09:39:26-07:00
by Bonzo
Here are a couple of ways to do what you want ( copied from my website notes ); I would probably go for the second method.
I just write the relative path to save to in the filename.

This method saves the tempory image into the memory; you can call the image whatever you like - in this case its called image.
Once the image is saved into the memory you can call it as many times as you like.

Code: Select all

<?php 
$cmd = "input.jpg -write mpr:image +delete ".
" \( mpr:image -thumbnail x480 -write 480_wide.jpg \) ".
" \( mpr:image -thumbnail x250 -write 250_wide.jpg \) ".
" \( mpr:image -thumbnail x100 -write 100_wide.jpg \) ".
" \( mpr:image -thumbnail 64x64! -write 64_square.jpg \) ".
" \( mpr:image -colorspace Gray -write black_white.jpg \)";
exec("convert $cmd ");
?> 
This method is "cloning" the image and using it multiple times

Code: Select all

<?php 
$cmd = " input.jpg \( +clone -thumbnail x480 -write 480_wide.jpg +delete \)".
" \( +clone -thumbnail x250 -write 250_wide.jpg +delete \) ".
" \( +clone -thumbnail x100 -write 100_wide.jpg +delete \) -thumbnail 64x64! ";
exec("convert $cmd 64_square.jpg ");
?> 
So far I have not found a way to use the tempory images in the memory once you have left exec( ); the image should still be in memory as shell examples I have seen can still access them.

Re: Converting PDF and -path variable!

Posted: 2011-05-18T19:36:30-07:00
by anthony
Bonzo wrote:

Code: Select all

<?php 
$cmd = "input.jpg -write mpr:image +delete ".
" \( mpr:image -thumbnail x480 -write 480_wide.jpg \) ".
" \( mpr:image -thumbnail x250 -write 250_wide.jpg \) ".
" \( mpr:image -thumbnail x100 -write 100_wide.jpg \) ".
" \( mpr:image -thumbnail 64x64! -write 64_square.jpg \) ".
" \( mpr:image -colorspace Gray -write black_white.jpg \)";
exec("convert $cmd ");
?> 
Couple of mistakes in the above.
  • You MUST have a a final outptu file name even if it is just "null:" meaning to junk the images
  • -write does not delete the image, so at the end you have 5 images in memory!
  • If writing and deleteing the images you only have the working image in memory, as such no need for parenthesis

Code: Select all

$cmd = "input.jpg -write mpr:image ".
"           -thumbnail x480 -write 480_wide.jpg +delete ".
" mpr:image -thumbnail x250 -write 250_wide.jpg +delete ".
" mpr:image -thumbnail x100 -write 100_wide.jpg +delete ".
" mpr:image -thumbnail 64x64! -write 64_square.jpg +delete ".
" mpr:image -colorspace Gray -write black_white.jpg ";
exec("convert $cmd  NULL:");
Adding an example like the above as an alternative method in IM Examples, File Handling, Writing Images Multiple Times
http://www.imagemagick.org/Usage/files/#write

NOTE this type of processing technique will likely be much more common once I have finished the planned replacement for "convert" as part of IM v7 development. This new command which will support not only command line, but file scripts, and even pipeline image operations streams.
This method is "cloning" the image and using it multiple times

Code: Select all

<?php 
$cmd = " input.jpg \( +clone -thumbnail x480 -write 480_wide.jpg +delete \)".
" \( +clone -thumbnail x250 -write 250_wide.jpg +delete \) ".
" \( +clone -thumbnail x100 -write 100_wide.jpg +delete \) -thumbnail 64x64! ";
exec("convert $cmd 64_square.jpg ");
?> 
This is also my preference, especially as by removing the deletes you can (if you want) access intermediate image processing results. However at least this one has the final write image!


this was the primary example for IM Examples, File Handling, Writing Images Multiple Times
http://www.imagemagick.org/Usage/files/#write

However also see examples of this, plus various debugging line inserts you can use, in IM Examples, Basics, Complex Image Processing and Debugging
http://www.imagemagick.org/Usage/basics/#complex

So far I have not found a way to use the tempory images in the memory once you have left exec( ); the image should still be in memory as shell examples I have seen can still access them.[/quote]

Re: Converting PDF and -path variable!

Posted: 2011-05-18T23:56:44-07:00
by Bonzo
Thanks for pointing out my errors Anthony; I will update my example on the website and check out your links.

Re: Converting PDF and -path variable!

Posted: 2011-05-19T00:48:44-07:00
by fRAiLtY-
Hi, thanks for the replys.

So my "image" that I'm inputting is called $targetFile, but also the output is the same name just with a JPEG extension, which is this:

Code: Select all

substr_replace($targetFile , 'jpg', strrpos($targetFile , '.') +1);
I want to convert all pages of that $targetFile (likely to be a PDF) into jpegs, store 1 set of large jpegs in the same folder as the PDF and store a smaller set in thumbs/ subdirectory.

I think I'm getting confused between PHP and IM commands so can someone tell me what I would write to achieve the above?

Re: Converting PDF and -path variable!

Posted: 2011-05-19T02:50:09-07:00
by Bonzo
I tend to do any php work outside the IM command and use the variable within it.

Code: Select all

$large = substr_replace($targetFile , 'jpg', strrpos($targetFile , '.') +1);
$thumbnail = "thumbs/".$thumbnail;

$cmd = " $targetFile \( +clone -write $large +delete \) -thumbnail 64x64 $thumbnail ";
exec("convert $cmd ");

Re: Converting PDF and -path variable!

Posted: 2011-05-19T04:04:30-07:00
by fRAiLtY-
Hi, thanks again.

Is that second line right?

I ask because on executing that code it creates 1 jpeg of the first page, and no thumbs in the thumbs directory.

Also the thumbnails would need _thumb.jpg adding on the end of the filename.

What about this?

Code: Select all

       $large = substr_replace($targetFile , 'jpg', strrpos($targetFile , '.') +1);
		$thumbnail = "thumbs/".substr($large, 0, -4)."_thumb".strtolower(substr($large, -4))
		
		$cmd = " $targetFile \( +clone -write $large +delete \) -thumbnail 64x64 $thumbnail ";
		exec("convert $cmd ");
Don't mean to question it, just doesn't look right but I'm no pro.

Cheers.

Re: Converting PDF and -path variable!

Posted: 2011-05-19T07:45:13-07:00
by Bonzo
You can do the php part how you like - its not quite the way I would do it but the Imagemagick part should work. I assume the thumbnail folder has been CHMOD to 777 or 755.

Using this method you can echo the variables $large, $thumbnail, $cmd and see what they contain.

Re: Converting PDF and -path variable!

Posted: 2011-05-19T08:30:06-07:00
by fRAiLtY-
Hi Bonzo,

I've echo'd out my variables to check and they contain what I'd expect them to contain, however it only creates one jpeg in the main directory, nothing in the thumbs directory, hence why I questioned the ImageMagick part?

Using my code:

$targetFile = test.pdf
$large = test.jpg
$thumbnail = thumbs/test_thumb.jpg

If I do an echo to see what $cmd is I've got:

Code: Select all

convert test.pdf \( +clone -write test.jpg +delete \) -thumbnail 64x64 thumbs/test_thumb.jpg
..which looks right, no errors in the error log either.

So the variables are working exactly as I'd want, just not producing the images :( Also it only creates an image of the first page, presumably [0] needs to be inserted in the command somewhere? Like this?

Code: Select all

$cmd = " $targetFile[0] \( +clone -write $large +delete \) -thumbnail 64x64 $thumbnail ";
Both the main directory and the thumbs directory are CHMOD 755.

Cheers.

Re: Converting PDF and -path variable!

Posted: 2011-05-19T09:38:06-07:00
by Bonzo
Thats strange; using a pdf file called map2009.pdf I end up with 9 pages starting from map2009-0.jpg in the folder with the original pdf and 8 pages in the thumbs folder starting with map2009_thumb-0.jpg

I get the last page twice in the first folder for some reason :?

See if you get any errors with this:

Code: Select all

$large = substr_replace($targetFile , 'jpg', strrpos($targetFile , '.') +1);
$thumbnail = "thumbs/".substr($large, 0, -4)."_thumb".strtolower(substr($large, -4));
      
$cmd = " $targetFile \( +clone -write $large +delete \) -thumbnail 64x64 $thumbnail ";
$array=array();
echo "<pre>";
exec("convert $cmd  2>&1", $array); 
echo "<br>".print_r($array)."<br>";
echo "</pre>"; 
This will only give you the first page:

Code: Select all

$cmd = " $targetFile[0] \( +clone -write $large +delete \) -thumbnail 64x64 $thumbnail ";
Are you using Windows or Linux ? Try removing the \ from ( and ) my memory is crap at the moment and I can not remember wether its Linux or windows that needs to escape the ( & )

Re: Converting PDF and -path variable!

Posted: 2011-05-19T16:37:55-07:00
by anthony
fRAiLtY- wrote:

Code: Select all

convert test.pdf \( +clone -write test.jpg +delete \) -thumbnail 64x64 thumbs/test_thumb.jpg
Just a small short cut... the parenthesis in the above is equivalent to +write, which internally makes a clone to ensure that the -write does not modify the image (for example when writing GIF, whcih usally needs color reduction, or color quantization)

Code: Select all

convert test.pdf +write test.jpg -thumbnail 64x64 thumbs/test_thumb.jpg
However as both images are saved to jpg direct writing even without an internal clone should be fine..

Code: Select all

convert test.pdf -write test.jpg -thumbnail 64x64 thumbs/test_thumb.jpg

Re: Converting PDF and -path variable!

Posted: 2011-05-19T16:41:47-07:00
by anthony
Bonzo wrote:Thats strange; using a pdf file called map2009.pdf I end up with 9 pages starting from map2009-0.jpg in the folder with the original pdf and 8 pages in the thumbs folder starting with map2009_thumb-0.jpg
Your PDF is multi-image file. JPEG can not save mutliple images to one file, so IM automatically does the equivelent of a +adjoin to create multiple files (one file per image).

See IM Examples, Writing Mutli-Image Sequences
http://www.imagemagick.org/Usage/files/#adjoin
This and teh next section lets you control how multiple images are output.

If you want the first page of the PDF , the read only one page using 'test.pdf[0]'

Re: Converting PDF and -path variable!

Posted: 2011-05-20T01:27:21-07:00
by fRAiLtY-
anthony wrote: However as both images are saved to jpg direct writing even without an internal clone should be fine..

Code: Select all

convert test.pdf -write test.jpg -thumbnail 64x64 thumbs/test_thumb.jpg
Hi,

I've just tried this and no luck. I am getting the right result on the first set of jpegs as they are coming out correctly, all pages, in the right directory with the right name. However, when navigating to the thumbs directory it's empty still, and the error log shows no signs of errors. I've attached images:

ImageImage

Both directories are created the same way with the same permissions (755). To help out I've attached my whole PHP script. It's an upload script which takes the file, creates folders puts it in those folders and then does the actions based on what filetype it is. I've also tried commenting out 90% of the script so all I'm left with is the "pdf" part and no luck, still just 1 set of images.

Code: Select all

<?php

if (!empty($_FILES)) {
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
	$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
	
	// $fileTypes  = str_replace('*.','',$_REQUEST['fileext']);
	// $fileTypes  = str_replace(';','|',$fileTypes);
	// $typesArray = split('\|',$fileTypes);
	// $fileParts  = pathinfo($_FILES['Filedata']['name']);
	
	// if (in_array($fileParts['extension'],$typesArray)) {
		// Uncomment the following line if you want to make the directory if it doesn't exist
		 mkdir(str_replace('//','/',$targetPath), 0755, true);
		 mkdir($targetPath . "thumbs", 0755, true); // create thumbs dir
		 	
		move_uploaded_file($tempFile,$targetFile);
		echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
	// } else {
	// 	echo 'Invalid file type.';
	// }
	   
}

$imgsize = getimagesize($targetFile);
switch(strtolower(substr($targetFile, -3))){
    case "pdf":
        $large = substr_replace($targetFile , 'jpg', strrpos($targetFile , '.') +1);
		$thumbnail = "thumbs/".substr($large, 0, -4)."_thumb".strtolower(substr($large, -4));		
		$cmd = "$targetFile -write $large -thumbnail 64x64 $thumbnail ";
		exec("convert $cmd "); // here's where I'm processing the above command, I've echo'd this out in a test file and it's exactly the same as above?
		exit;
    break;
    case "jpg":
        $image = imagecreatefromjpeg($targetFile);    
    break;
    case "png":
        $image = imagecreatefrompng($targetFile);
    break;
    case "gif":
        $image = imagecreatefromgif($targetFile);
    break;
    default:
        exit;
    break;
}

$width = 60; //New width of image    
$height = $imgsize[1]/$imgsize[0]*$width; //This maintains proportions

$src_w = $imgsize[0];
$src_h = $imgsize[1];
    

$picture = imagecreatetruecolor($width, $height);
imagealphablending($picture, false);
imagesavealpha($picture, true);
$bool = imagecopyresampled($picture, $image, 0, 0, 0, 0, $width, $height, $src_w, $src_h); 

if($bool){
    switch(strtolower(substr($targetFile, -3))){
        case "jpg":
            //header("Content-Type: image/jpeg");
            $bool2 = imagejpeg($picture,$targetPath."thumbs/".substr($_FILES['Filedata']['name'], 0, -4)."_thumb".strtolower(substr($targetFile, -4)));
        break;
        case "png":
            //header("Content-Type: image/png");
            imagepng($picture,$targetPath . substr($_FILES['Filedata']['name'], 0, -4)."_thumb".strtolower(substr($targetFile, -4)));
        break;
        case "gif":
            //header("Content-Type: image/gif");
            imagegif($picture,$targetPath . substr($_FILES['Filedata']['name'], 0, -4)."_thumb".strtolower(substr($targetFile, -4)));
        break;
    }
}

imagedestroy($picture);
imagedestroy($image);

echo '1'; // Important so upload will work on OSX

?>
Is there anyway of producing a verbose output of what's going on in this file? The file is a "silent" file as it's an upload process file, i.e. it never displays in the browser, just called as the result of a form so if I navigate to it directly I just get a load of errors, as you'd expect.

Cheers!

Re: Converting PDF and -path variable!

Posted: 2011-05-20T03:30:26-07:00
by Bonzo
I had to remove the first if { } as I do not have the upload form and manualy create the thumbs directory probably due to the way my server is setup. Apart from that both lots of files were created.