topical media & game development

talk show tell print

mashup-flickr-10-Exercise-cropToSquare.pl / pl



  use POSIX;
  
  sub cropToSquare
  {
      my (image, n) = @_;
      (origWidth, origHeight) = image->Get('width', 'height');
      
      square = Image::Magick->new;
      square->Set(size => origWidth . 'x' . origHeight);    
      square->ReadImage('xc:white');
      square->Composite(image => image, compose => 'Copy', x => 0, y => 0);
      
      w = n;
      h = n;
      x = 0;
      y = 0;
      if (origWidth > origHeight)
      {
          w = floor(h * origWidth/origHeight);
          x = floor((w - n)/2);
      }
      else
      {
          h = floor(w * origHeight/origWidth);
          y = floor((h - n)/2);
      }
      
      square->Resize(geometry =>  w . 'x' . h . '!');
      square->Crop(geometry =>  n . 'x' . n . "+x+y");
     
      return square;
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.