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);
}