Current time: 04-17-2014, 05:53 AM Hello There, Guest! (LoginRegister)

Post Reply 
ImageMagick: Convert PDF to JPG
03-29-2006, 08:35 PM
Post: #1
ImageMagick: Convert PDF to JPG
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);
}
Visit this user's website Find all posts by this user
Quote this message in a reply
03-30-2006, 11:35 PM
Post: #2
ImageMagick: Convert PDF to JPG
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.
Visit this user's website Find all posts by this user
Quote this message in a reply
03-31-2006, 12:00 AM
Post: #3
ImageMagick: Convert PDF to JPG
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?
Visit this user's website Find all posts by this user
Quote this message in a reply
03-31-2006, 06:25 PM
Post: #4
ImageMagick: Convert PDF to JPG
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
Visit this user's website Find all posts by this user
Quote this message in a reply
03-31-2006, 07:36 PM
Post: #5
ImageMagick: Convert PDF to JPG
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.
Visit this user's website Find all posts by this user
Quote this message in a reply
03-31-2006, 08:25 PM
Post: #6
ImageMagick: Convert PDF to JPG
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
Visit this user's website Find all posts by this user
Quote this message in a reply
03-31-2006, 10:03 PM
Post: #7
ImageMagick: Convert PDF to JPG
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.
Visit this user's website Find all posts by this user
Quote this message in a reply
03-31-2006, 10:56 PM
Post: #8
ImageMagick: Convert PDF to JPG
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
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: