| 1 | package Image::Delivery::Provider; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Image::Delivery::Provider - The abstract Image Provider class | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | An Image Provider is a class that provides images in a way that | ||
| 12 | makes them usable within an L<Image::Delivery|Image::Delivery> system. | ||
| 13 | |||
| 14 | As well as the actual image data, it provides a variety of metadata | ||
| 15 | that allows the Image::Delivery object to name and store the image correctly. | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | use Digest::MD5 (); | ||
| 21 | use Digest::TransformPath (); | ||
| 22 | |||
| 23 | use vars qw{$VERSION}; | ||
| 24 | BEGIN { | ||
| 25 | $VERSION = '0.14'; | ||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | ##################################################################### | ||
| 33 | # Static Methods | ||
| 34 | |||
| 35 | sub filetypes { | ||
| 36 | 'gif', 'jpg', 'png'; | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | ##################################################################### | ||
| 44 | # Constructor | ||
| 45 | |||
| 46 | # Should be implemented in a subclass | ||
| 47 | # sub new | ||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | ##################################################################### | ||
| 54 | # Main Instance Methods | ||
| 55 | |||
| 56 | # Object identifier | ||
| 57 | # If the Provider doesn't provide an ID, use a Digest of the | ||
| 58 | # image as a default. | ||
| 59 | sub id { | ||
| 60 | my $self = shift; | ||
| 61 | return $self->{id} if $self->{id}; | ||
| 62 | |||
| 63 | # Create a default id from the image data | ||
| 64 | my $image = $self->image; | ||
| 65 | ref $image eq 'SCALAR' or return undef; | ||
| 66 | $self->{id} = Digest::MD5::md5_hex( $$image ); | ||
| 67 | } | ||
| 68 | |||
| 69 | # Return the image data | ||
| 70 | ### Implemented in subclass | ||
| 71 | # sub image | ||
| 72 | |||
| 73 | # Determine the file-type of the image | ||
| 74 | sub content_type { | ||
| 75 | my $self = shift; | ||
| 76 | return $self->{content_type} if $self->{content_type}; | ||
| 77 | |||
| 78 | # Generate the content_type from the image data. | ||
| 79 | # We only need the first 32 bytes to do this. | ||
| 80 | my $image = $self->image or return undef; | ||
| 81 | my $head = substr($$image, 0, 32); | ||
| 82 | return $self->{content_type} = 'image/jpeg' if $head =~ /^\xFF\xD8/; | ||
| 83 | return $self->{content_type} = 'image/gif' if $head =~ /^GIF8[79]a/; | ||
| 84 | return $self->{content_type} = 'image/png' if $head =~ /^\x89PNG\x0d\x0a\x1a\x0a/; | ||
| 85 | undef; | ||
| 86 | } | ||
| 87 | |||
| 88 | # Determine the extention to use | ||
| 89 | sub extension { | ||
| 90 | my $self = shift; | ||
| 91 | return $self->{extension} if $self->{extension}; | ||
| 92 | |||
| 93 | # Generate the extension from the content_type | ||
| 94 | my $content_type = $self->content_type or return undef; | ||
| 95 | my $extension = { | ||
| 96 | 'image/jpeg' => 'jpg', | ||
| 97 | 'image/gif' => 'gif', | ||
| 98 | 'image/png' => 'png', | ||
| 99 | }->{$content_type} or return undef; | ||
| 100 | $self->{extension} = $extension; | ||
| 101 | } | ||
| 102 | |||
| 103 | # Return the TransformPath for the Provider | ||
| 104 | sub TransformPath { | ||
| 105 | my $self = shift; | ||
| 106 | return $self->{TransformPath} if $self->{TransformPath}; | ||
| 107 | |||
| 108 | # Create a default TransformPath from the id | ||
| 109 | my $id = $self->id or return undef; | ||
| 110 | $self->{TransformPath} = Digest::TransformPath->new($id); | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | ##################################################################### | ||
| 118 | # Coercion Support | ||
| 119 | |||
| 120 | sub __as_Digest_TransformPath { shift->TransformPath } | ||
| 121 | |||
| 122 | 1; |