Skip to content

Instantly share code, notes, and snippets.

@erkattak
Created March 2, 2011 19:25
Show Gist options
  • Save erkattak/851528 to your computer and use it in GitHub Desktop.
Save erkattak/851528 to your computer and use it in GitHub Desktop.
Image thumb helper that keeps the thumbnail once it's created. Must mod Image_lib to account for transparent PNGs.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('image_thumb_helper')){
function image_thumb($image_path, $filename, $height, $width){
$filename = preg_replace('/\.png/','_',$filename);
// Get the CodeIgniter super object
$CI =& get_instance();
// Path to image thumbnail
$image_thumb = dirname($image_path) . '/'.$filename . $height . '_' . $width . '.png';
if( ! file_exists($image_thumb)){
// LOAD LIBRARY
$CI->load->library('image_lib');
// CONFIGURE IMAGE LIBRARY
$config['image_library'] = 'gd2';
$config['source_image'] = $image_path;
$config['new_image'] = $image_thumb;
$config['maintain_ratio'] = TRUE;
$config['height'] = $height;
$config['width'] = $width;
$CI->image_lib->initialize($config);
$CI->image_lib->resize();
$CI->image_lib->clear();
}
return '<img src="' . dirname($_SERVER['SCRIPT_NAME']) . '/' . $image_thumb . '" />';
}
}
/* End of file image_helper.php */
/* Location: ./application/helpers/image_helper.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment