Image-Magick-Thumbnail-PDF

 view release on metacpan or  search on metacpan

META.yml  view on Meta::CPAN

    ExtUtils::MakeMaker:  0
build_requires:
    ExtUtils::MakeMaker:  0
requires:
    Carp:                 0
    Cwd:                  0
    File::Copy:           0
    File::Path:           0
    File::Which:          0
    Getopt::Std:          1
    Image::Magick:        0
    Image::Magick::Thumbnail:  0
    Smart::Comments:      0
    Test::Simple:         0
no_index:
    directory:
        - t
        - inc
generated_by:       ExtUtils::MakeMaker version 6.50
meta-spec:
    url:      http://module-build.sourceforge.net/META-spec-v1.4.html
    version:  1.4

Makefile.PL  view on Meta::CPAN

use ExtUtils::MakeMaker;
WriteMakefile(
	NAME => 'Image::Magick::Thumbnail::PDF',
	VERSION_FROM => 'lib/Image/Magick/Thumbnail/PDF.pm',
	PREREQ_PM => {
		'Smart::Comments' => 0,
		'File::Path' => 0,
		'File::Copy' => 0,	
		'Image::Magick' => 0,
		'Image::Magick::Thumbnail' => 0,
		'File::Which' => 0,
      'Getopt::Std' => 1,
      'Test::Simple' => 0,
      'Carp' => 0,
      'Cwd' => 0,

	},
   ABSTRACT_FROM => 'lib/Image/Magick/Thumbnail/PDF.pm',
   AUTHOR => 'Leo Charre leocharre at cpan dot org',
   LICENSE => 'perl',

README  view on Meta::CPAN

Image::Magick::Thumbnail::PDF - make thumbnail of a page in a pdf document
==========================================================================


I wanted a quick sub to make a thumbnail of a pdf.
The goal of this module is to make a quick thumbnail of a page in a pdf.

They give the viewer an idea of what is inside the document, but is not 
meant as a replacement for the actual file.

This module is a quick interface to a predetermined use of convert.

README  view on Meta::CPAN


DEPENDENCIES

   Carp
   Cwd
   Exporter
   File::Copy
   File::Path
   File::Which
   Getopt::Std
   Image::Magick::Thumbnail
   Smart::Comments
   Test::Simple

COPYRIGHT AND LICENCE

Copyright (C) 2009 by Leo Charre leocharre at cpan dot org

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.

lib/Image/Magick/Thumbnail/PDF.pm  view on Meta::CPAN

package Image::Magick::Thumbnail::PDF;
use strict;
use Carp;
require Exporter;
use File::Which;

use vars qw{$VERSION @ISA @EXPORT_OK %EXPORT_TAGS};
$VERSION = sprintf "%d.%02d", q$Revision: 1.13 $ =~ /(\d+)/g;

@ISA = qw(Exporter);
@EXPORT_OK = qw(create_thumbnail);
%EXPORT_TAGS = (
	all => \@EXPORT_OK,
);


$Image::Magick::Thumbnail::PDF::DEBUG = 0;
sub DEBUG : lvalue { $Image::Magick::Thumbnail::PDF::DEBUG }

sub create_thumbnail {

	# BEGIN GET PARAMS
	print STDERR __PACKAGE__."::create_thumbnail() DEBUG ON\ngetting arguments\n" if DEBUG;
	
	my ($abs_pdf,$abs_out,$page_number,$arg,$all);
	$abs_pdf = shift; $abs_pdf or croak(__PACKAGE__."::create_thumbnail() missing abs pdf argument");
	

lib/Image/Magick/Thumbnail/PDF.pm  view on Meta::CPAN

	# END GET PARAMS









	require Image::Magick::Thumbnail;

	my $src = new Image::Magick;
	my $err = $src->Read($abs_pdf);#	warn("92 ++++ $err") if $err;
	print STDERR "ok read $abs_pdf\n" if DEBUG;
	
	
	if (!$all){
		# not all pages
			my $image = $src->[($page_number-1)];
			defined $image or warn("file [$abs_pdf] page number [$page_number] does not exist?") and return;
			my $out = _dopage($image,$abs_out,$page_number,$arg,$name_of_outfile_in_arguments);		
			return $out;

lib/Image/Magick/Thumbnail/PDF.pm  view on Meta::CPAN

			my ($image,$abs_out,$pagenum,$arg,$name_of_outfile_in_arguments) = @_;
			$pagenum = sprintf "%03d", $pagenum;
			print STDERR " _dopage() $pagenum " if DEBUG;
			
			unless( $name_of_outfile_in_arguments ){
				$abs_out=~s/(\.\w{3,5})$/-$pagenum$1/;
			}

			if ( $arg->{normalize} ){
				my $step = $arg->{restriction} * 2;	
				my ($i,$x,$y) = Image::Magick::Thumbnail::create($image,$step);
				$i->Normalize;
				$image = $i;
				print STDERR ' (normalized) ' if DEBUG;
			}

			if ($arg->{quality}){
				print STDERR " (quality $$arg{quality}) " if DEBUG;
				$image->Set( quality => $arg->{quality} );
			}

			
			my($thumb,$x,$y) = Image::Magick::Thumbnail::create($image,$arg->{restriction});

			if ($arg->{frame}){
				$image->Frame($arg->{frame}.'x'.$arg->{frame});
				print STDERR " (framed $$arg{frame}) " if DEBUG;
			}

			my $err= $thumb->Write($abs_out); #warn("141 +++ $err") if $err;

			print STDERR "$abs_out\n" if DEBUG;
			return $abs_out;		

lib/Image/Magick/Thumbnail/PDF.pm  view on Meta::CPAN



1;

__END__

=pod

=head1 NAME

Image::Magick::Thumbnail::PDF - make thumbnail of a page in a pdf document

=head1 SYNOPSIS

	use Image::Magick::Thumbnail::PDF 'create_thumbnail';
   
	my $out = create_thumbnail('/home/myself/mypdfile.pdf');
	
=head1 DESCRIPTION

I wanted a quick sub to make a thumbnail of a pdf.
The goal of this module is to make a quick thumbnail of a page in a pdf.

They give the viewer an idea of what is inside the document, but is not meant as a replacement
for the actual file.

This module is a quick interface to a predetermined use of convert.
There are a ton of ways to do this via ImageMagick and convert, etc. I took what seemed to make most
sense from various suggestions/ideas, and slapped this together. If you think something can be 
better, please contact the L<AUTHOR>.

No subroutines are exported by default.

You must import as:

	use Image::Magick::Thumbnail::PDF 'create_thumbnail';
   
	my $abs_thumb = create_thumbnail('/abs/path/to.pdf');
	
Or you can do this also:

	Image::Magick::Thumbnail::PDF;
	
	my $abs_thumb = Image::Magick::Thumbnail::PDF::create_thumbnail('/abs/path/to.pdf');

The second example is a lot of text but improves legibility in a lot of code.

By default this will make a normalized thumbnail with a 6 pixel light grey border, 125 pixel max height
and 125 pixel max width png image, which in the above example returns '/abs/path/to-001.pdf'.

The subroutine create_thumbnail() takes 1 required argument and optionally 4 arguments in any order.
The first argument must always be the pdf file you want to make a thumbnail of.

=head1 create_thumbnail()

lib/Image/Magick/Thumbnail/PDF.pm  view on Meta::CPAN


=head1 GIF OUTPUT

If you make all thumbnails and specify as output a .gif file, the output image is an animated gif, with 
each page in its own frame. This may or may not be what you desire.

=head1 DEBUG

Note that if you enable debugger 

	$Image::Magick::Thumbnail::PDF::DEBUG = 1;

You will see that a restriction of 125 changes to 113.. how come? Because we compensate for the frame size.
Asking for a thumbnail no wider or taller then 125 px gives you just that. 

=head1 NOTES

The arguments can be provided in any order. I read somewhere that good code is liberal in what it
receives as input and conservative in its output- kind of what 'people' should be like.

=head1 CAVEATS

There are three ghostscripts out there, ESP Ghostscript, AFPL Ghostscript, and GNU Ghostscript.

=head1 PREREQUISITES

Image::Magick 
Image::Magick::Thumbnail
Smart::Comments
File::Which
Carp
ESP Ghostscript, NOT AFPL Ghostscript, this is tested for.

=head1 SEE ALSO

ImageMagick on the web, convert.
L<Image::Magick::Thumbnail>

=head1 AUTHOR

Leo Charre leocharre at cpan dot org

=head1 COPYRIGHT

Copyright (c) 2009 Leo Charre. All rights reserved.

=head1 LICENSE

t/0.t  view on Meta::CPAN

use Test::Simple 'no_plan';
use strict;
use lib './lib';
use Image::Magick::Thumbnail::PDF 'create_thumbnail';
use Cwd;
use File::Path;
use File::Copy;
use File::Which 'which';





sub _ghostscript_version_ok {

t/0.t  view on Meta::CPAN





#ok( _ghostscript_version_ok() , 'ghostscript version is ok');





$Image::Magick::Thumbnail::PDF::DEBUG = 1;
my $abs_pdf = cwd().'/t/test0/file1.pdf';

File::Path::rmtree(cwd().'/t/test0');
File::Path::rmtree(cwd().'/t/test1');
File::Path::rmtree(cwd().'/t/test2');
File::Path::rmtree(cwd().'/t/test3');

ok( mkdir (cwd.'/t/test0'),'made test dir');
ok( File::Copy::cp( cwd().'/t/file1.pdf', $abs_pdf), 'copied test file to test dir' );

t/1all.t  view on Meta::CPAN

use Test::Simple 'no_plan';
use strict;
use lib './lib';
use Image::Magick::Thumbnail::PDF 'create_thumbnail';
use Cwd;
use Smart::Comments '###';
use File::Path;
use File::Copy;

Image::Magick::Thumbnail::PDF::DEBUG = 1;
my $abs_pdf = cwd().'/t/test/linux_quickref.pdf';

File::Path::rmtree(cwd().'/t/test');
mkdir cwd.'/t/test';

ok( -d cwd.'/t/test','test dir exists');
ok( File::Copy::cp( cwd().'/t/file1.pdf', $abs_pdf), 'copied test file to test dir' );

my $out;
ok( $out = create_thumbnail($abs_pdf,'all_pages'),'create_thumbnail() all pages');



( run in 0.406 second using v1.01-cache-2.11-cpan-beeb90c9504 )