I am working on application where input files having extension .tif(Tagged Image File Format (TIFF)). The basic problem is to display .tif files in all browser which is not applicable on all major browsers. The solution i have accept is to convert them into png
I have am converting .tif file to png using http://image.intervention.io/api/encode in PHP Laravel 5.5
Image intervention is depends on imagick for tif encoding, i have installed this dependency but the encoding scheme is converting/display/store first image from tif file, not all files.
Can any one have solution from tif to PNG or PDF conversion using any PHP library?
I found very nice article here, the approach to solve this problem is,
$file = Storage::disk('s3')->get(request()->file_name);
$file_pdf =storage_path("app/bundle/tmp/".end($name).".pdf");
$document =new \Imagick();
$document->readImageBlob($file);
if (!$document->writeImages($file_pdf, true)) {
echo "Unable to write the file";
} else {
$document->clear();
}
return response()->file($file_pdf)->deleteFileAfterSend(true);
I have read s3 file stream using laravel storage and pass that blob using Imagick readImageBlob() method. The state of the art is $document->writeImages("to_pdf.pdf") file. At the end you can clear the file variable
My suggestion is: You have to use gdal2tiles.py to convert your .tif file into .png.
gdal2tiles Documentation
When you run this command its create an output folder, in which you get openlayers.html file, open it and see your .tif file in every browser easily.
If you need any help related to this, do comment. I'll guide you.
Related
So I have an XML file that has a base64 encoded data string for a pdf file, which just has an image taken from an iPad.
This pdf file can be excessively large, as much as 14MB with dimensions of 57"x38".
These images are taken from an iPad through a DocuSign session, thus I have no way at the moment of controlling their size or format before they get to my php listener script.
However, my script cannot work with such large files as my CRM's API file size max is 10MB, and I need a way of reducing the file size before I can upload it through my CRM's API.
Now if it was just a jpg, it would be ok as there are plenty of ways to reduce file size in PHP, but it is a PDF. I have found plenty of PHP extensions for making PDFs, but I haven't found any for reading a PDF and extracting an image from it.
So is there a way to extract the image from the PDF through PHP, or perhaps compress the pdf file?
UPDATE
I didn't think about the possibility of converting a pdf into a jpg, which apparently is easier to do with imagick. Having my server admin install it and I will see if I can make it work with my script.
UPDATE 2
So I was able to get imagick working and locally I am able to convert pdf files into jpg, and reduce file size dramatically.
However, I am running into an issue using it with my application. I get the following error from my CRM's API:
Failed to parse XML-RPC request: Invalid byte 1 of 1-byte UTF-8 sequence.
So the process is the following:
XML file has a base64 encoded data stream of the pdf file.
I decode this data
I then convert with imagick and reduce file size
I base64 encode and prep for upload
CODE
$imageBlob = base64_decode((string)$pdf->PDFBytes);
$imagick.$x = new Imagick();
$imagick.$x->readImageBlob($imageBlob);
$imagick.$x->setImageFormat('jpeg');
$imagick.$x->setImageCompressionQuality(60);
$imagick.$x->adaptiveResizeImage(1024,768,true);
$imageBlob = $imagick.$x->getImageBlob();
$PDFdata[] = base64_encode($imageBlob);
I can test the date by using the proper header and I can see the new jpeg fine, so I assume the data is properly formatted.
What I am missing?
Ok, so I figured it out.
Imagick was the way to go, and my use of it was good. I just goofed up on the file name because I wasn't using a proper dynamic variable name. Code should have looked like this:
CODE
$imageBlob = base64_decode((string)$pdf->PDFBytes);
${'imagick'.$x} = new Imagick();
${'imagick'.$x}->readImageBlob($imageBlob);
${'imagick'.$x}->setImageFormat('jpeg');
${'imagick'.$x}->setImageCompressionQuality(60);
${'imagick'.$x}->adaptiveResizeImage(1024,768,true);
$imageBlob = ${'imagick'.$x}->getImageBlob();
$PDFdata[] = base64_encode($imageBlob);
$PDFfile[] = $FormCustomField . $x . '.jpg';
So the error I was getting was because of an invalid file name, because the $x variable in the previous code was getting junk values. Now everything works fine.
I need to convert a .png file to .bmp; I'm using the outcome in printer_draw_bmp() to print out a barcode.
GD can generate WBMP, but as far as I can tell that's not the same as .bmp. How can I do this conversion? Or is there another way to print a .png directly?
There is a opensource project on Github that allows reading and saving of BMP files (and other file formats) in PHP.
The project is called PHP Image Magician.
AFAIK, GD doesn't support bmp format. But you can use ImageMagick to save file in bmp format:
$im = new Imagick('image.png');
$im->writeImage('image.bmp');
Or if you want to output image to http response:
$im = new Imagick('image.png');
$im->setImageFormat('bmp');
echo $im;
i need to verify a lot of images files for an application, the situations is that i have 10 directories with almost 10,000 images on each one, something like 100,000 files. Those files are supposed to be gif files but a lot of them (and really mean a lot) were jpg files that a designer change the extension from .jpg to .gif without converting the file format. This is causing the application crash, it creates pdf files using those images, and if i tell to the programm that use the file somefile.gif but it really is a jpg with the extension renamed the pdf creator crashes. I don't want to open every file in something like irfanview or photoshop to verify the format and then modify it if is necessary. Is there a library, class, plugin or something in php or another language that tell me the format of files with no base in the extension but in the headers of the file?
I can't find any ideas to do this, some one can help me?
Thanxs a lot in advance!!!
I personally use exif_imagetype for a true verification rather than checking extensions or mime types.
If you need to find out between gif or jpeg, the function getimagesize() can do that for you. See http://php.net/getimagesize.
list(,,$imageType) = getimagesize($path);
if ($imageType === IMAGETYPE_GIF) {
// GIF found
}
I've heard that the best way to handle uploaded images is to "re-process" them using the GD library and save the processed image. see: PHP image upload security check list
My question is how do this "re-processing" in GD? What this means exactly? I don't know the GD library very well and I'm afraid I will mess it up...
So if anyone who did this before could you give me an example for this?
(I know, another other option is to use ImageMagick. For ImageMagick I found an answer here: Remove EXIF data from JPG using PHP, but I can't use ImgMagick now. By the way.. removing EXIF data means completely recreate the image in this case?)
(I'm using Zend Framework if someone interested.)
If the user uploads a JPEG file, you could do something like this to reprocess it:
$newIm = #imagecreatefromjpeg($_FILES['file']['tmp_name']);
if (!$newIm) {
// gd could not create an image from the source
// most likely, the file was not a valid jpeg image
}
You could then discard the $newIm image using imagedestroy() and use the uploaded file from the user, or save out the image from GD and use that. There could be some issues with saving the GD image as it is not the original image.
Another simple method would be to check the header (first several bytes) of the image file to make sure it is correct; for example all JPEG files begin with 0xff 0xd8.
See also imagecreatefromstring(), and you can also use getimagesize() to run similar checks on the uploaded image.
function isvalidjpeg($file)
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
return is_resource($finfo) &&
(finfo_file($finfo, $file) === 'image/jpeg') &&
finfo_close($finfo);
}
if(isvalidjpeg($_FILES['file']['tmp_name'])) {
$newIm = #imagecreatefromjpeg($_FILES['file']['tmp_name']); .....
I have had a client request on a upload facility for his clients, but after upload that a image thumbnail to be created.
All normal images are ok but he his talking about .psd, .pdf, .eps, .ppt
Having a good look around I think wih imagemagick & ghostscript will cater for most of these but I cant find a solution of PPT or EPS.
Im hoping that imagemagick will be able to do eps as it can do a psd.
Any suggestion on EPS or PPT file format.
Thank you if you can advice.
I'm late to the party I know, however...
This is what I use for .PDF, .EPS and .AI thumbnailing. (Assuming all necessary ImageMagick distros installed)
$file = 'filename.pdf.eps.ai';
$cache = $_SERVER['DOCUMENT_ROOT'].'/cache/';//ensure dir is writeable
$ext = "jpg";//just the extension
$dest = $cache.$file.'.'.$ext;
if (file_exists($dest)){
$img = new imagick();
$img->readImage($dest);
header( "Content-Type: image/jpg" );
echo $img;
exit;
} else {
$img = new imagick($_SERVER['DOCUMENT_ROOT'].'/'.$file.'[0]');
$img->setImageFormat($ext);
$width = $img->getImageheight();
//$img->cropImage($width, $width, 0, 0);
$img->scaleImage(105, 149, true);
$img->writeImage($dest);
header( "Content-Type: image/jpg" );
echo $img;
exit;
}
Don't know why it works, but it does - One code to rule them all right?
PPT is a powerpoint presentation. So creating an image that is a PPT would require some library that can pull this off.
Here are some resources to help you out.
Generate Powerpoint file on the fly
EPS is a vector format, so not unless you have your image as vector objects, you wont be able to do this correctly.
Just some thoughts - none of these things has been tested by myself.
EPS:
You should be able to convert your EPS to a PDF with ghostscript. Using imagemagick & ghostscript you can convert the PDF to some bitmap format (GIF, PNG or JPG).
PPT:
This seems to be somehow more complicated. If your are on a Windows machine you could resort to use the Powerpoint API from within a small hand-written converter. Another possibility would perhaps be to use Apache POI-HSLF whichs is a Java API to the Powerpoint file format. This would require a Java program for the conversion process. The last resort could be that study the Powerpoint binary file format and see if there is e.g. a thumbnail embedded (perhaps beeing used for the file icon in Windows Explorer) that could be extracted.
You could find some free icon sets and use a default icon for all .ppt file and another for all .eps. You can then further extend this for all file formats that cannot be converted to a image, such as audio files. Not the perfect solution but something a user may feel more comfortable with then just having text.