1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
|
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$
package MT::Asset::Image;
use strict;
use base qw( MT::Asset );
use MT::Blog;
use MT::Website;
__PACKAGE__->install_properties(
{ class_type => 'image',
column_defs => {
'image_width' => 'integer meta',
'image_height' => 'integer meta',
},
child_of => [ 'MT::Blog', 'MT::Website', ],
}
);
# List of supported file extensions (to aid the stock 'can_handle' method.)
sub extensions {
my $pkg = shift;
return $pkg->SUPER::extensions(
[ qr/gif/i, qr/jpe?g/i, qr/png/i, qr/bmp/i, qr/tiff?/i, qr/ico/i ] );
}
sub class_label {
MT->translate('Image');
}
sub class_label_plural {
MT->translate('Images');
}
sub metadata {
my $obj = shift;
my $meta = $obj->SUPER::metadata(@_);
my $width = $obj->image_width;
my $height = $obj->image_height;
$meta->{image_width} = $width if defined $width;
$meta->{image_height} = $height if defined $height;
$meta->{image_dimensions} = $meta->{ MT->translate("Actual Dimensions") }
= MT->translate( "[_1] x [_2] pixels", $width, $height )
if defined $width && defined $height;
$meta;
}
sub image_height {
my $asset = shift;
my $height = $asset->meta( 'image_height', @_ );
return $height if $height || @_;
eval { require Image::Size; };
return undef if $@;
if ( !-e $asset->file_path || !-r $asset->file_path ) {
return undef;
}
my ( $w, $h, $id ) = Image::Size::imgsize( $asset->file_path );
$asset->meta( 'image_height', $h );
if ( $asset->id ) {
$asset->save;
}
return $h;
}
sub image_width {
my $asset = shift;
my $width = $asset->meta( 'image_width', @_ );
return $width if $width || @_;
eval { require Image::Size; };
return undef if $@;
if ( !-e $asset->file_path || !-r $asset->file_path ) {
return undef;
}
my ( $w, $h, $id ) = Image::Size::imgsize( $asset->file_path );
$asset->meta( 'image_width', $w );
if ( $asset->id ) {
$asset->save;
}
return $w;
}
our $computed_thumbnail_cache = undef;
sub has_thumbnail {
if ( !defined $computed_thumbnail_cache ) {
eval { require MT::Image; MT::Image->new or die; };
$computed_thumbnail_cache = $@ ? 0 : 1;
}
$computed_thumbnail_cache;
}
sub thumbnail_path {
my $asset = shift;
my (%param) = @_;
$asset->_make_cache_path( $param{Path} );
}
sub thumbnail_file {
my $asset = shift;
my (%param) = @_;
my $fmgr;
my $blog = $param{Blog} || $asset->blog;
require MT::FileMgr;
$fmgr ||= $blog ? $blog->file_mgr : MT::FileMgr->new('Local');
return undef unless $fmgr;
my $file_path = $asset->file_path;
return undef unless $fmgr->file_size($file_path);
require MT::Util;
my $asset_cache_path = $asset->_make_cache_path( $param{Path} );
my ( $i_h, $i_w ) = ( $asset->image_height, $asset->image_width );
return undef unless $i_h && $i_w;
# Pretend the image is already square, for calculation purposes.
if ( $param{Square} ) {
require MT::Image;
my %square
= MT::Image->inscribe_square( Width => $i_w, Height => $i_h );
( $i_h, $i_w ) = @square{qw( Size Size )};
if ( $param{Width} && !$param{Height} ) {
$param{Height} = $param{Width};
}
elsif ( !$param{Width} && $param{Height} ) {
$param{Width} = $param{Height};
}
}
if ( my $scale = $param{Scale} ) {
$param{Width} = int( ( $i_w * $scale ) / 100 );
$param{Height} = int( ( $i_h * $scale ) / 100 );
}
if ( !exists $param{Width} && !exists $param{Height} ) {
$param{Width} = $i_w;
$param{Height} = $i_h;
}
# find the longest dimension of the image:
my ( $n_h, $n_w )
= _get_dimension( $i_h, $i_w, $param{Height}, $param{Width} );
my $file = $asset->thumbnail_filename(%param) or return;
my $thumbnail = File::Spec->catfile( $asset_cache_path, $file );
# thumbnail file exists and is dated on or later than source image
if ($fmgr->exists($thumbnail)
&& ( $fmgr->file_mod_time($thumbnail)
>= $fmgr->file_mod_time($file_path) )
)
{
return ( $thumbnail, $n_w, $n_h );
}
# stale or non-existent thumbnail. let's create one!
return undef unless $fmgr->can_write($asset_cache_path);
my $data;
if ( ( $n_w == $i_w )
&& ( $n_h == $i_h )
&& !$param{Square}
&& !$param{Type} )
{
$data = $fmgr->get_data( $file_path, 'upload' );
}
else {
# create a thumbnail for this file
require MT::Image;
my $img = new MT::Image( Filename => $file_path )
or return $asset->error( MT::Image->errstr );
# Really make the image square, so our scale calculation works out.
if ( $param{Square} ) {
($data) = $img->make_square()
or return $asset->error(
MT->translate( "Error cropping image: [_1]", $img->errstr ) );
}
($data) = $img->scale( Height => $n_h, Width => $n_w )
or return $asset->error(
MT->translate( "Error scaling image: [_1]", $img->errstr ) );
if ( my $type = $param{Type} ) {
($data) = $img->convert( Type => $type )
or return $asset->error(
MT->translate( "Error converting image: [_1]", $img->errstr )
);
}
}
$fmgr->put_data( $data, $thumbnail, 'upload' )
or return $asset->error(
MT->translate( "Error creating thumbnail file: [_1]", $fmgr->errstr )
);
return ( $thumbnail, $n_w, $n_h );
}
sub _get_dimension {
my ( $i_h, $i_w, $h, $w ) = @_;
my ( $n_h, $n_w ) = ( $i_h, $i_w );
my $scale = '';
if ( $h && !$w ) {
$scale = 'h';
}
elsif ( $w && !$h ) {
$scale = 'w';
}
else {
if ( $i_h > $i_w ) {
# scale, if necessary, by height
if ( $i_h > $h ) {
$scale = 'h';
}
elsif ( $i_w > $w ) {
$scale = 'w';
}
}
else {
# scale, if necessary, by width
if ( $i_w > $w ) {
$scale = 'w';
}
elsif ( $i_h > $h ) {
$scale = 'h';
}
}
}
if ( $scale eq 'h' ) {
# scale by height
$n_h = $h;
$n_w = int( $i_w * $h / $i_h );
}
elsif ( $scale eq 'w' ) {
# scale by width
$n_w = $w;
$n_h = int( $i_h * $w / $i_w );
}
$n_h = 1 unless $n_h;
$n_w = 1 unless $n_w;
return ( $n_h, $n_w );
}
sub thumbnail_filename {
my $asset = shift;
my (%param) = @_;
my $file = $asset->file_name or return;
require MT::Util;
my $format = $param{Format} || MT->translate('%f-thumb-%wx%h-%i%x');
my $width = $param{Width} || 'auto';
my $height = $param{Height} || 'auto';
$file =~ s/\.\w+$//;
my $base = File::Basename::basename($file);
my $id = $asset->id;
my $ext = lc( $param{Type} || '' ) || $asset->file_ext || '';
$ext = '.' . $ext;
$format =~ s/%w/$width/g;
$format =~ s/%h/$height/g;
$format =~ s/%f/$base/g;
$format =~ s/%i/$id/g;
$format =~ s/%x/$ext/g;
return $format;
}
sub as_html {
my $asset = shift;
my ($param) = @_;
my $text = '';
my $app = MT->instance;
$param->{enclose} = 0 unless exists $param->{enclose};
if ( $param->{include} ) {
my $fname = $asset->file_name;
require MT::Util;
my $thumb = undef;
if ( $param->{thumb} ) {
$thumb = MT::Asset->load( $param->{thumb_asset_id} )
|| return $asset->error(
MT->translate(
"Can't load image #[_1]",
$param->{thumb_asset_id}
)
);
}
my $dimensions = sprintf(
'width="%s" height="%s"',
( $thumb
? ( $thumb->image_width, $thumb->image_height )
: ( $asset->image_width, $asset->image_height )
)
);
my $wrap_style = '';
if ( $param->{wrap_text} && $param->{align} ) {
$wrap_style = 'class="mt-image-' . $param->{align} . '" ';
if ( $param->{align} eq 'none' ) {
$wrap_style .= q{style=""};
}
elsif ( $param->{align} eq 'left' ) {
$wrap_style .= q{style="float: left; margin: 0 20px 20px 0;"};
}
elsif ( $param->{align} eq 'right' ) {
$wrap_style
.= q{style="float: right; margin: 0 0 20px 20px;"};
}
elsif ( $param->{align} eq 'center' ) {
$wrap_style
.= q{style="text-align: center; display: block; margin: 0 auto 20px;"};
}
}
if ( $param->{popup} && $param->{popup_asset_id} ) {
my $popup = MT::Asset->load( $param->{popup_asset_id} )
|| return $asset->error(
MT->translate(
"Can't load image #[_1]",
$param->{popup_asset_id}
)
);
my $link
= $thumb
? sprintf(
'<img src="%s" %s alt="%s" %s />',
MT::Util::encode_html( $thumb->url ), $dimensions,
MT::Util::encode_html( $asset->label ), $wrap_style
)
: MT->translate('View image');
$text = sprintf(
q|<a href="%s" onclick="window.open('%s','popup','width=%d,height=%d,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false">%s</a>|,
MT::Util::encode_html( $popup->url ),
MT::Util::encode_html( $popup->url ),
$asset->image_width,
$asset->image_height,
$link,
);
}
else {
if ( $param->{thumb} ) {
$text = sprintf(
'<a href="%s"><img alt="%s" src="%s" %s %s /></a>',
MT::Util::encode_html( $asset->url ),
MT::Util::encode_html( $asset->label ),
MT::Util::encode_html( $thumb->url ),
$dimensions,
$wrap_style,
);
}
else {
$text = sprintf(
'<img alt="%s" src="%s" %s %s />',
MT::Util::encode_html( $asset->label ),
MT::Util::encode_html( $asset->url ),
$dimensions, $wrap_style,
);
}
}
}
else {
$text = sprintf(
'<a href="%s">%s</a>',
MT::Util::encode_html( $asset->url ),
MT->translate('View image'),
);
}
return $param->{enclose} ? $asset->enclose($text) : $text;
}
# Return a HTML snippet of form options for inserting this asset
# into a web page. Default behavior is no options.
sub insert_options {
my $asset = shift;
my ($param) = @_;
my $app = MT->instance;
my $perms = $app->{perms};
my $blog = $asset->blog or return;
eval { require MT::Image; MT::Image->new or die; };
$param->{do_thumb} = $@ ? 0 : 1;
$param->{can_save_image_defaults}
= $perms->can_do('save_image_defaults') ? 1 : 0;
#$param->{constrain} = $blog->image_default_constrain ? 1 : 0;
$param->{popup} = $blog->image_default_popup ? 1 : 0;
$param->{wrap_text} = $blog->image_default_wrap_text ? 1 : 0;
$param->{make_thumb} = $blog->image_default_thumb ? 1 : 0;
$param->{ 'align_' . $_ }
= ( $blog->image_default_align || 'none' ) eq $_ ? 1 : 0
for qw(none left center right);
$param->{ 'unit_w' . $_ }
= ( $blog->image_default_wunits || 'pixels' ) eq $_ ? 1 : 0
for qw(percent pixels);
$param->{thumb_width}
= $blog->image_default_width
|| $asset->image_width
|| 0;
return $app->build_page( 'dialog/asset_options_image.tmpl', $param );
}
sub on_upload {
my $asset = shift;
my ($param) = @_;
$asset->SUPER::on_upload(@_);
return unless $param->{new_entry};
my $app = MT->instance;
require MT::Util;
my $url = $asset->url;
my $width = $asset->image_width;
my $height = $asset->image_height;
my ( $base_url, $fname ) = $url =~ m|(.*)/([^/]*)|;
$url = $base_url . '/'
. $fname; # no need to re-encode filename; url is already encoded
my $blog = $asset->blog or return;
my $blog_id = $blog->id;
my ( $thumb, $thumb_width, $thumb_height );
$thumb_width = $param->{thumb_width};
$thumb = $param->{thumb};
if ($thumb) {
if ( $thumb_width && ( $thumb_width !~ m/^\d+$/ ) ) {
undef $thumb_width;
}
# width > 1000 not really a thumbnail, so consider invalid
if ( $thumb_width > 1000 ) {
undef $thumb_width;
}
}
if ( $thumb && !$thumb_width ) {
undef $thumb;
}
if ( $param->{image_defaults} ) {
return $app->error(
$app->translate(
'Permission denied setting image defaults for blog #[_1]',
$blog_id
)
) unless $app->{perms}->can_do('save_image_defaults');
# Save new defaults if requested.
$blog->image_default_wrap_text( $param->{wrap_text} ? 1 : 0 );
$blog->image_default_align( $param->{align} || MT::Blog::ALIGN() );
if ($thumb) {
$blog->image_default_thumb(1);
$blog->image_default_width($thumb_width);
$blog->image_default_wunits( $param->{thumb_width_type}
|| MT::Blog::UNITS() );
}
else {
$blog->image_default_thumb(0);
$blog->image_default_width(0);
$blog->image_default_wunits( MT::Blog::UNITS() );
}
#$blog->image_default_constrain($param->{constrain} ? 1 : 0);
$blog->image_default_popup( $param->{popup} ? 1 : 0 );
$blog->save or die $blog->errstr;
}
my $fmgr = $blog->file_mgr;
require MT::Util;
# Thumbnail creation
if ( $thumb = $param->{thumb} ) {
require MT::Image;
my $image_type = scalar $param->{image_type};
my ( $w, $h ) = map $param->{$_}, qw( thumb_width thumb_height );
my ($pseudo_thumbnail_url)
= $asset->thumbnail_url( Height => $h, Width => $w, Pseudo => 1 );
my $thumbnail
= $asset->thumbnail_filename( Height => $h, Width => $w );
my $pseudo_thumbnail_path
= File::Spec->catfile( $asset->_make_cache_path( undef, 1 ),
$thumbnail );
my ( $base, $path, $ext )
= File::Basename::fileparse( $thumbnail, qr/[A-Za-z0-9]+$/ );
my $img_pkg = MT::Asset->handler_for_file($thumbnail);
my $original;
my $asset_thumb = $img_pkg->load(
{ file_name => "$base$ext",
parent => $asset->id,
}
);
if ( !$asset_thumb ) {
$asset_thumb = new $img_pkg;
$original = $asset_thumb->clone;
$asset_thumb->blog_id($blog_id);
$asset_thumb->url($pseudo_thumbnail_url);
$asset_thumb->file_path($pseudo_thumbnail_path);
$asset_thumb->file_name("$base$ext");
$asset_thumb->file_ext($ext);
$asset_thumb->image_width($w);
$asset_thumb->image_height($h);
$asset_thumb->created_by( $app->user->id );
$asset_thumb->label(
$app->translate(
"Thumbnail image for [_1]",
$asset->label || $asset->file_name
)
);
$asset_thumb->parent( $asset->id );
$asset_thumb->save;
}
else {
$original = $asset_thumb->clone;
}
# force these to calculate now, giving a full URL / file path
# for callbacks
$thumbnail = $asset_thumb->file_path;
my $thumbnail_url = $asset_thumb->url;
my $thumb_file_size = $fmgr->file_size($thumbnail);
$app->run_callbacks( 'cms_post_save.asset', $app, $asset_thumb,
$original );
$param->{thumb_asset_id} = $asset_thumb->id;
$app->run_callbacks(
'cms_upload_file.' . $asset_thumb->class,
File => $thumbnail,
file => $thumbnail,
Url => $thumbnail_url,
url => $thumbnail_url,
Size => $thumb_file_size,
size => $thumb_file_size,
Asset => $asset_thumb,
asset => $asset_thumb,
Type => 'thumbnail',
type => 'thumbnail',
Blog => $blog,
blog => $blog
);
$app->run_callbacks(
'cms_upload_image',
File => $thumbnail,
file => $thumbnail,
Url => $thumbnail_url,
url => $thumbnail_url,
Asset => $asset_thumb,
asset => $asset_thumb,
Width => $w,
width => $w,
Height => $h,
height => $h,
ImageType => $image_type,
image_type => $image_type,
Size => $thumb_file_size,
size => $thumb_file_size,
Type => 'thumbnail',
type => 'thumbnail',
Blog => $blog,
blog => $blog
);
}
if ( $param->{popup} ) {
require MT::Template;
if (my $tmpl = MT::Template->load(
{ blog_id => $blog_id,
type => 'popup_image'
}
)
)
{
( my $rel_path = $param->{fname} ) =~ s!\.[^.]*$!!;
if ( $rel_path =~ m!\.\.|\0|\|! ) {
return $app->error(
$app->translate( "Invalid basename '[_1]'", $rel_path ) );
}
$rel_path .= '-' . $asset->id;
my $ext = $blog->file_extension || '';
$ext = '.' . $ext if $ext ne '';
require MT::Template::Context;
my $ctx = MT::Template::Context->new;
$ctx->stash( 'blog', $blog );
$ctx->stash( 'blog_id', $blog->id );
$ctx->stash( 'asset', $asset );
$ctx->stash( 'image_url', $url );
$ctx->stash( 'image_width', $width );
$ctx->stash( 'image_height', $height );
my $popup = $tmpl->build($ctx) or die $tmpl->errstr;
my $root_path = $asset->_make_cache_path;
my $pseudo_path = $asset->_make_cache_path( undef, 1 );
my $abs_file_path
= File::Spec->catfile( $root_path, $rel_path . $ext );
my ( $i, $rel_path_ext ) = ( 0, $rel_path . $ext );
my $pseudo_url = File::Spec->catfile( $pseudo_path,
MT::Util::encode_url($rel_path_ext) );
$pseudo_path = File::Spec->catfile( $pseudo_path, $rel_path_ext );
my ( $vol, $dirs, $basename )
= File::Spec->splitpath($rel_path_ext);
## Untaint. We have checked for security holes above, so we
## should be safe.
($abs_file_path) = $abs_file_path =~ /(.+)/s;
$fmgr->put_data( $popup, $abs_file_path, 'upload' )
or return $app->error(
$app->translate(
"Error writing to '[_1]': [_2]", $abs_file_path,
$fmgr->errstr
)
);
my $html_pkg = MT::Asset->handler_for_file($abs_file_path);
my $original;
my $asset_html = $html_pkg->load(
{ file_name => "$basename",
parent => $asset->id
}
);
if ( !$asset_html ) {
$asset_html = new $html_pkg;
$original = $asset_html->clone;
$asset_html->blog_id($blog_id);
$pseudo_url =~ s!\\!/!g;
$asset_html->url($pseudo_url);
$asset_html->label(
$app->translate(
"Popup Page for [_1]",
$asset->label || $asset->file_name
)
);
$asset_html->file_path($pseudo_path);
$asset_html->file_name($basename);
$asset_html->file_ext( $blog->file_extension );
$asset_html->created_by( $app->user->id );
$asset_html->parent( $asset->id );
$asset_html->save;
}
else {
$original = $asset_html->clone;
}
# Select back the real URL for callbacks
$url = $asset_html->url;
$param->{popup_asset_id} = $asset_html->id;
$app->run_callbacks( 'cms_post_save.asset', $app, $asset_html,
$original );
$app->run_callbacks(
'cms_upload_file.' . $asset_html->class,
File => $abs_file_path,
file => $abs_file_path,
Url => $url,
url => $url,
Asset => $asset_html,
asset => $asset_html,
Size => length($popup),
size => length($popup),
Type => 'popup',
type => 'popup',
Blog => $blog,
blog => $blog
);
}
}
1;
}
sub edit_template_param {
my $asset = shift;
my ( $cb, $app, $param, $tmpl ) = @_;
$param->{image_height} = $asset->image_height;
$param->{image_width} = $asset->image_width;
}
1;
__END__
=head1 NAME
MT::Asset::Image
=head1 SYNOPSIS
use MT::Asset::Image;
# Example
=head1 DESCRIPTION
=head1 METHODS
=head2 MT::Asset::Image->class
Returns 'image', the identifier for this particular class of asset.
=head2 MT::Asset::Image->class_label
Returns the localized descriptive name for this type of asset.
=head2 MT::Asset::Image->extensions
Returns an arrayref of file extensions that are supported by this
package.
=head2 $asset->metadata
Returns a hashref of metadata values for this asset.
=head2 $asset->thumbnail_file(%param)
Creates or retrieves the file path to a thumbnail image appropriate for
the asset. If a thumbnail cannot be created, this routine will return
undef.
=head2 $asset->as_html
Return the HTML I<IMG> element with the image asset attributes.
=head1 AUTHOR & COPYRIGHT
Please see the L<MT/"AUTHOR & COPYRIGHT"> for author, copyright, and
license information.
=cut
|