I am trying to use a custom font in an image made with PHP GD.
I intend to call int imageloadfont(string $file), but I'm not sure what $file to give if I want to get a Google Font directly from their website.
Do I need to download the font or is there a way to get it remotely?
You can use the .ttf file from Google servers directly if you have allow_url_fopen enabled in your PHP installation. Look into the stylesheet to get the correct url.
However, for best performance, availability and security, you should download the file and use a local path.
Related
I want to generate a thumbnail image for a PDF file stored on google bucket using imagick and PHP
I deploy my application on google app engine(GAE) standard environment
the problem is that I keep getting this error
Fatal error: Uncaught exception 'ImagickException' with message
'UnableToWriteBlob `magick--1noB3XBwJhgfn': Read-only file system
I know that the file system the application is deployed to is not writable, but I need a way to achieve this...
this is my code
<?php
putenv('MAGICK_TEMPORARY_PATH='.sys_get_temp_dir());
$imagick = new Imagick();
// get the content of pdf url
$imagenblob=file_get_contents('http://www.pdf995.com/samples/pdf.pdf');
// read the content and load it inn imagick instance
$imagick->readimageblob($imagenblob);
// point to the first page, because I want thumbnail for first page
$imagick->setIteratorIndex(0);
// set image format
$imagick->setImageFormat('png');
// resize image
$imagick->resizeImage(320,320,Imagick::FILTER_LANCZOS,1,1);
// return the result
header("Content-Type: image/png");
$base64 = 'data:image/png;base64,' . base64_encode($imagick);
exit($base64);
Maybe if I can change the directory which imagick use to write, but I was not able to achieve this !!
ImageMagick requires a temp directory to write artifacts during the delegate decoding/encoding process. I'm not familiar with google-app-engine, but the documents suggest tempnam() & sys_get_temp_dir() should be used.
putenv('MAGICK_TEMPORARY_PATH='.sys_get_temp_dir());
$imagick = new Imagick();
// ...
Also note, Ghostscript is used by ImageMagick for PDF decoding. Verify that the gs binary is installed & available if working w/ PDF files.
You're hitting one of the restrictions of the standard environment sandbox:
An App Engine application cannot:
write to the filesystem. PHP applications can use Google Cloud Storage for storing persistent files. Reading from the filesystem is
allowed, and all application files uploaded with the application are
available.
One option (more costly) would be to use the flexible environment, which doesn't have such restriction.
Another approach would be to try to configure imagick to not use intermediate/temporary files (no clue if it supports that) or to use in-memory file emulation (donno if PHP supports that, I'm a python user) and specify that via Imagick::setFilename.
Another thing to try would be using setFormat instead of setImageFormat, the notes on the setImageFormat suggest that it might be possible to make the transformations before/without writing to the file:
To set the format of the entire object, use the Imagick::setFormat
method. E.g. load TIFF files, then use setFormat('pdf') on the Imagick
object, then writeImagesFile('foo.pdf') or getImagesBlob().
There's no real way to make this work due to the two opposing forces at play:
1) Google App Engine Standard's sandbox limitations
2) Imagick apparently needing local file system access (at least temporarily) in order to work.
So if you can't change how Imagick work, then the only remaining solution is to not use GAE Standard. You can either use GAE Flex or Google Compute Engine. I don't know why Flex is not an option for you; you don't need to move the whole project over. You can just port this part over as a microservice on GAE Flex in the same project. The sole function of the service would be to process the image and get the thumbnail. You can then place the thumbnail in a Google Cloud Storage bucket for the rest of your app (in GAE Standard) to use.
to anyone facing the same problem , I solved mine by created a new Google Compute Engine , with PHP installed on it, I created a function to convert the pdf to image using Imagick and it returns the response as base64 stream.
Imagick does not work well with pdf files on GAE
I am trying to receive and use an api from another site using php. How would I get the link?
https://assetgame.roblox.com/Asset/?id=942531508
this links downloads a file auto, but how would I save it using php (to a path on my site)?
And changing the file extention to .PNG?
file_put_contents("gh", "https://assetgame.roblox.com/Asset/?id=942531508");
you want to brother with gd library for converting if this is not a png
I am using the dompdf library to create my table based PDF and I can view it online or I can have it download to the users folder of choice.
But what I would like to do is have it save it to the remote server( i dont need it to be save to the users PC), like an automatically upload script that would create the file then upload it to the remote server, so i can then use it within my application later on.
is it possible to point the $_FILES["file"] script say so fetch the php page that creates the pdf and it then uploads it from there.
You can do one thing like below which i am doing for my application. First create a folder in your server under the root directory for example. Then change read write permissions to that folder using chmod command.
Then get all the code in $html string.
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
$file_location = $_SERVER['DOCUMENT_ROOT']."app_folder_name/pdfReports/".$pdf_name.".pdf";
file_put_contents($file_location,$pdf);
Where pdfReports is the folder which i created to save all the pdf's. You can change to your folder name.
Quoted from PHP manual :
A URL can be used as a filename with this function if the fopen
wrappers have been enabled.See fopen() for more details on how to
specify the filename. See the Supported Protocols and Wrappers for
links to information about what abilities the various wrappers have,
notes on their usage, and information on any predefined variables they
may provide.
Source: http://php.net/manual/en/function.file-put-contents.php
Is it feasible to create the PDF directly on the server instead, and let the client download it if he needs it? If so, you could use wkhtmltppdf to convert any HTML page into a PDF on the server and save it and/or stream it to the client.
http://code.google.com/p/wkhtmltopdf/
Can't you use the FTP functions of PHP ?
I'm using Filepicker.io to upload PDFs to my application. I have all those URLs and now I am trying to merge some of those PDFs using the PDF Tool Kit PHP library. It was not working for me so I ran some tests using the "file_exists" on PHP and it kept returning false.
I think this has to do with the fact that the URL does not have a ".pdf" extension at the end. This is what they look like: "https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1"
Does anyone know how I can pull the PDF using PHP in order to merge those files using the PDF Toolkit Library?
Thanks!
Alain F.
file_exists doesn't work with URLs, only with local files. Instead download the file to the temp dir using the copy command.
If the file can't be downloaded, the copy command will return false.
$exists = copy('https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1', '/tmp/example.pdf');
if (!$exists) throw new Exception("PDF could not be downloaded");
Use the downloaded file in the PDF Tool Kit.
EDIT: This does not solve this particular problem but does address the theory that it didn't work because "the URL does not have a ".pdf" extension at the end."
You can add things to the end of the filepicker URL with a trailing +
The following urls are equivalent:
https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1
https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1+name.pdf
Images in my website is located in other server. I tried to get the size of an image using getimagesize of php and it is not working. It gives an error 'getimagesize is not seekable'. I tried this same function from other server(images are in same server and I change the location of the file, which is using getimagesize function) and it worked. Can anyone solve this?
From the manual:
filename
This parameter specifies the file you wish to retrieve information about. It can reference a local file or (configuration permitting) a remote file using one of the supported streams.
The settings they're talking about (for the HTTP wrappers) is for the most part having allow_url_fopen enabled or disabled.
The error is probably could not make seekable and could mean that PHP / GD cannot recognize the image file as such.
Have you tried with different images and different image formats (GIF, JPG, PNG) ?
Maybe GIF or PNG is not supported by your PHP version.
maybe the image path is wrong
try using this instead:
dirname($_SERVER['SCRIPT_FILENAME'])
it will return the parent directory's path,
example:
getimagesize(dirname($_SERVER['SCRIPT_FILENAME']).'/images/sampleimage.jpg');