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

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Detect Image Dimensions and Feed to View? (/showthread.php?tid=21025)



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

[eluser]atw[/eluser]
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:
Code:
// 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:
Code:
<?php if($row['pub_photo']): ?>
<h2>Pub Photo:</h2>
<p>&lt;?=img($img)?&gt;</p>
&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

[eluser]atw[/eluser]
@david Johansson: I had always assumed that the img width and height were required but you are right; looking at the w3schools.com 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.