htmanning has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I have a site that allows clients to upload logos. Using css, we fit the logo into a square container no matter what the size. Anyone with a logo that is square, or slightly rectangular, fits nicely. However, some logos are extremely wide. By that I mean the width far exceeds the height. The database contains the width and height of the pics. I'm trying to detect pics that are extremely wide, and if so, display them above the title instead of next to the title. So I guess I'm looking for feedback as to the best percentage of width to height and how to calculate it?

Replies are listed 'Best First'.
Re: Dealing with rectangular images
by Linicks (Scribe) on May 25, 2016 at 20:07 UTC

    This is insane input error: Impose a MAX/MIN HxW upload for images - otherwise you are chasing the moon and some idiot will try to do the worst.

    Nick

Re: Dealing with rectangular images
by mhearse (Chaplain) on May 25, 2016 at 22:45 UTC
    Would the diagonal diameter work?
    #!/usr/bin/env perl use strict; # identify -format "%h:%w" image.png # 774:423 my $width = 423; my $height = 774; my $diag = sqrt( $width**2 + $height**2); print $diag, "\n";
Re: Dealing with rectangular images
by $h4X4_|=73}{ (Monk) on May 25, 2016 at 20:10 UTC

    Image::ExifTool works good.

    my $file = ''; my $File_Info= ''; use Image::ExifTool 'ImageInfo'; my $exifTool = new Image::ExifTool; $exifTool->Options(Binary => 1, Composite => 1, DateFormat => '%Y:%m:% +d %H:%M:%S', Unknown => 2, Verbose => 0); # Get file info my $info = $exifTool->ImageInfo($file); $File_Info .= "$_ => $$info{$_}<br>\n" foreach (sort keys %$info); print $File_Info;

    There are modules that can re-size the image, but you will find that not all images will size down as well as others.

    I read your post to fast. I would not suggest doing that. Just limit them and force them to make an image that fits. Because you will end up making it to hard for yourself to program. When you can do the above and have the user do the work that should have been done to the image.

      Sorry, maybe I wasn't clear. I'm resizing via css so I'm not worried about that. BUT, I'd like to either use a gray background to letterbox the wide images, or display the image above the title if it is too wide. I'm using this but it's not working all the time:
      if ($picwidth / $picheight > 1.5) { $backcolor="#f5f5f5"; } else { $backcolor="#ffffff"; }
      So I divide the width by the height and if it's greater than 1.5 it should display the gray color. Some pics that the calculation equals 1.77 do not trigger the gray though. I might be doing it wrong.

        What are the values of $picwidth and $picheight that do and don't work?

        When I wrapped it in a loop to try a handful of ratios, I couldn't find any that didn't. That's why How do I post a question effectively recommends providing actual code with data that evidences the problem.

        OUTPUT:

        I'm resizing via css

        That's generally not a good idea. Images that are too big the for target display area are a waste of resources. High speed connections often aren't. And even now, a lot of people have underpowered GPUs. Also, even a small difference in size will add up, pushing up your bandwidth usage as well as your customers.

        The end result is usually much better when you tell the customers the sizes of the image areas. At very least, you avoid customer dismay when they see what your system has done to their images.