This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
rendering html to png (server-side)
I have a DIV element with some images positioned within the DIV element. I want to make a JPG or a PNG file out of that, so people can save it for their Facebook timeline cover image. I found a LOT of scripts and tutorials, but nothing really fills my needs. This can't really be a very hard thing to do?
I already got my host to install iMagick.
You should get the positions via JavaScript (I guess you are using js to set the positions of the images in the div) and send them to a PHP-Script. With help of the PHP GD manual you can easily generate a png oder jpeg image.
You can try
WKHTMLTOIMAGE
I had to achieve the same thing, only for something else than Facebook and I used webkit2png
I save the HTML code on a temporary HTML file, then I run the python webkit2png command on that temporary HTML file which converts it to PNG. It requires xvfb (apt-get install xvfb)
I then use the following command in PHP:
exec('xvfb-run --server-args="-screen 0, 1024x768x24" python webkit2png.py --log=/tmp/webkit2pnglog.log --output=' .$fullUploadPath . ' ' . $fullPath);
Where $fullUploadPath is the location of the target file and $fullPath is the location of the temporary HTML file.
Related
I have a site with about 1500 JPEG images, and I want to compress them all. Going through the directories is not a problem, but I cannot seem to find a function that compresses a JPEG that is already on the server (I don't want to upload a new one), and replaces the old one.
Does PHP have a built in function for this? If not, how do I read the JPEG from the folder into the script?
Thanks.
you're not telling if you're using GD, so i assume this.
$img = imagecreatefromjpeg("myimage.jpg"); // load the image-to-be-saved
// 50 is quality; change from 0 (worst quality,smaller file) - 100 (best quality)
imagejpeg($img,"myimage_new.jpg",50);
unlink("myimage.jpg"); // remove the old image
I prefer using the IMagick extension for working with images. GD uses too much memory, especially for larger files. Here's a code snippet by Charles Hall in the PHP manual:
$img = new Imagick();
$img->readImage($src);
$img->setImageCompression(Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(90);
$img->stripImage();
$img->writeImage($dest);
$img->clean();
You will need to use the php gd library for that... Most servers have it installed by default. There are a lot of examples out there if you search for 'resize image php gd'.
For instance have a look at this page http://911-need-code-help.blogspot.nl/2008/10/resize-images-using-phpgd-library.html
The solution provided by vlzvl works well. However, using this solution, you can also overwrite an image by changing the order of the code.
$image = imagecreatefromjpeg("image.jpg");
unlink("image.jpg");
imagejpeg($image,"image.jpg",50);
This allows you to compress a pre-existing image and store it in the same location with the same filename.
Note: PDF files would be uploaded by user.
I am making a document sharing website(pdf). I don't want users to be able to copy/select the pdf file text. I am looking for a nice PDF/Image Document viewer. Currently using the following method:
I am using imagemagick to convert PDF file to Jpeg. But the process is too slow even after disabling OpenMP. I was looking forward to convert full PDF to different image files but decided to just convert the first image to a thumbnail because the process is so slow.
I am using it on my local machine and it takes more than 30 seconds to convert 40-50 pages in good quality. So if there are pdf files more than 100 pages, this process would ruin the user experience.
Is there any way to convert PDF to Image files?
Also is there a way to let this process (pdf to image) happen asynchronously? Like the user filling out file details while the pdf is being uploaded and converted, something like YT videos?
Enter the command in exec() function, this will help you out
<?php
$pdf_url = 'testfile.pdf'; // Url of the PDF
$image_name = 'output.jpg'; // Set the image name
exec('convert "'.$pdf_url.'" -colorspace RGB -resize 800 "'.$image_name.'"', $output, $return_var);
?>
Hope this helps you
¿You use a linux sever?
Just use convert command and put [0] sufix to pdf file for save a cover:
exec("convert '/home/sample.pdf[0]' '/home/test.png'");
This is an odd question but I'm stuck on how I would achieve this and I am unable to find any methods of doing so.
I have a simple php script that takes variables (containing file names) from the URL, cleans then and then uses them to generate a single image from the inputted values. This works fine and outputs a new png to the webpage using:
imagepng($img);
I also have a facebook sharing script in PHP that takes a filepath as an input and then shares the image on the users feed where this statement is used to define the image variable:
$photo = './mypic.png'; // Path to the photo on the local filesystem
I don't know how I can link these two together though. I would like to use my generation script as the image to share.
Can anyone point me in the right direction of how to do this? I am not the master of PHP so go easy please.
-Tim
UPDATE
If it helps, here are the links to the two pages on my website containing the outputs. They are very ruff mind you:
The php script generating the image:
http://the8bitman.herobo.com/Download/download.php?face=a.png&color=b.png&hat=c.png
The html page with the img tag:
http://the8bitman.herobo.com/Share.html
Treat it as a simple image:
<img src="http://yourserve/yourscript.php?onlyImage=1" />
yourscript.php
if($_GET['onlyimage']) {
header('Content-type:image/png'); //or your image content type
//print only image
} else {
//print image and text too
}
I want to make PDFs from external URLs searchable. I'm using pdftotext from XPDF.
It's working fine with PDFs already on my webspace, but I keep getting an error message when trying to use external PDFs instead. Specifically I get:
"Error: Couldn't open file 'https://www.vericoa.com/sandbox/test2.pdf' "
Here is my code
$path = 'https://www.vericoa.com/sandbox/test2.pdf';
echo shell_exec('pdftotext -enc UTF-8 '.$path.' pdf.txt 2>&1');
$file = file_get_contents('pdf.txt');
echo $file;
Is it even possible to extract text from external PDF sources? Are there any alternatives (I spent the last hours searching, but found nothing).
Thanks in advance
Matthias
You could perhaps try downloading the external URL in php, saving it to a file and passing that to the pdftotext script?
How can I convert 2 tiff images to PDF, I already knows how to get the image out of the DB, and I print it using echo and setting up the MIME type.
But, right know I need to use a duplex printer option, so I need a way to generate a PDF from inside my PHP page, that PDF must containt both TIFF images (one per page) How can I do that? What do I need for php to work with that library.
Thank you very much.
EDIT:
Is a self hosted app, I own the server (actually I'm using WAMP 2).
I extract the images from the MySQL DB (stored using LONGBLOBS).
There is a very simple PHP script that interfaces with ImageMagick:
How to convert multipage TIFF to PDF in PHP
I haven't used it myself but it looks all right.
For this you will need
ImageMagick installed
Ghostscript installed
the linked article describes how to install those in a Ubuntu Linux environment.
Another road to take would be inserting the images directly into a auto-generated PDF file without ImageMagick. The best-known PDF generation library, FPDF, can do this, but for JPEG, PNG and GIF only.
Maybe one of these works for you.
What you really need is a library that brings you a PDF composition engine. And of course you need that engine to support image insertions (specifically TIFF).
The best option is iText.
public void createPdf(String filename) throws DocumentException, IOException
{
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
document.add(new Paragraph("PDF Title"));
// step 5
document.add(new Image("Tiff image path..."));
// step 6
document.close();
}
Hope it helps!
Using imagick library, below solution worked for me -
$document = new Imagick($path."/".$fileName.tiff);
$data = $document->getImageBlob();
$document->setImageFormat("pdf");
$document->writeImages($path."/".$fileName.pdf, true);