#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
#
#                  画像変換プログラム   - convert.cgi VER.1.10 -
#
#                     ----- 2006(C) WEB INVENTOR(Solve) -----
#                                 solve@wb-i.net
#                                 http://wb-i.net/
#
#================================ [注意事項] ===================================
# このスクリプトはフリーソフトです。このスクリプトを使用したいかなる損害に対して
# も作者は一切の責任を負いません。
#====================================使用法=====================================
#		./convert.cgi?src=*******&w=***&h=***&type=***
#///////////////////////////////////////////////////////////////////////////////

#=== method="get"のみ ==============================
$buffer = $ENV{'QUERY_STRING'};
@pairs = split(/&/,$buffer);
foreach $pair (@pairs){
	($key, $value) = split(/=/, $pair);
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$in{$key} = $value;
}


# ==================================================
#改造しました。phone.pl使ってQVGAならば実寸。違うなら120pxに。で、いけっかな？

    require "phone.pl";
    my $phone = &phone_info();

    if ( $phone->{type} eq "docomo" ) {

        if ( $phone->{qvga} ) { 
            $new_width =$in{'w'};
            $image_type =$in{'type'};
            if($image_type eq 'jpeg') { $image_type="jpg"; }
            if($image_type eq '') { $image_type="gif"; }
        } else { 
            $new_width =$in{'w'};
            if($new_width eq '') { $new_width ="120"; }
            $image_type =$in{'type'};
            if($image_type eq 'jpeg') { $image_type="jpg"; }
            if($image_type eq '') { $image_type="gif"; }
        }

    } elsif ( $phone->{type} eq "jphone" ) {

        if ( $phone->{qvga} ) {
            $new_width =$in{'w'};
            $image_type =$in{'type'};
            if($image_type eq 'jpeg') { $image_type="png"; }
            if($image_type eq '') { $image_type="png"; }
        } else {
            $new_width =$in{'w'};
            if($new_width eq '') { $new_width ="120"; }
            $image_type =$in{'type'};
            if($image_type eq 'jpeg') { $image_type="png"; }
            if($image_type eq '') { $image_type="png"; }
        }

    } elsif ( $phone->{type} eq "ezweb" ) {

        if ( $phone->{qvga} ) {
            $new_width =$in{'w'};
            $image_type =$in{'type'};
            if($image_type eq 'jpeg') { $image_type="jpg"; }
            if($image_type eq '') { $image_type="gif"; }
        } else {
            $new_width =$in{'w'};
            if($new_width eq '') { $new_width ="120"; }
            $image_type =$in{'type'};
            if($image_type eq 'jpeg') { $image_type="jpg"; }
            if($image_type eq '') { $image_type="gif"; }
        }

    } else { 

            $new_width =$in{'w'};
            $image_type =$in{'type'};
            if($image_type eq '') { $image_type="gif"; }
    }



#    } else { $new_width =$in{'w'}; }


# ==================================================
#画像ＵＲＬ、幅、画像形式を指定して表示
$image_url =$in{'src'};
$new_height =$in{'h'};

#サイズも形式も同じときは直接表示
if(($new_width eq '') && ($image_url =~ /$image_type$/i)){
	print "Location: $image_url\n\n";
	exit;
}

else {
	print "Location: $image_url\n\n";
	exit;
}

#もうサイズ縮小する必要ないだろうから、カット（09/11/05）

#ImageMagicの使用

#use Image::Magick;
#
#$i = Image::Magick->new;
#$i->Read($image_url);
#
#if($new_width && $new_height){
#	$i->Scale(geometry=>geometry,width=>$new_width,height=>$new_height);	#縦横を与えて縮小拡大
#}elsif($new_width){
#	$i = $i->Transform(geometry=>$new_width);	#同じ比率で縮小拡大
#	$i->Quantize(colors=>12, dither=>'False');	#減色してます
#}
#
#unless($image_type){	#指定しないときは同一形式
#	if ($image_url =~ /jpg$/i || $image_url =~ /jpeg$/i) { $image_type="jpg"; }
#	elsif ($image_url =~ /png$/i ) { $image_type="png"; }
#	elsif ($image_url =~ /bmp$/i ) { $image_type="bmp"; }
#	else { $image_type="gif"; }		#その他はgif
#}
#
#if($image_type eq 'jpg'){$content_type ='jpeg';		#jpgのときはjpeg
#}else{$content_type =$image_type;}
#
#print "Content-type: image/$content_type\n\n";
#binmode(STDOUT);
#$i->Write("$image_type:-");#指定した画像形式に変換
#
#exit; 

