grantiago
grantiago

Reputation: 175

Creating a function in PHP

I use this often and would like to turn it into a function:

$f = fopen('../images/snotel/'. $name .'.pdf','w+');
fwrite($f, $pdf);
fclose($f);
$conv='/usr/bin/convert../images/snotel/'. $name .'.pdf ../images/snotel/'. $name .'.jpg'; 
system ($conv); 

This is what I've tried but it doesn't seem to work:

function pdf2jpg($name)
{
    $f = fopen('../images/snotel/'. $name .'.pdf','w+');
    fwrite($f, $pdf);
    fclose($f);
    $conv='/usr/bin/convert../images/snotel/'. $name .'.pdf ../images/snotel/'. $name .'.jpg'; 
    system ($conv);
}

...

pdf2jpg('wsr');

Upvotes: 0

Views: 243

Answers (1)

DaveRandom
DaveRandom

Reputation: 88647

As it is, your function tries to write the file with no data in the $pdf variable, because you did not pass it in.

You need to do one of two things:

This version takes the PDF data as an argument and creates the file in the function:

function pdf2jpg ($pdf, $name)     {
  $f = fopen('../images/snotel/'.$name.'.pdf','w');
  fwrite($f,$pdf);
  fclose($f);
  $conv = '/usr/bin/convert ../images/snotel/'.$name.'.pdf ../images/snotel/'.$name.'.jpg'; 
  //run 
  system($conv);
}

// Usage
pdf2jpg($pdf, 'wsr');

This version just takes the name, assuming that the file already exists:

function pdf2jpg ($name)     {
  $conv = '/usr/bin/convert ../images/snotel/'.$name.'.pdf ../images/snotel/'.$name.'.jpg'; 
  //run 
  system ($conv);
}

// Usage
$name = 'wsr';
$f = fopen('../images/snotel/'.$name.'.pdf','w');
fwrite($f,$pdf);
fclose($f);

pdf2jpg($name);

Upvotes: 1

Related Questions