Question

I am using a php script to convert PDF files into images when users upload supporting documentation. It is working great, however ocasionaly it creates 2 near identical files and appends "-0" and "-1". This is throwing me for a loop because my script does not know when this happens and then points to the wrong file name. Any idea what causes this and how to correct would be greatly appreciated.

This is the code:

  $filename = $_FILES['file']['name'];  
  $filename1 = $upload_dir.$_FILES['file']['name'];
  rename($_FILES['file']['tmp_name'], $upload_dir.$_FILES['file']['name']);

  chmod($filename, 0777);

  //If the file is a pdf change it to a jpg.
  $file_array = explode(".", strtolower($filename));
  $file_jpg = $file_array[0];
  $file_jpg = $upload_dir.$file_jpg . ".jpg";
  $file_extn = end(explode(".", strtolower($filename)));

  if($file_extn == 'pdf'){
    $filename3 = substr($filename1, 0, -3);
    $filename3 = $filename3 . "jpg";
    $createjpgpath = $filename3;
    $basefile_jpg = substr($filename, 0, -3) . "jpg";
    exec('convert -geometry 1600x1600 -density 130x130 -quality 20 "'.$filename1.'" "'.$filename3.'"');
    unlink($filename1);

    }
  Else{
    $filename3 = $upload_dir.$_FILES['file']['name'];
    $basefile_jpg = $filename;
    }
Was it helpful?

Solution

This is what I use:

exec('convert "'.$targetFile.'[0]" -flatten -geometry 1600x1600 -density 130x130 -quality 20 "'.$previewTargetFile.'"');

The "[0]" and "-flatten" will flatten the file and output only one image. Some PDF's have multiple pages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top