ImageMagick: Convert PDF to JPG

ImageMagick: Convert PDF to JPG

Posted by: briandichiara
Posted on: 2006-03-29 19:35:00

I'm new to ImageMagick and have no idea what I'm doing. DreamHost told me ImageMagick and GhostScript are installed on my server. All I'm trying to do is convert a PDF to JPEG. After looking through some of the threads on this forum, I've come to the conclusion that it's not that difficult, but it doesn't seem to be working for me. Here's the code I'm using:

Code:
function cmyk2rgb($file) {
  $mgck_wnd = NewMagickWand();
  MagickReadImage($mgck_wnd, $file);

  $img_colspc = MagickGetImageColorspace($mgck_wnd);
  if ($img_colspc == MW_CMYKColorspace) {
    echo "$file was in CMYK format<br />";
    MagickSetImageColorspace($mgck_wnd, MW_RGBColorspace);
  }
}


Code:
if($ext == "pdf"){
  $origfile = $_FILES['upload']['name'];
  $origfile = "files/".$orgdir."/".$origfile;
  $newfile = "new_".$_FILES['upload']['name'];
  $newfile = "files/".$orgdir."/".$newfile;
  // convert file
  exec("convert $origfile $newfile");
  cmyk2rgb($newfile);
}


Well, i've looked at all my variables and they all look right. But when i check the directory, the new file isn't there. I'm not getting any errors either. What is wrong?

I also need a similar method of doing Tif to Jpeg. Haven't tried it yet, but doubt it will work. Am I missing something?

Code:
function tiff2jpg($file) {
  $mgck_wnd = NewMagickWand();
  MagickReadImage($mgck_wnd, $file);

  $img_colspc = MagickGetImageColorspace($mgck_wnd);
  if ($img_colspc == MW_CMYKColorspace) {
    echo "$file was in CMYK format<br />";
    MagickSetImageColorspace($mgck_wnd, MW_RGBColorspace);
  }
  MagickSetImageFormat($mgck_wnd, 'JPG' );
  MagickWriteImage($mgck_wnd, str_replace('.tif', '.jpg', $file));
}



Code:
if($ext == "tif" || $ext == "tiff"){
  //copy file to new name
  $origfile = $_FILES['upload']['name'];
  $origfile = "files/".$orgdir."/".$origfile;
  $newfile = "new_".$_FILES['upload']['name'];
  $newfile = "files/".$orgdir."/".$newfile;
  if (!copy($origfile, $newfile)) {
    $filecopyerror = "true";
  }
  // convert file
  cmyk2rgb($newfile);
  tiff2jpg($newfile);
}

Re: ImageMagick: Convert PDF to JPG

Posted by: ardco
Posted on: 2006-03-29 21:42:00

You didn't explain the need for complexity, so here's a simple approach for the command line:

> convert a PDF to JPEG

convert file.pdf file.jpg

A multi-page pdf converts to a separate jpg per page (or did when I tested).

> Tif to Jpeg

convert file.tif file.jpg

Cheers,

BobS

Re: ImageMagick: Convert PDF to JPG

Posted by: briandichiara
Posted on: 2006-03-30 22:35:00

What I'm trying to do is create a JPEG preview (that's viewable in any browser) of PDF. It can be the exact size/dimensions but it should be at or above 72 dpi and probably needs to be RGB. I've tried the convert command like you have, but it doesn't work and I'm not exactly sure how to produce the error message that's causing it not to convert. Any ideas on why it's not working?

I'm also not sure if I need to specify what folder the file is in since this script will not be in the same folder as the files i'm converting.

Re: ImageMagick: Convert PDF to JPG

Posted by: briandichiara
Posted on: 2006-03-30 23:00:00

I've shortened my code a bit, but still having problems. If I use:

convert $origfile $newfile;

I get the error: Parse error: syntax error, unexpected T_VARIABLE in /home/.galina/the76/mydigitalproof.com/file.php on line 179

And if I use:

exec("convert $origfile $newfile") or die("couldn't convert the $ext");

it just shows me my die message.

This is my code:

$ext = substr(strrchr($_FILES['upload']['name'], "."), 1);
if($ext == "tif" || $ext == "pdf"){
  //copy file to new name
  $origfile = $_FILES['upload']['name'];
  $origfile = "files/".$orgdir."/".$origfile;
  $newfile = substr($origfile, 0, -4).".jpg";
  $newfile = "files/".$orgdir."/".$newfile;
  // convert file
  exec("convert $origfile $newfile") or die("couldn't convert the $ext");
  cmyk2rgb($newfile);
}

I've even tried passthru instead of exec and it still doesn't work.

and my cmyk2rgb function is:

function cmyk2rgb($file) {
  $mgck_wnd = NewMagickWand();
  MagickReadImage($mgck_wnd, $file);

  $img_colspc = MagickGetImageColorspace($mgck_wnd);
  if ($img_colspc == MW_CMYKColorspace) {
    echo "$file was in CMYK format<br />";
    MagickSetImageColorspace($mgck_wnd, MW_RGBColorspace);
  }
}

Why is this so difficult for me? How come this doesn't work?

Re: ImageMagick: Convert PDF to JPG

Posted by: silkrooster
Posted on: 2006-03-31 17:25:00

Try including the directory to the convert program. I beleive it is /usr/bin/convert

Below is an example that I made awhile ago.


<?php
$location='/usr/bin/convert';
$command='-thumbnail 150';
$name='glass.';
$extfrm='jpg';
$extto='png';
$output="{$name}{$extto}";
$convert=$location . ' ' .$command . ' ' . $name . $extfrm . ' ' . $name . $extto;
exec ($convert);
print "<img src=" . $output . ">";
?>

Silk

Re: ImageMagick: Convert PDF to JPG

Posted by: briandichiara
Posted on: 2006-03-31 18:36:00

ok, i'm about to go crazy.

When using this script, i need to reference "convert" according to the folder it is in. Do I need to do the same to the files I'm trying to convert? and if so, should they be like: /home/username/domain.com/files/ or can they just be /files/file.pdf?

I've tried tons of combinations of this and am not having any success. I also tried an ImageMagick command and got an error:

Fatal error: Call to undefined function: newmagickwand() in
/home/mystuff/mydomain.com/file.php on line 85

Which makes me wonder if it or GhostScript is installed and thats why none of this is working. Anyway, thanks for the help.

Re: ImageMagick: Convert PDF to JPG

Posted by: silkrooster
Posted on: 2006-03-31 19:25:00

If I remember right, the script had to be in the same folder as the images. Been awhile, but it seams to me the script would not work unless I did it that way.
Silk

Re: ImageMagick: Convert PDF to JPG

Posted by: briandichiara
Posted on: 2006-03-31 21:03:00

Well, it looks like I may have to install a custom version of PHP that includes GD Library, ImageMagick, MagickWand, and GhostScript to perform the functions I need to perform. There's no way I can run my convert script from the folder that the files are in, because the folder value is dynamic and changes depending on user. Oh well, I'll have to figure this thing out, I guess.

_Brian.

Re: ImageMagick: Convert PDF to JPG

Posted by: silkrooster
Posted on: 2006-03-31 21:56:00

I think the problem is being able to give convert the location of the files. If there was a way to pass this on to the command line of convert, that may help.
I never heard of magickwand so I can't help there.
Silk

Tags: imagemagickdreamhostconclusionthreadsidea