Solved

Using getimagesize

Posted on 2011-02-28
8
364 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
Comment Utility
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 30

Expert Comment

by:Marco Gasi
Comment Utility
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
Comment Utility
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
 

Author Comment

by:brucegust
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 30

Assisted Solution

by:Marco Gasi
Marco Gasi earned 66 total points
Comment Utility
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 30

Assisted Solution

by:Marco Gasi
Marco Gasi earned 66 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now