I am using an image manipulation library in PHP called Intervention. It's a great library but doesn't quite have all the features I need, specifically lossless compression. There are command line tools that do this, but I want to do the processing on the fly and pass the data to the browser, not write the data to the file system.
I want to pull the file data the is supposed to go to a file to be fed straight to PHP, but I am not familiar enough with the command line to know how to "pipe" the data to PHP or how to get PHP to read the data. Maybe something like this:
use Intervention\Image\ImageManagerStatic as Image;
$data = exec('optipng image.png >tophp');
$image_data = fopen('php://stdin', 'r');
$img = Image::make($image_data);
//...do more stuff...
echo $img->response();
I know that obviously won't work, but is there a way to accomplish what I am trying to do?
For any command line tools that have the ability to return the image data to stdout, you can capture that using shell_exec. Then you can pass that data to Intervention. Otherwise, you could save the data to a tmp file, have PHP read the data and then delete the file.
Related
I use the class in this tutorial to open a mp4 file in PHP and stream it to HTML5 video player and everything works fine. Now I would like to do some manipulations to the stream on the fly. e.g: adding a watermark to the stream. I am not sure if it can be done using ffmpeg command or PHP-FFMpeg.
The class in the mentioned tutorial opens the file (or the range of the file that is needed by the player), into the $data variable:
$data = fread($this->stream, $bytesToRead);
echo $data;
flush();
I guess I need to pass the $data to ffmpeg somehow, add the watermark and then echo() the manipulated $data instead of the original one. But I am not sure if it's possible or not, and if it is, how it can be done? I don't know how to open a variable in ffmpeg instead of a file and how to get the output as a variable, and not a file.
Any help would be appreciated.
I'm currently learning PHP and programming in general. I'm currently writing my first website where I have a simple login system and users can enter some data in plain text. Upon entry, PHP encrypts this data.
I want to add a feature where users can then download an unencrypted copy of the data and I want to do it in such a way that the files on disk remain encrypted the entire time and an unencrypted copy is not stored anywhere.
At the moment I should be able to decrypt the data line by line and store it in an array, but I don't know what I could do once I get to that point. Is there a way for PHP to treat an array as if it was just a text file and then ask the client to download it? Or maybe I could just somehow stream the array to a file on the client, line by line?
I have no code to show at the moment as I'm still trying to work out in my head how it will all be structured.
The solution must also be portable between a windows and Linux server.
Assuming the encrypted file is plain text. Second, writing in pseudo-code, because I do not know your solution
First, you need to decrypt the data in something that can be echoed to the user. Or more generally, something that is represented as a String.
I am doing something simillar with PDF files, so I am showing the similar on TXT file:
$encryptedData = $this->getEncryptedData(); // load what needs to be decrypted. Pseudocode
$filename = 'download.txt'; // file name to download. Will be download.txt
$contents = $this->decrypt($encryptedData) // $contents should be String. Decryption is pseudocode
header("Content-Type: text/plain"); //set header as TXT file
header("Content-Disposition:attachment;filename={$filename}"); //force download prompt from the browser
echo $contents; //print decrypted data to TXT
exit; //stop script after download
I have a function that uploads a file into a web storage and prior to saving the file on the storage system if the file is a pdf file i would like to determine how many pages a pdf file has.
Currently i have the following:
$pdftext = file_get_contents($path);
$num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
return $num;
Where $path is the temporary path that i use with fopen to open the document
This function works at times but is not reliable. I know theres also this function
exec('/usr/bin/pdfinfo '.$pdf_file.' | awk \'/Pages/ {print $2}\'', $output);
But this requires the file to donwloaded on the server. Any ideas or suggestions to accomplish this?
PHP is a server-side language, meaning all processing happens on your server. There's no way for PHP to determine details of a file on the client side, it has no knowledge of it neither the required access to it.
So the answer to your question as it is now is: It's not possible. But you probably have a goal in mind why you want to check this, sharing this goal might help to get more constructive answers/suggestions.
As Oldskool already explained this is not possible with PHP on the client side. You would have to upload the PDF file to the server and then determine the amount of pages. There are libraries and command line tools that could accomplish this.
In case you don't want to upload the PDF file to the server (which seems to be the case here) you could use the pdf.js library. Now the client is able to determine the amount of pages in a PDF document on its own.
PDFJS.getDocument(data).then(function (doc) {
var numPages = doc.numPages;
}
There are other libraries as well but I'm not certain about their browser support (http://www.electronmedia.in/wp/pdf-page-count-javascript/)
Now you just submit the amount of pages from javascript to your php file that needs this information. In order to achive this you simply use ajax. In case you don't know ajax, just google it there are enough examples out there.
As a side note; Always remember to not trust the client. The client is able to modify the page count and send a completely different one.
For those of you running linux servers this actually is possible. You need the pdfinfo extension installed and using the function
$pages = exec('/usr/bin/pdfinfo '.$pdf_file.' | awk \'/Pages/ {print $2}\'', $output);
outputs the correct page number where $pdf_file is the temporary path on the server upon upload.
The reason it wasnt working for me was because i didnt have the PDFinfo installed.
I want to return an image when a PHP script is called.
The problem I am having is, that I have the image saved in a variable ($image).
I know I could first save the image on the server with
imagepng($image, 'image.png');
and after that return it with something like
readfile('image.png');
but I am sure there is a more elegant way to do the job, I just don't know it.
Is there a way to combine these two commands or any other way, that allows me not to use a temporary file?
imagepng($image), with no second parameter, will simply dump the raw PNG data out to the client (e.g. the browser). No need to write it to a file, then read that file back in.
This is clearly documented in the man page: http://php.net/imagepng and applies to ALL of the "save" functions in GD.
What are some possible ways to save an image or make use of it that is generated from a PHP script. Using save as it does not help though.
This is not an image created by me that's why I want to avoid get_contents.
here is the picture
and here is the url
https://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0
Just write the content of the URL to a file
<?php
file_put_contents("img.png", file_get_contents("http://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0"));
Using file_put_contents() function. If you don't have data in variable and want to readout use file_get_contents()
Since you are not generating the image in your own code, the simplest would be a combo of file_get_contents and file_put_contents:
$url = '...'; // your url here
$data = file_get_conents($url);
file_put_conents('image.png', $data);
In this specific case the render is a PNG image, but if there's a possibility of it being a JPEG or something else then you need to somehow detect that as well. I 'm not giving any suggestions for this because there's not enough info to go by.
You can define a filename in imgpng() or the other functions to tell PHP to store the picture instead of sending it to the calling browser.
I understand you want to save it on the client, with a browser, not on the server.
"Save as" worked fine for me (Firefox 7). In Chrome you'll have to specify the extension of the filename manually. Did not test other browsers, but it should work similarly
You can do this from the terminal using the curl command.
curl -o out.png 'http://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0'
This will save the file as out.png
use imagepng function.
It will return file to browser or save it specified location.
Need to set parameter for function to save image on specified location.