Last active
April 3, 2020 10:33
-
-
Save vuthaihoc/345ece3a4ff92ca7b5a06a441677b20d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
<<<CONFIG | |
packages: | |
- "symfony/process:^5.0" | |
CONFIG; | |
/** | |
* Created by PhpStorm. | |
* User: hocvt | |
* Date: 2020-04-03 | |
* Time: 11:13 | |
*/ | |
use Symfony\Component\Process\Process; | |
class PdfToImageConverter { | |
public static $bin = 'pdftocairo'; | |
protected $process; | |
protected $page = 1; | |
protected $width = 600; | |
protected $quality = 100; | |
protected $url; | |
protected $path; | |
protected $resource; | |
protected function __construct() { | |
} | |
protected function validProcess( Process $process ) { | |
$status = $process->getExitCode(); | |
$error = $process->getErrorOutput(); | |
if ( $status !== 0 and $error !== '' ) { | |
throw new \RuntimeException( | |
sprintf( | |
"The exit status code %s says something went wrong:\n stderr: %s\n stdout: %s\ncommand: %s.", | |
$status, | |
$error, | |
$process->getOutput(), | |
$process->getCommandLine() | |
) | |
); | |
} | |
} | |
protected function getInput() { | |
if ( $this->resource ) { | |
return $this->resource; | |
} | |
if ( $this->path ) { | |
return fopen( $this->path, "r" ); | |
} | |
if ( $this->url ) { | |
return file_get_contents( $this->url ); | |
} | |
} | |
////// CREATOR /////// | |
public static function fromFile( $path ): PdfToImageConverter { | |
$instance = new self(); | |
$instance->path = $path; | |
return $instance; | |
} | |
public static function fromUrl( $url ): PdfToImageConverter { | |
$instance = new self(); | |
$instance->url = $url; | |
return $instance; | |
} | |
public static function fromResource( $resource ): PdfToImageConverter { | |
$instance = new self(); | |
rewind( $resource ); | |
$instance->resource = $resource; | |
return $instance; | |
} | |
////// CONFIG /////// | |
public function page( $index ): PdfToImageConverter { | |
$this->page = $index; | |
return $this; | |
} | |
public function width( $width ): PdfToImageConverter { | |
$this->width = $width; | |
return $this; | |
} | |
////// GET RESULT /////// | |
public function toJpeg( $path = '' ) { | |
$command = [ | |
self::$bin, | |
"-jpeg", | |
"-f", | |
$this->page, | |
"-l", | |
$this->page, | |
"-singlefile", | |
"-antialias", | |
"fast", | |
"-scale-to", | |
"800", | |
"-", | |
"-", | |
]; | |
$process = new Process( $command ); | |
$process->setInput( $this->getInput() ); | |
$process->run(); | |
$this->validProcess( $process ); | |
if ( $path ) { | |
return file_put_contents( $path, $process->getOutput() ); | |
} | |
return $process->getOutput(); | |
} | |
} | |
chdir("."); | |
echo "working dir : " . __DIR__ . "\n"; | |
$url = "https://www.phdfood2019.it/wp-content/uploads/2019/05/APA_Guide_2017.pdf"; | |
PdfToImageConverter::fromUrl( $url )->page(2)->toJpeg(__DIR__ . "/output.jpg"); | |
echo "Page 2 saved to " . __DIR__ . "/output.jpg"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$pdf = \Storage::disk(DiskPathInfo::getBestDisk( $disk ))->readStream( $path );
PdfToImageConverter::$bin = "/usr/local/bin/pdftocairo";
$png = PdfToImageConverter::fromResource( $pdf )->toJpeg();
return response($png, 200, ['Content-type' => 'image/jpeg']);