开发者

How do I resize, then convert an image type with CGI::ImageMagick?

开发者 https://www.devze.com 2023-01-29 01:28 出处:网络
I am having so much trouble figuring this out. Below is a verbose example with many issues, but hopefully excusing this beginner\'s attempt, someone will guide me on how to do this.

I am having so much trouble figuring this out. Below is a verbose example with many issues, but hopefully excusing this beginner's attempt, someone will guide me on how to do this.

use LWP::Simple;
use Image::Resize;
use CGI::ImageMagick;

sub convertandsave {
    # This is my remote site where I am grabbing th开发者_如何学Goe image from
    my $url = 'http://someremotedomain.example.com/';

    # Actually, the $image name is $_[0], but for this post I wanted to be clear
    my $image = '6226701.bmp';

    # Displays full size
    my $showimage = '<img src="$url/$image">';
    print qq~Full Size: $showimage <br />~;

    # this is crude, but I absolutely know the last three chars are the image type
    my $image       = lc '6226701.bmp';
    my $remoteimage = "$url/$image";
    my $imagetype   = substr($image, -3, 3);
    print qq~$imagetype<br />~;

    # Save the full sized image locally
    my $savelocal = LWP::Simple::getstore($imageurl, $image);

    # Below are notes and attempts
    # convert a.bmp a.jpg;????
    # if ($imagetype ne 'jpg'){
    #     my $cmd = "imgcvt -i $imagetype -o jpg $old.$cnt $new.$cnt";
    #     print $cmd."\n";
    #     if (system($cmd)) { print "imgcvt failed\n"; }
    # }

    my $thumb = $image;
    my @thumb = split(/./, $thumb);
    my $new   = "$thumb[0].jpg";

    #my $gd = $image1->convert($image1, $new);
    my $new = new CGI::ImageMagick(size => '120x100');
    my $thumb = new CGI::ImageMagick(convert => $image, $new);

    #my $magick> convert rose.jpg rose.png;
    #my $gd = $image1->convert(120, 120);
    # This did work for resizing
    my $thumbnail = Image::Resize->new($image);
    my $gd = $thumbnail->resize(120, 120);

    # open(FH, ">$thumbnail");
    # print FH $thumbnail->jpeg();
    # close(FH);
    # Shows results of my continual failures
    print qq~$image<br />~;
    print qq~$new<br />~;
    print qq~$thumb<br />~;
    exit;
}

All I desire to find out with this question is how to resize an image, convert it to JPEG, then save it. Don't worry about all the other stuff I screwed up and all my ridiculous attempts. I thought maybe showing my attempts would help clarify my desired result.

I really appreciate all the help I get here.


CGI::ImageMagick does not work like you assume it does. You did not understand the documentation. You want to use Image::Magick proper, or just Image::Resize, or simply one of the ready-made thumbnailing modules. See below for working examples.

$savelocal is named inappropriately. getstore returns the response code. Besides you are not doing anything with this variable afterwards. You must include error checking.

Use File::LibMagic to determine file types of local files. Doing it the wrong way (file name extensions) takes more effort and lines of code. This even is not at all necessary; since you are fetching the image over HTTP, trust its Content-Type header. In the example, I skip this since the module supports image format autodetection.

split /./ does not do what you think it does.


# working on a local file
use Image::Thumbnail;
my $t = Image::Thumbnail->new(
    size       => 120,
    input      => '6226701.bmp',
    outputpath => '6226701-thumbnail.jpg',
);
$t->create or die "Could not create thumbnail - error: $t->{error} - warning: $t->{warning}\n";

# fetching an image, then working on it without explicitely saving it first locally
use Image::Thumbnail;
use LWP::UserAgent;
my $image_name = '6226701.bmp';
my $response = LWP::UserAgent->new->get('http://someremotedomain.example.com/' . $image_name);
die "Could not fetching image: $response->status_line\n" unless $response->is_success;

my $t = Image::Thumbnail->new(
    size       => 120,
    input      => \$response->decoded_content, # scalar ref = in-memory access
    outputpath => "$image_name-thumbnail.jpg",
);
$t->create or die "Could not create thumbnail - error: $t->{error} - warning: $t->{warning}\n";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号