I need to convert all PDFs file of a directory into jpgs.
Like:
1.pdf
2.pdf
3.pdf
into
1.jpg
2.jpg
3.jpg
With this script I can find all PDFs.
$dir_name = "";
$pdfs = glob($dir_name."*.pdf");
foreach($pdfs as $pdf) {
echo $pdf;
}
But how can I convert multiple files?
$imagick = new Imagick();
$imagick->setResolution(300, 300);
$imagick->readImage('???');
$imagick->writeImages('???', false);
Related
I have written a function in PHP 7.4 that generates a thumbnail of the first page of a PDF document. The function works as intended for most PDF files.
However, some PDFs get cropped when I generate the thumbnail. Additionally, sometimes, I don't get the first page.
This PDF triggers both problems:
http://www.teavigoinfo.com/pdf/study-27.pdf
function PDF2Thumbnail($url) {
$image = #file_get_contents($url);
if($image) {
$im = new Imagick();
$im->readImageBlob($image);
$im->setIteratorIndex(0);
$im->thumbnailImage(300, 0);
$im->setImageFormat('png');
$im = $im->flattenImages();
header('Content-Type: image/png');
echo $im;
}
}
Thank you.
I have an eps file and I want it to convert svg or png format.
I have installed PHP Imagick extension in my localhost and also installed the GhostScript.
I have tried the following code
for eps to svg code
$file_path = realpath('./sample.eps');
$dest_path = getcwd() . '/sample.svg';
$command = "inkscape --file=$file_path --export-plain-svg=$dest_path";
$output = shell_exec($command);
var_dump($output);
var_dump(is_file('./sample.svg'));
this code return null and bool(false)
for eps to png code
$path = 'http://localhost/Sample.eps';
$save_path = 'http://localhost/imprint_option_2E_c.png';
$image = new Imagick();
$image->setResolution(300,300);
$image->readimage($path);
$image->setBackgroundColor(new ImagickPixel('transparent'));
$image->scaleImage(600, 270);
$image->setImageFormat("png");
$image->writeImage($save_path);*/
error is : Uncaught ImagickException: Failed to read the file
I'm trying to create a pdf thumbnail with Imagick and save it on the server in the same location as the pdf. The code below works fine as is. The problem is that I don't want to echo the image. But if I remove the echo statement, the resulting jpg file contains errors and is unreadable. How can I create the thumbnail and write to a file without sending it to the browser?
$pdfThumb = new \imagick();
$pdfThumb->setResolution(10, 10);
$pdfThumb->readImage($filePath . $fileName . $fileExt . '[0]');
$pdfThumb->setImageFormat('jpg');
header("Content-Type: image/jpeg");
echo $pdfThumb;
$fp = fopen($filePath . $fileName . '.jpg', "x");
$pdfThumb->writeImageFile($fp);
fclose($fp);
DaGhostman Dimitrov provided some helpful code on #16606642, but it doesn't work for me for some reason.
I would try:
$pdfThumb = new imagick();
$pdfThumb->setResolution(10, 10);
$pdfThumb->readImage($filePath . $fileName . $fileExt . '[0]');
$pdfThumb->setImageFormat('jpg');
$fp = $filePath . $fileName . '.jpg';
$pdfThumb->writeImage($fp);
Bonzo's answer requires imagick on the webserver. If imagick is not on the webserver you can try to execute imagemagick from the commandline by php command exec():
exec('convert -thumbnail "178^>" -background white -alpha remove -crop 178x178+0+0 my_pdf.pdf[0] my_pdf.png')
And if you like to convert all pdfs in one step from same folder where your script is located try this:
exec('for f in *.pdf; do convert -thumbnail "178^>" -background white -alpha remove -crop 178x178+0+0 "$f"[0] "${f%.pdf}.png"; done');
In this examples I create a png thumbnail 178x178 pixel from the first page (my_pdf.pdf[0] the 0 means first pdf page).
I am basically all the way there. I am unsure how to push the temporary pdf object to imagick to generate and return the jpeg image. Using mPDF's standard output(), I get the pdf rendered to the screen:
$mpdf=new mPDF();
$mpdf->WriteHTML($output);
$img = new Imagick($mpdf->output());
$img->setResolution(300,300);
$img->resampleImage(150,150,imagick::FILTER_UNDEFINED,1);
$img->resizeImage(512,700,Imagick::FILTER_LANCZOS,0);
$img->setImageFormat('jpeg');
$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$img->writeImage ("test.jpeg");
Under this scenario I get the pdf outputed to the screen instead of a jpg image returned/created. What is the proper way to pass the pdf into imagick?
I think you need: $mpdf->Output('temp/'.$filename.'.pdf','F');
$mpdf->Output('temp/'.$filename.'.pdf','F');
$pdf_file = 'temp/'.$filename.'.pdf';
$savepath = 'temp/'.$filename.'.jpg';
$img = new imagick($pdf_file);
$img->setImageFormat('jpg');
$img->writeImage($savepath);
echo "<img src='temp/$filename.jpg' />";
http://phpform.net/pdf-to-image.php
So what I'm trying to do is:
- given an image url -> convert image to png
- zip resulting png
I have the following code which successfully does the conversion and zipping (I'm going to expand it later to test the extension to auto convert formats):
$file = "../assets/test.jpg";
$img = imagecreatefromjpeg($file);
imagePng($img, "files/temp.png" );
$zip->addFile( "files/temp.png", "test.png" );
What I want to know is, is it possible to do this without creating a copy of image before it's zipped
See ZipArchive::addFromString().
$file = "../assets/test.jpg";
// capture output into the internal buffer
ob_start();
$img = imagecreatefromjpeg($file);
imagepng($img);
// get contents from the buffer
$contents = ob_get_clean();
$zip = new ZipArchive();
$zip->open('archive.zip', ZipArchive::CREATE);
// and put them in the zip file...
$zip->addFromString('name_in_the_zip.png', $contents);