Imagick - get a thumbnail for each page of a PDF file - pdf

I'm trying to get some information from pdf-files that i uploaded via move_uploaded_file().
I need thumbnails of each page to do some color analysis. further i need the width and height of each page in an actual unit of distance (not pixel).
is there a way to do the conversion from pdf to image in imagick not imagemagick. just because I'm using the imagick instace for the color analysis.
now this works for a single image file but not for a multipage pdf.
what to do know...?
/* Read the image */
$im = new Imagick($file);
/* Thumbnail the image */
$im->thumbnailImage(null, 200);
/* Overwrite it */
file_put_contents($file, $im);
/* Gather Info */
$width = $im->getImageWidth();
$height = $im->getImageHeight();
$ratio = $width / $height;
/* Calculate gray */
$pixel_count = 0;
$sum_gray = 0;
for ($row=0;$row<$height;$row++) {
for ($col=0;$col<$width;$col++) {
$pixel = $im->getImagePixelColor($col, $row);
$rgb = $pixel->getColor();
$this_gray = .299*$rgb['r'] + .587*$rgb['g'] + .114*$rgb['b'];
$sum_gray += $this_gray;
$pixel_count++;
}
}
$gray = (1-($sum_gray / $pixel_count) / 256)*100;
and if anybody knows how to get the height and width information, plaes let me know.
thanks for the help!

Related

How to add Code128 Barcode image to existing pdf using pdfbox(1.8.12) with barcode4j library?

I am trying to generate the barcode from barcode4j library(code128bean, other barcode beans) and try to add to the existing pdf. The barcode image is getting created locally using the below code.
//Create the barcode bean
Code128Bean code128Bean = new Code128Bean();
final int dpi = 150;
code128Bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar
//width exactly one pixel
//bean.setCodeset(2);
code128Bean.doQuietZone(false);
//Open output file
File outputFile = new File("D:/barcode4jcod128.png"); //I dont want to create it
OutputStream code128Stream = new FileOutputStream(outputFile);
try {
//Set up the canvas provider for monochrome PNG output
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
code128Stream, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
//Generate the barcode
code128Bean.generateBarcode(canvas1, "123456");
//Signal end of generation
canvas1.finish();
} finally {
code128Stream.close();
}
My problem is I don't want to create an image and save it locally in filesystem and then add it as image to pdf. I just want to create dynamically i mean just create the barcode image dynamically and add it to the pdf.
How do I set the pagesize (like PDPage.PAGE_SIZE_A4) to the existing PDPages which I retrieved from catalog.getAllPages() method, like (List<PDPage> pages = catalog.getAllPages();)
Can somebody help on this?
Thank you so much for your help Tilman. Here is what i did
public static BufferedImage geBufferedImageForCode128Bean(String barcodeString) {
Code128Bean code128Bean = new Code128Bean();
final int dpi = 150;
code128Bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar
code128Bean.doQuietZone(false);
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0
);
//Generate the barcode
code128Bean.generateBarcode(canvas1, barcodeString);
return canvas1.getBufferedImage();
}
// main code
PDDocument finalDoc = new PDDocument();
BufferedImage bufferedImage = geBufferedImageForCode128Bean("12345");
PDXObjectImage pdImage = new PDPixelMap(doc, bufferedImage);
PDPageContentStream contentStream = new PDPageContentStream(
finalDoc, pdPage, true, true, true
);
contentStream.drawXObject(pdImage, 100, 600, 50, 20);
contentStream.close();
finalDoc.addPage(pdPage);
finalDoc.save(new File("D:/Test75.pdf"));
The barcode is getting created the but it is created in vertical manner. i would like to see in horizontal manner. Thanks again for your help.
1) add an image to an existing page while keeping the content:
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0
);
code128Bean.generateBarcode(canvas1, "123456");
canvas1.finish();
BufferedImage bim = canvas1.getBufferedImage();
PDXObjectImage img = new PDPixelMap(doc, bim);
PDPageContentStream contents = new PDPageContentStream(doc, page, true, true, true);
contents.drawXObject(img, 100, 600, bim.getWidth(), bim.getHeight());
contents.close();
2) set the media box to A4 on an existing page:
page.setMediaBox(PDPage.PAGE_SIZE_A4);

CMS and store hi-resolution images in generated pdf

I'm looking for good CMS for publishing software manuals.
Requirements:
publish manual pages as web pages with thumbnails and shows full resolution after click on image,
exporting manual pages to a pdf file with full resolution images instead to thumbnails.
I found IMHO best wiki system named Tiki Wiki (https://info.tiki.org/) but when I export to pdf then I gets low resolution thumbnail.
I solve this problem by very simple Tiki Wiki code modification:
Modify lib/wiki-plugins/wikiplugin_img.php to force using full image resolution instead to thumbnail in print page mode (inserted code 1) and rescale images in generated HTML by 0.5 factor (inserted code 2):
[...]
function wikiplugin_img( $data, $params )
{
[...]
$imgdata = array_merge($imgdata, $params);
// inserted code 1 (~410 line)
if ($GLOBALS['section_class']=="tiki_wiki_page print"){
$imgdata['thumb'] = '';
}
// end of inserted code 1
//function calls
if ( !empty($imgdata['default']) || !empty($imgdata['mandatory'])) {
[...]
$fwidth = '';
$fheight = '';
if (isset(TikiLib::lib('parser')->option['indexing']) && TikiLib::lib('parser')->option['indexing']) {
$fwidth = 1;
$fheight = 1;
} else {
// inserted code 2 (~410 line)
if ($GLOBALS['section_class']=="tiki_wiki_page print"){
$fwidth = $imageObj->get_width() / 2;
$fheight = $imageObj->get_height() / 2;
} else {
$fwidth = $imageObj->get_width();
$fheight = $imageObj->get_height();
}
// end of inserted code 2 (~638 line)
}
[...]
Now, after printing to pdf by wkhtmltopdf we gets pdf with small but full resolution images.
Additional modifies:
Adds following lines to cms/cssmenus.css (or other css included in print mode) for increase bottom margin of image caption:
div.thumbcaption {
margin-bottom: 5mm;
}
Removes lines from 171 to ~175 in templates/tiki-show_content.tpl for remove the "The original document is available at" foot.

fit a image on a background image with wideimage

is it possible to fit image onto a white background image using wideimage and in that way to show the image without deformations?.
Now I have this, this source merges a background image with an uploaded image, but a part of image loses
public function createThumb($pathimg, $img, $id) {
$paththumb = 'img/upload/thumbs/' . $img;
$imagen = WideImage::load($pathimg);
$width = $imagen->getWidth();
$height = $imagen->getHeight();
$newWidth = 427;
$newHeight = ($newWidth / $width) * $height;
$imagen->resize($newWidth, $newHeight, 'fill')->saveToFile($paththumb);
$img1 = WideImage::load(realpath($paththumb));
$watermark = WideImage::load(realpath('img/bg.jpg'));
$new = $img1->merge($watermark, 0, 0, 20);
$new->saveToFile($paththumb);
}

Photoshop Automation of alignment of text layer with image

I have never scripted in photoshop before, so I am wondering if this is possible. The following is currently done manually for over than 300 files. The next time round is for 600 files, therefore I am looking into automating it.
Steps:
Make Image Size to 54pixels Hight and 500px Width -- Found that this is doable.
Align Image Left.
Create a text layer and insert text -- Found that this is doable.
Align Text layer 1px to the right of the image.
Trim empty space.
Would appreciate any help and pointers. Thanks.
This script will get you started: Note that in your request you didn't mention what what the original image was going to be and shrinking it to 500 x 54 is going to stretch it one way or another. Step 2, Align the image left, was omitted as you didn't mention what you are aligning this image to. I suspect you are dealing with a large image and what to shrink it down (as long as it's not smaller than 500 x 54) and work from there. I've also omitted stage 4 as I've hard coded the position of the text to be 1 px from the right hand edge (and it vertically centered with Arial font size 18)
Anhyoo.. you should be able to alter the script to your needs.
// set the source document
srcDoc = app.activeDocument;
//set preference units
var originalRulerPref = app.preferences.rulerUnits;
var originalTypePref = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.POINTS;
app.preferences.typeUnits = TypeUnits.POINTS;
// resize image (ignoring the original aspect ratio)
var w = 500;
var h = 54;
var resizeRes = 72;
var resizeMethod = ResampleMethod.BICUBIC;
srcDoc.resizeImage(w, h, resizeRes, resizeMethod)
//create the text
var textStr = "Some text";
createText("Arial-BoldMT", 18.0, 0,0,0, textStr, w-1, 34)
srcDoc.activeLayer.textItem.justification = Justification.RIGHT
//set preference units back to normal
app.preferences.rulerUnits = originalRulerPref;
app.preferences.typeUnits = originalTypePref;
//trim image to transparent width
app.activeDocument.trim(TrimType.TRANSPARENT, true, true, true, true);
// function CREATE TEXT(typeface, size, R, G, B, text content, text X pos, text Y pos)
// --------------------------------------------------------
function createText(fface, size, colR, colG, colB, content, tX, tY)
{
// Add a new layer in the new document
var artLayerRef = srcDoc.artLayers.add()
// Specify that the layer is a text layer
artLayerRef.kind = LayerKind.TEXT
//This section defines the color of the hello world text
textColor = new SolidColor();
textColor.rgb.red = colR;
textColor.rgb.green = colG;
textColor.rgb.blue = colB;
//Get a reference to the text item so that we can add the text and format it a bit
textItemRef = artLayerRef.textItem
textItemRef.font = fface;
textItemRef.contents = content;
textItemRef.color = textColor;
textItemRef.size = size
textItemRef.position = new Array(tX, tY) //pixels from the left, pixels from the top
}
Everything you listed is doable in a script. I suggest you start by reading 'Adobe Intro To Scripting' in your ExtendScript Toolkit program files directory (e.g. C:\Program Files (x86)\Adobe\Adobe Utilities - CS6\ExtendScript Toolkit CS6\SDK\English)

Flex 3 - Enable context menu for text object under a transparent PNG

Here is the situation. In my app I have an overlay layer that is composed of a transparent PNG. I have replaced the hitarea for the png with a 1x1 image using the following code:
[Bindable]
[Embed(source = "/assets/1x1image.png")]
private var onexonebitmapClass:Class;
private function loadCompleteHandler(event:Event):void
{
// Create the bitmap
var onexonebitmap:BitmapData = new onexonebitmapClass().bitmapData;
var bitmap:Bitmap;
bitmap = event.target.content as Bitmap;
bitmap.smoothing = true;
var _hitarea:Sprite = createHitArea(onexonebitmap, 1);
var rect:flash.geom.Rectangle = _box.toFlexRectangle(sprite.width, sprite.height);
var drawnBox:Sprite = new FlexSprite();
bitmap.width = rect.width;
bitmap.height = rect.height;
bitmap.x = -loader.width / 2;
bitmap.y = -loader.height / 2;
bitmap.alpha = _alpha;
_hitarea.alpha = 0;
drawnBox.x = rect.x + rect.width / 2;
drawnBox.y = rect.y + rect.height / 2;
// Add the bitmap as a child to the drawnBox
drawnBox.addChild(bitmap);
// Rotate the object.
drawnBox.rotation = _rotation;
// Add the drawnBox to the sprite
sprite.addChild(drawnBox);
// Set the hitarea to drawnBox
drawnBox.hitArea = _hitarea;
}
private function createHitArea(bitmapData:BitmapData, grainSize:uint = 1):Sprite
{
var _hitarea:Sprite = new Sprite();
_hitarea.graphics.beginFill(0x900000, 1.0);
for (var x:uint = 0; x < bitmapData.width; x += grainSize)
{
for (var y:uint = grainSize; y < bitmapData.height; y += grainSize)
{
if (x <= bitmapData.width && y <= bitmapData.height && bitmapData.getPixel(x, y) != 0)
{
_hitarea.graphics.drawRect(x, y, grainSize, grainSize);
}
}
}
_hitarea.graphics.endFill();
return _hitarea;
}
This is based off the work done here: Creating a hitarea for PNG Image with transparent (alpha) regions in Flex
Using the above code I am able to basically ignore the overlay layer for all mouse events (click, double click, move, etc.) However, I am unable to capture the right click (context menu) event for items that are beneath the overlay.
For instance I have a spell check component that checks the spelling on any textitem and like most other spell checkers if the word is incorrect or not in the dictionary underlines the word in red and if you right click on it would give you a list of suggestions in the contextmenu. This is working great when the text box is not under the overlay, but if the text box is under the overlay I get nothing back.
If anyone can give me some pointers on how to capture the right click event on a textItem that is under a transparent png that would be great.