Steven Kovar - Interactive Designer

Tech Journal

Latest Entries

Tech and Design

iPhone

Web

Motion

Custom code based on image sizes in ExpressionEngine

August 12, 2011

This is not really ExpressionEngine-centric - The concept will work in any CMS that spits out an uploaded image path - but since I’m using an ExpressionEngine tag, I’m classifying it as an ExpressionEngine article.

Ok, so I found that I had a preferred dimension for a particular image type in a site I work on. I try to fill a width of 615px wide, when possible, but I unfortunately don’t always have the luxury of getting this resolution image. To preserve my layout, and preserve image quality as much as possible, I want to allow large images to fill the width, and place smaller images centered within a grey div. Here’s how I did it:


First of all, you need to set PHP to “Output” in your template preference. If you are ever confused as to whether you need to set PHP to render on input or output in a template, just remember when set to “Output”, ExpressionEngine tags will parse first, then your PHP will parse. When set to “Input”, your PHP will parse first.

<?php 

$min_width 
550//through some testing, I found i can safely scale images that are approximately 550 wide up to 615 wide without tremendous loss of quality, but I could be stricter and just set this to 615 if I wanted. 
$img_path "{event_image_thumb}"// my ExpressionEngine custom file tag. This inputs the image URL into my function
$imagehw GetImageSize("$img_path"); // This creates an array, of the image width and height. 
$imagewidth $imagehw[0]// this grabs the first element in the array. Remember, the first item in an array is always [0].

if ($imagewidth>=$min_width//if the actual image is at least as wide as my preferred dimension

    
echo "<img src=\"$img_path\" width=\"615\" />"// simply echo the image tag with the image URL and use it as is. 

else // if we made it here, the image is narrower

    
echo "<div class=\"imgHolder\"><img src=\"$img_path\" width=\"$imagewidth\" /></div>"// echo  div with the image within it. With CSS, I'm setting auto margins and the properties of the div. 

}
?> 

And that’s it! This could be used for a lot of other purposes in which you need to write some custom code based on your image size. For example, dealing with vertical vs horizontal images would only require some greater than or less than comparison between the first (width) and second (height) items of the array to adjust a layout accordingly.

 


Reader Comments


There are no comments yet.

Leave a Comment

Commenting is not available in this channel entry.