Image-Magick-Thumbnail-NotFound

 view release on metacpan or  search on metacpan

META.yml  view on Meta::CPAN

# http://module-build.sourceforge.net/META-spec.html
#XXXXXXX This is a prototype!!!  It will change in the future!!! XXXXX#
name:         Image-Magick-Thumbnail-NotFound
version:      1.008
version_from: lib/Image/Magick/Thumbnail/NotFound.pm
installdirs:  site
requires:
    Image::Magick:                 0
    Image::Magick::Square:         0
    Image::Magick::Thumbnail:      0

distribution_type: module
generated_by: ExtUtils::MakeMaker version 6.17

Makefile.PL  view on Meta::CPAN

use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
    NAME              => 'Image::Magick::Thumbnail::NotFound',
    VERSION_FROM      => 'lib/Image/Magick/Thumbnail/NotFound.pm', # finds $VERSION
	 ABSTRACT_FROM		 => 'lib/Image/Magick/Thumbnail/NotFound.pm',
    PREREQ_PM         => {
		'Image::Magick' => 0,
		'Image::Magick::Thumbnail' => 0,
		'Image::Magick::Square' => 0,
	 }, 
    ($] >= 5.005 ? (    ## Add these new keywords supported since 5.005
      AUTHOR         => 'Leo Charre <leo@leocharre.com>',
	   ABSTRACT_FROM	=> 'lib/Image/Magick/Thumbnail/NotFound.pm',		
		) : ()),
);

README  view on Meta::CPAN

To install this module type the following:

   perl Makefile.PL
   make
   make install

DEPENDENCIES

This module requires these other modules and libraries:

  Image::Magick, 
  Image::Magick::Thumbnail
  Image::Magick::Thumbnail::Square

COPYRIGHT AND LICENCE

Copyright (C) 2006 by Leo Charre

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.6 or,
at your option, any later version of Perl 5 you may have available.

cgi-bin/error.cgi  view on Meta::CPAN

#!/usr/bin/perl -w
use strict;
use Image::Magick::Thumbnail::NotFound; 

# Will exit on succeed.
new Image::Magick::Thumbnail::NotFound;

# use this constructor instead for 125px square thumbs
#new Image::Magick::Thumbnail::NotFound ({ square => 1, restriction => 125, });


# if no thumbnail was asked for or there were errors...
print "Content-type: text/html\n\n";
print 'Sorry, '.$ENV{REQUEST_URI}.' is unavailable.';
exit;

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

package Image::Magick::Thumbnail::NotFound;
use strict;
use warnings;

require Exporter;
our @ISA = qw(Exporter);

our $VERSION = sprintf "%d.%03d", q$Revision: 1.8 $ =~ /(\d+)/g;

use Image::Magick::Thumbnail;
use Image::Magick::Square;
use File::Path;
use Carp;

# PUBLIC METHODS
sub new { #{{{
	my $class = shift;
	my $self = shift;
	$self ||= {};
	
	$self->{ thumbnails_directory } ||= '/.thumbnails'; # relative to $ENV{DOCUMENT_ROOT}/index.html	

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

	$self->{status}->{thumbnail_path_exists}=1;

	return 1;
} 


# attempt to create thumb and save to disk
sub _create_thumbnail { 
	my $self= shift;

	my $src = new Image::Magick;
	$src->Read($self->{in}); 
	my $side= undef;

	if ($self->{square}){
		($src, $side) = Image::Magick::Square::create($src);
	}	
		
	my ($x,$y);

	# thumb and src are the same thing
	($self->{thumb},$x,$y) = Image::Magick::Thumbnail::create( $src, $self->{size}) 
			or $self->_error( $! . ' - Image::Magick::Thumbnail::create failed') and return $self;

	$self->{status}->{thumbnail_created} = 1;
	
	return 1;	
} 


# helper subs _error, _decode, _cleanpath 

# push errors in to array

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

	return $path;
}

#}}}

1;
__END__

=head1 NAME

Image::Magick::Thumbnail::NotFound - Create thumbnails as http requests for them fail

=head1 SYNOPSIS

	# 1) in your .htaccess file, add/modify this line:
	
	ErrorDocument 404 /cgi-bin/error.cgi
	
	
	# 2) in /cgi-bin/error.cgi:
	
	use Image::Magick::Thumbnail::NotFound;
		
	new Image::Magick::Thumbnail::NotFound; # used as auto, no need to receive object.

	# if this is a normal 404 response.. 	
	print "Content-type: text/html\n\n";
	print "The requested resource returned a 404, File Not Found error.";

	exit;
	

	# 3) Try it out, in your html 

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

	Let's you change where your thumbnails will be. This is I<relative> 
	to $ENV{DOCUMENT_ROOT}/index.html, default is /.thumbnails/

size
	
	If a thumbnail is made, this is the maximum pixel height or width that it will be

square
	
	This will make square thumbnails- Undistorted, sides chopped accordingly.
	Default is 0, uses C<Image::Magick::Square>.

auto

	by default a thumbnail is creted from the source image, given to the request, and
	saved to disk. If you want to do something else with the thumbnail object before
	you save it, or some for some reason you don't even want to save it.. set this to
	auto => 0 and the o 	
	ater, the
	default is 0, so if a failed thumbnail request is detected, the thumbnail
	is made and output to browser (to the request caller, your html, whatever)

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

	ErrorDocument 404 /cgi-bin/error.cgi

This is what makes apache call error.cgi when a request for a resource fails.
In turn, error.cgi will examine the request uri to see if the failed request seems to be for a thumbnail.

=head2 error.cgi

In your error.cgi file you must have this:

	#!/usr/bin/perl 
	use Image::Magick::Thumbnail::NotFound;

	new Image::Magick::Thumbnail::NotFound; # will exit on success

	print "Content-Type: text/html\n\n";
	print "That resource is missing.";
	exit;

=cut



=head1 EXAMPLES

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


=head2 EXAMPLE A

Use a directory that will reside in (htdocs) /.thumbs instead
(will be created). Also make the thumbnails 125px squares.
And show debug info (status steps).
In your error.cgi: 
	
	use strict;

	use Image::Magick::Thumbnail::NotFound;

	new Image::Magick::Thumbnail::NotFound ({
		thumbnails_directory	=> '/.thumbs', 		
		size		=> 125, 		
		square	=> 1,	
		debug		=> 1,
	});

	# if a failed request for a thumbnail was not made, continue..	
	print "Content-type: text/html\n\n";
	print "That resource is not here.";
	exit;

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

=head2 EXAMPLE B

Set auto off. Let's put a border around our thumbnail after it is created,
before we save it to disk. We will let it default to /.thumbnails directory.
We will also be using default size, etc.
Show debug info to STDERR.
In your error.cgi: 

	use strict;

	use Image::Magick::Thumbnail::NotFound;

	my $thumb = new Image::Magick::Thumbnail::NotFound ({
		auto => 0,	
	});

	# we have to check that the 404 error was for an image thumbnail
	if ($n->get_status('requested')){
	
		# we treat $n->{thumb} just like we do a regular Image::Magick loaded image 
		$n->{thumb}->Border(geometry=>'1x1', color=>'black');
		
		# save it - optional.. strongly encoluraged though! 
		$n->save_thumbnail();
		
		# show it and exit. 
		$n->display_and_exit();
	}	
	
	#  continue..	

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


The edge in this system is that it uses the 404 error to call it. Thus, you do not call a thumbnail script
like 'thumbnail_please.cgi?thumb=/bla/bla.jpg', nor do you have to pre run anything to make them. 
As the thumbnails are requested, if they are not found on the server, they are made. No error to the end user is
reported. The next call for the same thumbnail will not go through the script, since the request will no 
longer generate a 404 (file not found) error.

By default this module needs little work on your part. It will detect if a request failed for a thumbnail and
if so create it, show it, and save it, so next time there is no 404 error.

But- This script does not hog the Image::Magick object, you can disable I<auto>, and make changes to the
thumbnail as you wish- before you save it to the disk, display, or both.

=cut



=head1 NOTES

Sample public_html/.htacces file and cgi-bin/error.cgi files are included in the download.

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




=head1 BUGS

I had a query string encrypted with L<Crypt::CBC>, it generated an error and this took me to error.cgi, there was a problem
in parsing the query string to match a request (it looks to see if a failed request uri matches jpg, gif, etc in the end.

=head1 SEE ALSO

L<Image::Magick::Thumbnail>

=head1 AUTHOR

Leo Charre, E<lt>leo@leocharre.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2006 by Leo Charre

This library is free software; you can redistribute it and/or modify



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