Image magic rotate particular page in php - php

I have a PDF where I need to rotate particular page in PDF.
I have used below command but it rotates all pages in that pdf
convert -density 300x300 -rotate 90 input.pdf result.pdf
Is there any way to rotate particular page ?
Thanks in advance

Using ImageMagick, you can rotate a particular page using the [pagenum] syntax after the filename like this:
convert document.pdf[3] -rotate 90 page3rotated.pdf
However, that will only extract that single page (i.e. page 3), rotate it and save it (and it alone) in the output file. Of course, you could extract ALL the pages, rotate the one you want and re-assemble your PDF, but I can't help thinking that there must be a better way with a different tool.
In general, you should be aware that ImageMagick will rasterise your PDF (so it is basically a picture) and that words/text will no longer be selectable as words/text when ImageMagick puts your PDF back together.

If you are using Linux, You may use PDFMod. This allows independent operations on given pages of the pdf file.
I have attached a screenshot below for demonstration purpose.

Related

Jquery mobile orientation uploaded photo

I'm trying to upload some photo from a mobile using jquery mobile and php but after uploading my photo it's always in landscape format even if my photo is in portrait format on my iphone !
So i can't know which one must be rotated or no .
Thanks
I had this same problem. My solution was a little quick and dirty but here you go:
Write a function to be called when the image loads, check to see if the image is going to rendered in landscape.
$(".imgTile").load(function(){
if(this.width>this.height){
$(this).css("transform","rotate(90deg)");
$(this).css("top",".7em");
$(this).css("left","-.5em");
}
}
A couple notes: I found I had to do some repositioning to get the rotated image to fit properly into the listview (hence the setting of top and left properties) so you may need to tweak this for your needs. Second, there are different css "transform" properties for different browsers, so you may need to set each to have cross browser support. Here's a guide on those properties http://www.w3schools.com/cssref/css3_pr_transform.asp
EDIT: I took some time and came up with a much more solid solution.
First, use php to read the exif tags:
$exif=exif_read_data("uploads/$fileExtension.jpg",0,true);
$orient=$exif["IFD0"]["Orientation"];
Some research into the EXIF standard reveals that the three cases that the iPhone will generate are:
1-- requires no rotation
3-- requires 180 deg. rotation
6-- required 90 deg. rotation
Then use php's shell_exec to run the following shell script, which uses imagemagick to destroy the EXIF headers, and physically rotates the image the specified amount:
#!/bin/bash
for i in $[filename]
do convert -rotate $[number of degrees] $i $i
done
mogrify -strip $[filename]
Now, the image will be correctly oriented everywhere, and browsers that do read EXIF headers won't show it any differently.
The only thing left to do is execute the script with the correct arguments based on the tag value.
Hope this helps!

Converting a multi-page PDF into single-page PDFs and extracting image

I have a multi-page PDF file that has information I need to parse. The information and picture is confined to its own page. I need to extract the text and image from the PDF.
I'm using CentOS and PHP.
My attempt:
I originally tried using a combination of pdftotext and imagemagick. I converted the PDF into an image and that actually separated the pages into their own images. Unfortunately the quality of the image on the page came out very poor.
My goal:
I need to split the PDF into multiple PDFs, one per page. Then, I need to extract the image from that page with the best quality possible.
Thanks.
imagemagick does not fit to perform this task
when you need to extract images from a pdf, at their original size (i.e. the best, since any other resolution is or lesser or bigger than original), you must to use
pdfimages
http://www.foolabs.com/xpdf/download.html
(static binaries are available if you cannot compile from source)
syntax:
pdfimages file.pdf image-root
the image resulting will have the extension .ppm , unless you add the switch -j to have jpeg images as output
pdfseparate to split multi-page.pdf to 1.pdf 2.pdf … + convert 1.pdf 1.png …
pdfseparate (part of poppler) to split multi-page.pdf to 1.pdf 2.pdf …
pdfseparate multi-page.pdf ./single-pages/%d.pdf
extracts all pages from multi-page.pdf
and saves them as single page PDFs, (%d variable for page number)
mogrify (part of ImageMagick) to batch convert all single page PDFs to PNGs at your desired resolution (in DPI)
mogrify ./single-pages/*.pdf -density 300 -format png

combining few jpgs(and a txt) to a single jpg file

I have a sort of a canvas on my site, (not canvas though for the need of support in IE) where images are loaded to and modified.
At the end of the process I'd like to take all the files used in the canvas and connect them into a (bigger) jpg.
My thoughts were to collect all the relative positions of the images and use it to reposition them on the new file.
I should add as well that one of the files is a background image.
layers should not count more than lets say 4 including the background.
on top of that i needed to jpg-ize a text layer as well but for that I have a php script, if anyone has an efficient way to do it I'll be glad to see it as well.
Look at GD or ImageMagic PHP extensions. Both allow to do that.
Here is an example of how to do image overlays in imagemagick. You can specify the scaling and position of the overlay in the convert parameters.

How to convert specific no. of pages from a .pdf file to .png image using imagemagick

i am using Imagemagick for converting my .pdf file to .png images
but when i issue the command
$convert sample.pdf image.png
then it will convert all the pages of sample.pdf file to .png images but exactly i want to
convert a specific no. of pages(e.g. first 10 pages or page no.22 or 12 etc.)
then pleases suggest me a way to solve this issue.
and one more question is that:
when we view our .pdf files in google docs .pdf viewer then they are also in image format
but we can select and copy the text written on pages to the clipboard(simply select the text and press
Ctrl+c)
so how can i implement this so the users of my website can select the text form my images.
(there are already some discussion about it on stackoverflow but they are not very clear)
for i in {0..9} 11 21
do
convert "sample.pdf[$i]" "image_$i".png
done
Benoits answer is what you were looking for for slicing and converting a PDF in to images.
Alternatively you can use pdftk with the cat operation. This would get you the first 10 pages and generate a new sliced PDF for example.
pdftk YOUR.PDF cat 1-10 output SLICED.PDF
Regarding your second question about converting an image PDF to a PDF with text data the only way is to use a OCR tool like Tesseract for example.
The only problem is that those OCR tools are not always that exact. In other words sometimes they will not always be able to output what you read on that image.

How rotate a pdf document using php?

How can I rotate a pdf document using php and linux?
Rotate an Entire PDF Document's Pages to 180 Degrees
$command = "pdftk in.pdf cat 1-endS output out.pdf";
system($command);
You could use pdf90 from PDFjam.
To address some of the other suggestions:
I would be wary of adjusting the Rotate attribute directly, as this attribute is stored as text, and '90' or '270' obviously uses a different number of bytes to '0'. I believe inserting the required bytes can make a mess of the index tables that appear at the end of a PDF file. After that, you're reliant on a viewer being able to interpret the damaged file.
Rendering the PDF to an image and rotating that is going to rasterize any text or vector graphics, leading to either a much larger file size, or much lower quality.
You would have to use a external library like this to extract the info a generate an image, then put it back to the pdf(or a new one)
EDIT:
If your going to get a Logo or a diagram this is a good choice, if its a big document with text and lots of images... its going to be pretty hard, could you edit the OP with more info on what you need?
You will have to access the PDF as a binary file then find and adjust the "Rotate" attribute for each page (and possibly the "MediaBox" attribute). I am not aware of any PDF libraries for PHP that allow for this sort of direct manipulation of existing files. This method will not require changing anything about the content of the pages, it just changes the orientation the pages are displayed in by viewers (similar to the EXIF Orientation information in JPEG images).
This snippet of perl should help illustrate what parts of the file you are looking for.
There are a few libraries for handling PDFs with PHP.
Here's a good code example using such a library. I found it, just by Googling "PHP PDF":
http://www.fpdf.org/en/script/script2.php

Categories