Using PERL,VB.NET,JAVASCRIPT to Resize pictures

来源:互联网 发布:js访问style标签样式 编辑:程序博客网 时间:2024/06/05 17:30

 

    When I develop  projects , often need to resize an image to fit different space, while you directly use html tag like this "< img src="xxx" width="100" height="100" / >" ,the photo will be crashed, so we need to resize images in another way. Here are the code I wrote(just for a reference):

 

PERL(using Image::Magick module):

#-------------------------------------------------------

#begin

sub ResizeImage{ 

 my $img = shift; #file name "tmp.jpeg"

 my $base_width = 120;
 my $base_height = 120;

 my $image = Image::Magick->new($img);

 my($width,$height) = $image->Get('width','height'); 

    if($width<=$base_width && $height<=$base_height){

        # don't resize

        }

    else{

         $W_rate = $base_width/$width;

         $H_rate = $base_height/$height;

          if($W_rate >= $H_rate){$RATE = $H_rate;}

          else{$RATE = $W_rate;}

            $width  = int($RATE*$width  + 0.5);

            $height = int($RATE*$height + 0.5);

       }

    $image->Scale(width => $width, height => $height);
    $image->Sharpen('0.0x1.0');

    $image->Write('-',quality => 70);
    undef $image; 

}

#end

#---------------------------------------------------- 

Javascript:

//----------------------------------------------------

//begin

function ResizeImage(id,w,h){

   //<img src="tmp.jpeg"  id="tmpid" alt="" />
   var sw = document.getElementById(id).width;
   var sh = document.getElementById(id).height;
      var rate;
      var width,height;
      if ((sw<=w)&&(sh<=h)){return;}
      else{
        var w_rate = w/sw;
        var h_rate = h/sh;
       
        if(w_rate>=h_rate){rate = h_rate;}
        else rate = w_rate;
         width = Math.floor(rate * sw + 0.5);
         height = Math.floor(rate * sh +0.5);
        
      }
      document.getElementById(id).width = width;
      document.getElementById(id).height = height; 
    }

//end

//---------------------------------------------------------------    

 '------------------------------------------------------------------

 'VB.NET Output directly to webpage

 

    Private Sub ResizeImg(ByVal ImageBytes As Byte(), ByVal w As Integer, ByVal h As Integer)

        'ImageBytes is read from MSSQLServer

        'w is default width

        'h  is default  height   


        Dim ms As New IO.MemoryStream(ImageBytes)
        Dim oms As New IO.MemoryStream

        Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(ms)

        Dim width As Int32 = image.Width
        Dim height As Int32 = image.Height
        Dim rate As Double

        If width <= w And height <= h Then
        Else
            Dim w_rate As Double = w / width
            Dim h_rate As Double = h / height
            If w_rate >= h_rate Then
                rate = h_rate
            Else
                rate = w_rate
            End If
            width = CInt(Math.Round(rate * width + 0.5))
            height = CInt(Math.Round(rate * height + 0.5))
        End If

        'Dim bitImage As New System.Drawing.Bitmap(wi, hi)
        Dim bitImage As New System.Drawing.Bitmap(width, height)
        Dim g As System.Drawing.Graphics = Drawing.Graphics.FromImage(bitImage)
        g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.High
        g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
        g.Clear(Color.Transparent)
        g.DrawImage(image, New Rectangle(0, 0, width, height))
        ' g.DrawImage(image, New Rectangle(0, 0, wi, hi), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel)

        Try
            bitImage.Save(oms, System.Drawing.Imaging.ImageFormat.Jpeg)
        Catch ex As Exception
        Finally
            image.Dispose()
            bitImage.Dispose()
            g.Dispose()

        End Try

        Dim nImageByte(oms.Length) As Byte
        'oms.Read(nImageByte, 0, nImageByte.Length)

        nImageByte = oms.GetBuffer()

        Response.ContentType = "image/jpeg"
        Response.AppendHeader("Content-Disposition", "filename=S_" & tid.ToString & rid.ToString & ".jpg")
        Response.BinaryWrite(nImageByte)


    End Sub

'end

'-------------------------------------------------------------------

原创粉丝点击