CodeIgniter Forums
Detect Image Dimensions and Feed to View? - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Detect Image Dimensions and Feed to View? (/showthread.php?tid=21025)

Detect Image Dimensions and Feed to View? - El Forum - 07-28-2009

Hi guys,

I have created this code in my controller. It uses data fed from my model (image name) to detect the size of an image:
// if this record has an image defined in the db:
if($data['result']['0']['pub_photo']) {
  // create an array called '$img' for the img() function (used in the view)
  // there MUST be a better way to do this!??
  // this was mainly to gather the width and height of the image (XHTML requirement)!
  $doc_root = str_replace( 'system/application/', '', APPPATH );  // lighter-weight than explode()ing?
  $pub_images = 'images/pubs/';
  $data['img']['src'] = $pub_images . $data['result']['0']['pub_photo'];
  list($data['img']['width'], $data['img']['height']) = getimagesize( $data['img']['src'] );
  $data['img']['alt']   = 'A beautifully composed image of ' . $data['result']['0']['name'] . ', ' . $data['result']['0']['city'];
  $data['img']['title'] = $data['result']['0']['name'] . ', ' . $data['result']['0']['city'];

I use APPPATH to create a base file system path, then use getimagesize() to gather size info. Then, create another array with that info in it (plus other stuff).

These are my questions:

1. Is there a more efficient way to do this (i.e. is ther something built into CI to get an image's dimensions)?
2. Is what I have done the most efficient way to do it?

This is how I use the info in my view:
<?php if($row['pub_photo']): ?>
<h2>Pub Photo:</h2>
&lt;?php endif; ?&gt;

It's the controller bit that I am wondering about.

Many thanks.

Detect Image Dimensions and Feed to View? - El Forum - 07-28-2009

[eluser]David Johansson[/eluser]
Why do yo need to set the size of the image? If you use the html-tag img without any size set your browser will show the image in it's actual size, since you are setting the image size parameters to the images actual size they won't change anything...

Detect Image Dimensions and Feed to View? - El Forum - 07-28-2009

[eluser]Colin Williams[/eluser]
You could maybe modify the helper img() function to autodetect sizes. That would clean up your controller code. Also, no reason to avoid PHP functions like getimagesize() in CI. CI is PHP. Use it freely.

Detect Image Dimensions and Feed to View? - El Forum - 07-29-2009

@david Johansson: I had always assumed that the img width and height were required but you are right; looking at the article on the img tag width and height are optional.
You (I) learn something new everyday! :oD

Do all browsers do this automatically or are there any issues with IE6 etc...?

@Colin Williams: I am a little reticent about modifying the framework itself (I'm trying to use the default "install" so that I can easily upgrade it when an upgrade is available).
This is my first foray into the world of frameworks (I also just started using jQuery too).

I have to say frameworks are awesome, especially CI and jQuery (everything seems so much easier). Smile

Detect Image Dimensions and Feed to View? - El Forum - 07-29-2009

[eluser]Colin Williams[/eluser]
Take a peak at the user guide. You can create helpers in your application folder that override system helpers, using the MY_ prefix on the helper filename.

Also, while width and height are not required, having them in place will improve page loading time slightly, since the browser doesn't have to process the image dimensions itself. Although, to be honest, I don't know how it compares with the amount of time the server needs to process the dimensions. Might be better to let the client to do that lifting, actually.