PDFLib (PCos) get image position from PDF - php

So i'm trying to get image position in PDF with PHP and PDFlib pCos library.
I can list images and get image dimensions like:
$imgcount = $tet->pcos_get_number($doc, "length:pages[" . $page . "]/images");
for ($im = 0; $im < $imgcount; $im++) {
$width = $tet->pcos_get_number($doc, "pages[" . $page . "]/images[" . $im . "]/Width");
$height = $tet->pcos_get_number($doc, "pages[" . $page . "]/images[" . $im . "]/Height");
}
But how do i get image position?
Already tried paths like:
"pages[" . $page . "]/images[" . $im . "]/Box"
"pages[" . $page . "]/images[" . $im . "]/Box[0]"
"pages[" . $page . "]/images[" . $im . "]/Rect"
Nothing works.. and i have searched in web everywhere,
checked also the PDF reference manual.. nothing there..
any suggestions?

To determine the position of an image you have to parse the page content. This is not possible with pCOS. For this, PDFlib offers an additional tool named TET:
http://www.pdflib.com/products/tet/
Please see TET manual, chapter 8.5 "Geometry of Placed Images" for further details on this topic.
But from your code fragment it looks like you already use the pCOS interface within TET. If so, simply check the related manual chapter.
A good starting point might be the TET 4.3 sample image_extractor.php, where you should simply add the positions to the following code fragment:
while ($ti = $tet->get_image_info($page) ) {
/* Report image geometry */
print("page " . $pageno . ": " .
(int) $ti->width . "x" .
(int) $ti->height . "pt [" .
(int) $ti->x . "x" .
(int) $ti->y . "], alpha=" .
$ti->alpha . ", beta=" .
$ti->beta . "\n");

Related

How to generate thumbnail from PDF upload in laravel

I am trying to generate a thumbnail of the PDF I upload in laravel the thumbnail should be the first page of the PDF. Right now I am manually uploading an image to make the thumbnail like this:
if (request()->has('pdf')) {
$pdfuploaded = request()->file('pdf');
$pdfname = $request->book_name . time() . '.' . $pdfuploaded->getClientOriginalExtension();
$pdfpath = public_path('/uploads/pdf');
$pdfuploaded->move($pdfpath, $pdfname);
$book->book_file = '/uploads/pdf/' . $pdfname;
$pdf = $book->book_file;
}
if (request()->has('cover')) {
$coveruploaded = request()->file('cover');
$covername = $request->book_name . time() . '.' . $coveruploaded->getClientOriginalExtension();
$coverpath = public_path('/uploads/cover');
$coveruploaded->move($coverpath, $covername);
$book->card_image = '/uploads/cover/' . $covername;
}
This can be tedious while entering many data I want to generate thumbnail automatically. I searched many answers but I am not able to find laravel specific. I tried to use ImageMagic and Ghost script but I couldn't find a solution and proper role to implement.
Sorry, can't comment yet!
You can use spatie/pdf-to-image to parse the first page as image when file is uploaded and store it in your storage and save the link in your database.
First you need to have php-imagick and ghostscript installed and configured. For issues with ghostscript installation you can refer this. Then add the package composer require spatie/pdf-to-image.
As per your code sample:
if (request()->has('pdf')) {
$pdfuploaded = request()->file('pdf');
$pdfname = $request->book_name . time() . '.' . $pdfuploaded->getClientOriginalExtension();
$pdfpath = public_path('/uploads/pdf');
$pdfuploaded->move($pdfpath, $pdfname);
$book->book_file = '/uploads/pdf/' . $pdfname;
$pdf = $book->book_file;
$pdfO = new Spatie\PdfToImage\Pdf($pdfpath . '/' . $pdfname);
$thumbnailPath = public_path('/uploads/thumbnails');
$thumbnail = $pdfO->setPage(1)
->setOutputFormat('png')
->saveImage($thumbnailPath . '/' . 'YourFileName.png');
// This is where you save the cover path to your database.
}

Searching for file in directory using glob, not returning results

I'm trying to make a small script for retrieving configuration files containing mapping instructions for a product import, using php glob. But I fail miserably.
What I have right now:
echo "Shop type: " . $this->shopType . '<br />';
echo "Shop version: " . $this->shopVersion . '<br />';
echo "Glob search: config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php";
foreach (glob("config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
exit;
$this->shopType represents the name of the shop from which the export file should be imported. E.g. ccvshop.
$this->shopVersion represents the version of the shop, to be able to write different import mappings for different versions of the shop types.
The value of shopType = ccvshop.
The value of shopVersion = 11.2.
Which would make the search string in the glob function:
config/mappings/ccvshop_11.2.php.
Unfortunately my result is empty, and I can't see what I'm doing wrong.
Any help would be appreciated.
I added an image representing my directory structure:
glob() returns an array of matching files. It looks like you already know the name of the file that you want to read. Try something like this:
$filename = "config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php";
if (file_exists($filename))
echo "$filename:" . filesize($filename);

Cropping, Re-sizing and Saving images from one folder to another folder in wordpress

I'm adding an upgrade for the WordPress plugin which I have developed ages ago. This plugin is just a product catalog, so just show products and their images. Products can have more than one images.
I was re-sizing images in the older version of the plugin by CSS, assigning them width and height. Which was working but images look stretched but users were happy. Now I have added a new feature in the plugin which is to crop and re-size from the uploaded image and save it with a different name, like a thumbnail.jpg.
The new feature is working remarkably for the new users who upload images, but the thing is about old users who upgraded to newer version.
The issue is old users already has products and images. When I try to get all the products and images via foreach loop, it works perfectly on 200 - 250 images but breaks on more than 250+ images - No Error :(
Many of my old users has 600+ images so I want a way to crop and re-size the existing images and saves them with a new name and save the file names in the DB.
I'm using wordpress's default wp_get_image_editor(); function.
Here's my query to get old products which has images:
$wpc_product_images_sql = "Select wpc_posts.*, wpc_meta.* From " . $wpdb->posts . " As wpc_posts Inner Join " . $wpdb->postmeta . " As wpc_meta On wpc_posts.ID = wpc_meta.post_id Where wpc_meta.meta_key = 'product_images' Order By wpc_posts.post_title;
And here's my foreach loops (I'm using two loops. First one is getting the products which has images and the second loop is for get images from each post, as I'm mentioned earlier in my question that products can have more than one images. So have to use two loops)
foreach ($wpc_images_qry as $wpc_prod_images) {
echo '<div class="wpc_image_body">'
. '<h3>' . $wpc_prod_images->post_title . '</h3>'
. '<div class="wpc_images">';
$wpc_post_id = $wpc_prod_images->ID;
$wpc_product_images = get_post_meta($wpc_post_id, 'product_images', true);
$big_img_name = array();
foreach ($wpc_product_images as $wpc_prod_img) {
/// For Big
$big_resize_img = wp_get_image_editor($wpc_prod_img['product_img']);
if (!is_wp_error($big_resize_img)) {
$product_big_img = $wpc_prod_img['product_img'];
$product_img_explode = explode('/', $product_big_img);
$product_img_name = end($product_img_explode);
$product_img_name_explode = explode('.', $product_img_name);
$product_img_name = $product_img_name_explode[0];
$product_img_ext = $product_img_name_explode[1];
$big_crop = array('center', 'center');
$big_resize_img->resize($wpc_image_width, $wpc_image_height, $big_crop);
$big_filename = $big_resize_img->generate_filename('big-' . $wpc_image_width . 'x' . $wpc_image_height, $upload_dir['path'], NULL);
$big_resize_img->save($big_filename);
$big_img_name[]['wpc_big_img'] = $upload_dir['url'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext;
if (file_exists($upload_dir['path'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext)) {
echo $upload_dir['path'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext . ' - <strong style="color: #7ad03a;">OK</strong><br>';
} else {
echo $upload_dir['path'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext . ' <strong style="color: red">:(</strong><br>';
}
}
}
update_post_meta($wpc_post_id, 'wpc_big_images', $big_img_name);
echo '</div>'
. '</div>';
}
You might try adding the new image size with
add_image_size()
and then run
https://wordpress.org/plugins/regenerate-thumbnails/
which surely has a solution to the timeout problem.
There is a way to display a message for your plugin users upon update, ask them to install and run the plugin if they have old images.
Hope that works for you.
If you want resize image mean follow these one, it resize your image whenever upload image in media,
add_filter('wp_handle_upload_prefilter','aj_handle_upload');
function aj_handle_upload($file)
{
add_image_size( 'mobile', 360, 0, array( 'center', 'center' ));//mobile is userdefined name.
add_image_size( 'desktop', 700, 0, array( 'center', 'center' ));//desktop & tablet is user defined name.
return $file;
}

PHP Image prints out garbage

Attempting to print out an image to the browser using code I copied from http://php.net/manual/en/function.imagecopyresampled.php.
It prints out a box of random characters.
CODE:
public function printSummaryArticle($article, $copy, $thumb) {
$src_image = Config::getAbsPath() . '/images/articles/' . $article['image'];
echo
'<div class="summary_article"><a href="/'
. BreadCrumbs::getCrumb(1)
. '/'
. BreadCrumbs::getCrumb(2)
. '/article/'
. $article['id']
. '"><h4>'
. $article['title']
. '</h4></a> ('
. $article['date']
. ')'
. '<img src="data:image/jpeg;base64,'. imagejpeg($thumb->generateThumb($src_image, 300, 200)) . '"'
. '<p>'
. strip_tags($copy->truncateString($article['body'], 250, " "))
. '</p><p><a href="/' . BreadCrumbs::getCrumb(1)
. '/'
. BreadCrumbs::getCrumb(2)
. '/article/'
. $article['id']
. '"> Read more</a></p></div>';
}
Also tried:
public function printSummaryArticle($article, $copy, $thumb) {
$src_image = Config::getAbsPath() . '/images/articles/' . $article['image'];
echo
'<div class="summary_article"><a href="/'
. BreadCrumbs::getCrumb(1)
. '/'
. BreadCrumbs::getCrumb(2)
. '/article/'
. $article['id']
. '"><h4>'
. $article['title']
. '</h4></a> ('
. $article['date']
. ')';
header('Content-type: image/jpeg');
imagejpeg($thumb->generateThumb($src_image, 300, 200));
echo
'<p>'
. strip_tags($copy->truncateString($article['body'], 250, " "))
. '</p><p><a href="/' . BreadCrumbs::getCrumb(1)
. '/'
. BreadCrumbs::getCrumb(2)
. '/article/'
. $article['id']
. '"> Read more</a></p></div>';
}
Same result. except with an added error claiming headers have already been sent.
How can I fix this?
imagejpeg() neither returns a string nor performs Base64 encoding. To work around this, capture its output in a PHP output buffer, and then Base64 encode the captured output:
ob_start();
imagejpeg( $my_img );
echo '<img src="data:image/jpeg;base64,' . base64_encode(ob_get_clean()) . '">';
Note that data: URLs are limited to 32 KB in Internet Explorer 8 and do not work in earlier versions of IE (source). If you need to support IE 8 and below, you may want to instead save the image as a separate file on the server. This is left as an exercise for the reader :)
(For an explanation of the "Headers already sent" warning, see How to fix "Headers already sent" error in PHP.)
You haven't closed the image tag, but more importantly you haven't base64_encoded the image data
Before you echo that string
ob_start();
imagejpeg($thumb->generateThumb($src_image, 300, 200));
$imagejpg=ob_get_clean();
then this
. '<img src="data:image/jpeg;base64,'
. base64_encode($imagejpg)
. '" />'
You can't do what you're trying to do in the 2nd example

saving contents with file put contents

i'm trying to get google steetview tiles and save them. But file_get_contents doesn't like my url:
here's my code:
$img = "http://cbk0.google.com/cbk?output=tile&panoid="+$panorama_id+"&zoom=3&x="+$i+"&y='"+$x+"'";
$url = ""+$panorama_id+"Xval"+$i.+"Yval"+$x+".jpg";
file_put_contents($url, file_get_contents($img));
You're using + for concatenation. That's not PHP. You need the dot . operator instead:
$img = "http://cbk0.google.com/cbk?output=tile&panoid=" . $panorama_id . "&zoom=3&x=" . $i . "&y='" . $x . "'";
$url = $panorama_id . "Xval" . $i . "Yval" . $x . ".jpg";
See string operators in the PHP manual.
Alternatively, with double quoted strings, variables within the string will be parsed. See the manual page for the string type.
$img = "http://cbk0.google.com/cbk?output=tile&panoid=$panorama_id&zoom=3&x=$i&y='$x'";
$url = "${panorama_id}Xval${i}Yval${x}.jpg";

Categories