Solved

Using getimagesize

Posted on 2011-02-28
8
367 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 19

Accepted Solution

by:
billmercer earned 400 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 34 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 

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 66 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 66 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

Title # Comments Views Activity
What does $selected = $month == $beginning_month ? 'selected' : ''; mean? 5 40
Currency in SQL? 2 30
jQuery force form POST 7 44
ajax to record click 3 16
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

839 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