Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:57:50+00:00 2026-05-30T14:57:50+00:00

I am using ImageMagick for Perl use Image::Magick; I have written functional code to

  • 0

I am using ImageMagick for Perl

use Image::Magick;

I have written functional code to perform all necessary tasks like resize, thumbnail, read dimensions, crop, crop to center.

I do have a requirement that all photos are landscape so I toss out any images with a height greater than or equal to the width.

The issue I am struggling with is cropping before resizing. The photos need to be 4:3 aspect ratio. Some photos come in the new 16:9 format or may have been cropped before sent to the server, which causes a skewed on-screen display. Before I resize I would like to crop the image so it’s closest 4:3 equivalent. For example, 800/600 is ok but 802/600 would crop center to 800/600.

I will be resizing all photos, regarless of cropped size to a max stored image of 800/600 but that is fairy irrelevant. Since I can read the height and width dimensions ahead of time perhaps there is an algorithm available to calculate the closest match. I really just need help with obtaining the new dimensions to crop to, not ImageMagick unless there is a function within ImageMagick that can do this for me.

However, I have been unsuccessful locating one that does.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:57:51+00:00Added an answer on May 30, 2026 at 2:57 pm

    Worked for me:

    my $image_width  = 800; # play with this when testing
    my $image_height = 600;
    
    my $target_aspect  = 4 / 3; 
    my $current_aspect = $image_width / $image_height;
    
    given ($current_aspect <=> $target_aspect) {
      when (1) { $image_width = int($image_height * $target_aspect); }
      when (-1)  { $image_height = int($image_width / $target_aspect); }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to compress an image on the command line using Imagemagick in Perl
I am using Perl Magick which is the Perl module for Image Magick to
How do I tile an image using ImageMagick? I don't think I can use
I'm using Perl and the Image::Magick module to process some JPEGs. I'm using the
I am working on a .NET application to reduce image file sizes using ImageMagick
I'm trying to perform morphology using Magick++ from a c++ program that I am
I'm using ImageMagick's convert tool to generate image thumbnails for a web application. I'm
I am converting images using imagemagick from php. I have a directory name that
I'm using ImageMagick from the command line to resize images: convert -size 320x240 image.jpg
I am using ImageMagick to programmatically reduce the size of a PNG image by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.