Jun 25 / Phil

Codeigniter Image Cache & Auto Size on Demand”

What

This ‘library’ for lack of a better description allows  designers to use libraries of high-res images anywhere in the site, at any size, without having to pre-define or pre-scale your images.  It can also be used on the fly with user uploaded photos in a gallery setting.

Why

Other alternatives I have found in both codeigniter and other frameworks/cms’s like Drupal, EE, and Wordpress require the administrators to pre-define set image sizes like thumbnail, medium, large.  While this makes sense for a new site, it can be an issue if you want to redesing and start using a different size for thumbnails, previews etc.

Basic Usage Overview

To use the ‘library’,  all you need to do is set your img tag source to point to the controller with a few pre-defined uri segments.

Image Tag: <img src=”/img/size/o/folders_your_image_is_in-your_image.jpg/w/240/h/160/m/auto” />

* If you arn’t using .htaccess to mask your index.php.. change the path to index.php/img/size…..

Sample site using library: http://www.synlawn.com/gallery

Features

  • Fully customizable with little work
  • Can support any directory depth in any location (does not need to be web accessible)
  • Automatically caches called images in their original directory at called size so it only processes once
  • Can be dropped into any CI installation – It was even originally written as a Matchbox module
  • Maintains your image heirarchy and file name in source (for SEO) and keeps images available for external reference.
  • Uses default codeigniter img_library, nothing else to install (unless you do not have imagemagick, netpbm, gd, or gd2)
  • Uses a default ‘error/not-found’ image so you can stick a site logo on a high res image to use anywhere in case of an error (no image-not-found issues)

read more…

Apr 24 / Phil

Codeigniter AMFPHP library – updated

Update 11/24/09: In order to load other libraries/models into your models with AMFPHP, change the BASEPATH declaration in index.php to

define('BASEPATH', realpath(dirname(__FILE__)).'/'.$system_folder.'/');

This is more of a repost of my wiki page on Codeigniter’s site, but i needed filler content for my first few postings on my site.

At first glance there is a lot to digest, but it is actually a pretty simple installation and is basically just replacing the gateway file with a Codeigniter controller

What it does

This AMF library will give you full access to your Codeigniter Models, as well as any libraries. In other words, it was written to work with Codeigniter, as Codeigniter was intended to be used – and you can drop this into your existing setup.

How??

Instead of trying to bootstrap Codeigniter, or use models outside of your app.  We create a Codeigniter controller, and a Codeigniter AMFPHP library that uses AMFPHP as packaged. – Then you create an AMFPHP Services in the services folder that just extends your Codeigniter models, and gives you full access

Full Working Example

We have packaged up the entire working sample that includes a fresh Codeigniter 1.7.1, AMF 1.9 and 2 flash samples modified from the Open Source Flash Development book chapter by (Wade Arnold) and a sample product mysql table.

All of the sample code assumes you ARE NOT using a .htaccess file, and also assumes you are using the default everything.  If you have moved your app and system path outside your webroot (as you should) it should still work fine too.

Dowload the files

And away we go: Instructions / Explanation of whats going on

  • Download Codeigniter and AMFPHP (or pull apart the link above)
  • Make sure Codeigniter works as expected before moving forward
  • Put the unzipped amfphp package into your libraries folder
  • Create your amf_gateway.php in ‘controllers’

This file replaces the need for gateway.php
setup netConnection to ‘/index.php/amf_gateway’

class Amf_gateway extends Controller
{
  public function __construct()
  {
    parent::Controller();
    //Start our library (keep reading the tutorial)
    $this-&gt;load-&gt;library('amfci');
    //Set new include path for services
    ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . AMFSERVICES);
  }
 
//startup the amf gateway service
 public function index()
 {
  $this-&gt;amfci-&gt;service();
 }
}
  • Create a standard Codeigniter model or use an existing one
  • Drop in the amfci_db.php file provided into ‘libraries/amfphp/services/amfci_db.php’
  • Create AMFPHP Service in ‘libraries/amfphp/services’ folder using the template provided

This file is a service for AMFPHP – called from flash
This extends your existing model to give flash access to all its methods

//Load CI DB Instance since we are not coming through index.php
require_once('amfci_db.php');
 
// Use this path if you are using a module (ie, MatchBox)
// require_once(AMFSERVICES.'/../../../modules/test_shop/models/product_model.php');
 
// Use this path if you are using standard install
require_once(AMFSERVICES.'/../../../models/your_model.php');
class your_model_service extends your_model
{
  //Since extending your_model, all existing methods are available
  //New Methods
  public function yourNewMethod()
  {
    $something = array(); //Create array to send to flash
    return $something;
  }
}

Add the amfci.php library to your library folder The amfci library is used by amf_gateway controller that replaces the default gateway.php file, by creating an instance of the gateway as $this->gateway, and settign the needed options

This file is the new amfphp library for CI
It it loaded from the amf_gateway controller, and binds all of it together

class Amfci
{
  public $gateway;
  public $CI;
  public function __construct()
  {
    $this-&gt;CI = get_instance();
    require realpath(dirname(__FILE__))."/amfphp/globals.php";
    require realpath(dirname(__FILE__))."/amfphp/core/amf/app/Gateway.php";
    define('AMFSERVICES', realpath(dirname(__FILE__))."/amfphp/services");
 
    $this-&gt;gateway = new Gateway();
    $this-&gt;gateway-&gt;setCharsetHandler("utf8_decode", "ISO-8859-1", "ISO-8859-1");
    $this-&gt;gateway-&gt;setLooseMode();
    $this-&gt;gateway-&gt;setErrorHandling(E_ALL ^ E_NOTICE);
    $this-&gt;gateway-&gt;setClassMappingsPath(AMFSERVICES.'/vo');
    $this-&gt;gateway-&gt;setClassPath(AMFSERVICES);
 
    if(PRODUCTION_SERVER)
    {
      //Disable profiling, remote tracing, and service browser
      $this-&gt;gateway-&gt;disableDebug();
    }
  }
 
  public function service()
  {
    $this-&gt;gateway-&gt;service();
  }
}

Connect and call from flash

package com.page12.hello_world
{
  import flash.display.MovieClip;
  import fl.events.*;
  import flash.events.*;
  import flash.net.NetConnection;
  import flash.net.Responder;
  import flash.net.ObjectEncoding;
 
  public class Main extends MovieClip {
    private var gateway:String = "/index.php/amf_gateway";
    private var connection:NetConnection;
    private var responder:Responder;
 
    public function Main()
    {
      send_btn.addEventListener(MouseEvent.CLICK, sendData);
      responder = new Responder(onResult, onFault);
      connection = new NetConnection;
      //connection.objectEncoding = ObjectEncoding.AMF3;
      connection.connect(gateway);
    }
 
    public function sendData(e:MouseEvent):void
    {
      var params = server_txt.text;
      response_txt.text = 'Send '+server_txt.text+' to server';
      connection.call("hello_world_service.say", responder, params);
    }
 
    private function onResult(result:Object):void
    {
      response_txt.text = String(result);
    }
 
    private function onFault(fault:Object):void
    {
      response_txt.text = String(fault.description);
    }
  }
}
Aug 7 / Phil

Dropdown Select Feed Widget