?
Solved

Using getimagesize

Posted on 2011-02-28
8
Medium Priority
?
371 Views
Last Modified: 2013-12-13
I want to create a script that looks to see if the image that's getting ready to be displayed in my loop exceeds a 3:4 ratio.

Right now, I've got the width of my photos being displayed as 100. If they're close to 75 px high, the page looks great. But when I get into those dynamics where a picture is resized to 100 px wide and the hieght is 125, things start to look goofy.

I found getimagesize, but I've yet to be able to get my arms around how I grab the width, so I can compare it to the height and determine if it matches that ratio that looks the best with my page.

How do you grab the width and height using getimagesize?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 19

Accepted Solution

by:
billmercer earned 1600 total points
ID: 35002183
getimagesize returns a list of values which includes the width and height among other things.
Capture those values, then do your calculation.

I did something similar recently, here's a quick-and-dirty snippet that might help you. (Code Not Tested)

 $tname = $_FILES["imagename"]["tmp_name"];
 list($width, $height,$t, $a ) = getimagesize($tname);
 $ar = ($width/$height);
 if ( abs($ar - 0.75) > 0.05)
     echo "Aspect ratio is off. Ratio should be 0.750, but this image has an aspect ratio of ". sprintf("%.3f",$ar) . "</li>";

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35004211
You find a nice script about image processing here: http://www.em-web.it/scriptsindex.php Look at puzzle script: you can view it in action and then download it to analyze its code: it showsseveral things about image processin in php.

Cheers
0
 
LVL 7

Assisted Solution

by:Vimal DM
Vimal DM earned 136 total points
ID: 35005999
Hai,

Just find the attachment and follow the function,which i have used

1) resize_image(
2) create_image(

Are the two function for grab the images
functions.php
0
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 

Author Comment

by:brucegust
ID: 35014149
Guys, question...

The solutions look great, but while I can trace the logic of the code to an extent, I'm thinking a question is in order as well as a look at what I'm trying to do so you can tell me if your solutions are what it is that I need.

Head out to http://www.countryshowdown.com/Texaco You'll notice that the photos in the photo gallery change everytime you refresh the screeen. I've hardwired the size of the picture to be 100 X 75. While the symetery is OK, you'll see that some of the images which don't have a 3:4 ration are being obviously stretched and that's what I'm trying to avoid.

Initially I thought I would just skip any image that was outside the dimensions that would look good, but now I'm seeing that I don't have a lot of images that match those measurements.

So I want to resize the images without them looking distorted. Do your suggestions accomplish that?
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 264 total points
ID: 35014625
You can find something interesting here http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_24403569.html?sfQueryTermInfo=1+10+30+crop+imag+resiz#a24373414.
You can also try the script attached written by Ray_Paseur (I lost the right url where it has been posted here at EE)

Cheers
<?php // RAY_resize_and_crop.php
error_reporting(E_ALL);

// RESIZE AN IMAGE PROPORTIONALLY AND CROP CENTERED

function resize_and_crop($original_image_url, $thumb_image_url, $thumb_w, $thumb_h, $quality=75)
{
    // ACQUIRE THE ORIGINAL IMAGE: http://us3.php.net/manual/en/function.imagecreatefromjpeg.php
    $original = imagecreatefromjpeg($original_image_url);
    if (!$original) return FALSE;

    // GET ORIGINAL IMAGE DIMENSIONS
    list($original_w, $original_h) = getimagesize($original_image_url);

    // RESIZE IMAGE AND PRESERVE PROPORTIONS KEEPING AS MUCH OF THE IMAGE AS POSSIBLE
    $thumb_w_resize = $thumb_w;
    $thumb_h_resize = $thumb_h;
    if ($original_w > $original_h)
    {
        $thumb_h_ratio  = $thumb_h / $original_h;
        $thumb_w_resize = (int)round($original_w * $thumb_h_ratio);
    }
    else
    {
        $thumb_w_ratio  = $thumb_w / $original_w;
        $thumb_h_resize = (int)round($original_h * $thumb_w_ratio);
    }
    if ($thumb_w_resize < $thumb_w)
    {
        $thumb_h_ratio  = $thumb_w / $thumb_w_resize;
        $thumb_h_resize = (int)round($thumb_h * $thumb_h_ratio);
        $thumb_w_resize = $thumb_w;
    }

    // CREATE THE PROPORTIONAL IMAGE RESOURCE
    $thumb = imagecreatetruecolor($thumb_w_resize, $thumb_h_resize);
    if (!imagecopyresampled($thumb, $original, 0,0,0,0, $thumb_w_resize, $thumb_h_resize, $original_w, $original_h)) return FALSE;

    // ACTIVATE THIS TO STORE THE INTERMEDIATE IMAGE
    // imagejpeg($thumb, 'RAY_temp_' . $thumb_w_resize . 'x' . $thumb_h_resize . '.jpg', 100);

    // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS
    $final = imagecreatetruecolor($thumb_w, $thumb_h);

    $thumb_w_offset = 0;
    $thumb_h_offset = 0;
    if ($thumb_w < $thumb_w_resize)
    {
        $thumb_w_offset = (int)round(($thumb_w_resize - $thumb_w) / 2);
    }
    else
    {
        $thumb_h_offset = (int)round(($thumb_h_resize - $thumb_h) / 2);
    }

    if (!imagecopy($final, $thumb, 0,0, $thumb_w_offset, $thumb_h_offset, $thumb_w_resize, $thumb_h_resize)) return FALSE;

    // STORE THE FINAL IMAGE - WILL OVERWRITE $thumb_image_url
    if (!imagejpeg($final, $thumb_image_url, $quality)) return FALSE;
    return TRUE;
}

// USE CASE
resize_and_crop('RAY_temp_600x374.jpg', 'RAY_temp_100x100.jpg', 100, 100);
resize_and_crop('RAY_temp_600x374.jpg', 'RAY_temp_200x100.jpg', 200, 100);
resize_and_crop('RAY_temp_600x374.jpg', 'RAY_temp_200x300.jpg', 200, 300);
?>

Open in new window

0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 264 total points
ID: 35014684
I used in my scripts the snippet below and it works. The point is that if you need to have a specified dimension for thumbnails they will be distorted, otherwise they will be proportional but they will have different size. To solve this problem I would use a div for each image: the div will have 100x75 and then center images within divs so to mantain the gallery correctly sized even if images sizes are different.

cheers
function resize_image($source_pic, $destination_pic = "auto", $new_width, $new_height, $proprtion = false, $unlink_src = false){
    $src = imagecreatefromjpeg($source_pic);
    list($width,$height) = getimagesize($source_pic);

    $x_ratio = $new_width / $width;
    $y_ratio = $new_height / $height;

    if (proportions){
        if (($x_ratio * $height) < $new_height){
            $tn_height = ceil($x_ratio * $height);
            $tn_width = $new_width;
        }else{
            $tn_width = ceil($y_ratio * $width);
            $tn_height = $new_height;
        }
    }else{
        $tn_width = $new_width;
        $tn_height = $new_height;
    }

    $tmp=imagecreatetruecolor($tn_width,$tn_height);
    imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

    if ($destination_pic == "auto"){
        $filearr = explode(".", $source_pic);
        $basename = array_shift($filearr);
        $destination_pic = $basename."_".$tn_width."jpg";
    }

    imagejpeg($tmp,$destination_pic,100);
    imagedestroy($src);
    imagedestroy($tmp);

    if ($unlink_src){
        unlink($source_pic);
    }
}

Open in new window

0
 
LVL 19

Expert Comment

by:billmercer
ID: 35021563
Your original question didn't mention resizing the images, you just said you wanted to check the aspect ratio of the photos. Resizing and cropping is a whole different kettle of fish. It's important when you ask your original question that you specify exactly what it is you are trying to accomplish.

0
 

Author Comment

by:brucegust
ID: 35031220
billmercer is right. I was originally looking for a way to weed out the photos that exceeded the dimension I needed in order for the site to look symetrical. I do have to resize the images, though, and I''m not completely understanding the code above. So, I'll close this question and award points based on the original inquiry with a little delegated to those who weighed in, and then I would invite whoever is interested to look at http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26862402.html

Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question