To Handle ImageMagic Exceptions in Try::Tiny

Asked 2 years ago, Updated 2 years ago, 73 views

I'm creating something like a file server.
I am generating thumbnails of PDF files using ImageMagic, but there are PDF files that fail with ImageMagic.
For that file, I just want to avoid using thumbnails, but enclosing it with try::tiny doesn't produce what I expect.

Code

try{
    my$image=Image::Magic->new;
    $image->Read("${file}[0]");
    $image->Transform(geometry=>$imgsize);
    $image->Write($thumbnail);
    undef($image);
    $image=Image::Magic->new;
    $image->Read ($thumbnail);
    $image->Resize($imgsize);
    $image->Write($thumbnail);
    undef($image);
}
catch{
    $ret="<div class=\"misc\">$ext</div>";
    return$ret;
}

Output HTML Files

<div class = "item"><div class = "misc"><a href = "_postcard.pdf">.pdf</a></dError: /undefined in findresource
Operand stack:
   --dict:5/14(L)--   F1   9.0   --dict:5/5(L)--   --dict:5/5(L)--   MSGothic-90msp-RKSJ-H   --dict:10/12(ro)(G)--   --nostringval--   CIDFontObject   --dict:6/6(L)--   --dict:6/6(L)--   Adobe-Japan1
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1862   1   3   %oparray_pop   1861   1   3   %oparray_pop   1845   1   3   %oparray_pop   --nostringval--   --nostringval--   2   1   1   --nostringval--   %for_pos_int_continue   --nostringval--   --nostringval--   --nostringval--   --nostringval--   %array_continue   --nostringval--   false   1   %stopped_push   --nostringval--   %loop_continue   --nostringval--   --nostringval--   --nostringval--   --nostringval--   --nostringval--   --nostringval--   %array_continue   --nostringval--   --nostringval--   --nostringval--   --nostringval--   --nostringval--   %loop_continue
Dictionary stack:
   --dict:1156/1684(ro)(G)--   --dict:1/20(G)--   --dict:75/200(L)--   --dict:75/200(L)--   --dict:106/127(ro)(G)--   --dict:286/300(ro)(G)--   --dict:22/25(L)--   --dict:4/6(L)--   --dict:25/40(L)--
Current allocation mode is local
Last OS error: 2
Error: /undefined in findresource
Operand stack:
   --dict:5/14(L)--   F1   9.0   --dict:5/5(L)--   --dict:5/5(L)--   MSGothic-90msp-RKSJ-H   --dict:10/12(ro)(G)--   --nostringval--   CIDFontObject   --dict:6/6(L)--   --dict:6/6(L)--   Adobe-Japan1
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1862   1   3   %oparray_pop   1861   1   3   %oparray_pop   1845   1   3   %oparray_pop   --nostringval--   --nostringval--   2   1   1   --nostringval--   %for_pos_int_continue   --nostringval--   --nostringval--   --nostringval--   --nostringval--   %array_continue   --nostringval--   false   1   %stopped_push   --nostringval--   %loop_continue   --nostringval--   --nostringval--   --nostringval--   --nostringval--   --nostringval--   --nostringval--   %array_continue   --nostringval--   --nostringval--   --nostringval--   --nostringval--   --nostringval--   %loop_continue
Dictionary stack:
   --dict:1156/1684(ro)(G)--   --dict:1/20(G)--   --dict:75/200(L)--   --dict:75/200(L)--   --dict:106/127(ro)(G)--   --dict:286/300(ro)(G)--   --dict:22/25(L)--   --dict:4/6(L)--   --dict:25/40(L)--
Current allocation mode is local
Last OS error: 2
iv><input type="checkbox" name="cb[]" value="_postcard.pdf"/>a href="_postcard.pdf">_postcard.pdf;/a>/div>;

I just want to return the catch block...

Thank you for your reply, Mr.ernix.
I rewritten it as follows.

if(!(-f$thumbnail)or(-M$thumbnail)>$elapsedtime)){
        $ret=eval{
            my$image=Image::Magic->new;
            $image->Read("${file}[0]");
            $image->Transform(geometry=>$imgsize);
            $image->Write($thumbnail);
            undef($image);
            $image=Image::Magic->new;
            $image->Read ($thumbnail);
            $image->Resize($imgsize);
            #$image->Resize("120x");
            $image->Write($thumbnail);
            undef($image);
            return"<div class=\"img\">a href=\"$file\"><img src=\"thumbnail\" class=\"img\"/>a>><div>";
        } ||do{"<div class=\"misc\">$ext</div>";
        };
        return$ret;
    }
    if (-f$thumbnail) {
        $ret="<div class=\"img\">a href=\"$file\">img src=\"$thumbnail\"class=\"img\"/>a><div>";
    }
    else{
        $ret="<div class=\"misc\">$ext</div>";
    }

I get the same error.
The answer you gave me seems too difficult for me...

perl imagemagick

2022-09-30 18:24

1 Answers

#!/usr/bin/perl
use 5.022;
use strict;
use warnings;
use Try:Tiny;

sub wrong {
    try{
        die "ERROR";
    }
    catch{
        my$ret="CATCH";
        return$ret;
    };  # Don't forget the last ";"

    `return` from return `try/catch` does not skip subroutines.";
}

US>subright{
    US>return try{#Return here will leave the subroutine.
        die "ERROR";
    }
    catch{
        my$ret="CATCH";
        return$ret;
    };

    return "This line is not reached.";
}

subusing_eval{
    my$ret=eval{
        die "ERROR";

        #
        # If the code above does not die,
        # Returns 1 so that `||do` blocks below are not evaluated.
        #
        # If you write a return in the block, it's easy to confuse it with the return of the subroutine.
        # It is desirable not to write a return, but simply to have it evaluated at the end.
        #
        1;
    } |do{
        In the block, if there is no return, the last evaluated value is returned.";
    };

    return$ret;
}

say wrong();
say right();
say using_eval();

1;


2022-09-30 18:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.