Related thread : http://expressionengine.com/forums/viewthread/97703/
Hello,
I have a simple extension on the site Ocean Rodeo Forums which ads the path of phpThumb to the front of any image that is attached to a forum thread. When the extension is enabled, all images in the forum are resized on the fly to a maximum of 600px and cached locally:
Final result if <img /> tag looks like this:
<img src="/_inc/phpthumb/phpThumb.php?w=700&q=70&src=http://farm2.static.flickr.com/1356/1311955635_e6adac5a42_b.jpg" alt='1311955635_e6adac5a42_b.jpg' />
We do this as a lot of people were uploading very large images which were difficult to view in the forums. The extension does its job, however the forum feeds stop working when I enable the extension. If I disable the extension the forum feeds work again.
This is my first extension, so the answer will most likely be quite obvious to someone with a wee bit more experience than myself. Here is the code I wrote:
<?php
/*
* Add phpthumb resizing to [img][/img] tags for ExpressionEngine
*
* @package ooc_phpThumbImg
* @version 0.0.1
* @author James Riordon <http://outofcontrol.ca>
* @see http://www.outofcontrol.ca/extensions/phpThumb_img
* @copyright Copyright (c)2008 James Riordon
* @license {@link http://creativecommons.org/licenses/by-sa/3.0/ Creative Commons Attribution-Share Alike 3.0 Unported} All source code commenting and attribution must not be removed. This is a condition of the attribution clause of the license.
*/
if ( ! defined('EXT')) exit('Invalid file request');
class ooc_phpthumb_img
{
var $settings = array();
var $name = 'phpThumb IMG';
var $version = '0.0.1';
var $description = 'Write [img] tag to include phpthumbs.php prefix.';
var $settings_exist = 'y';
var $docs_url = 'http://www.outofcontrol.ca/extensions/phpThumb_img/docs';
var $max_width = '700';
var $image_quality = '70'; // From 0 - 100 for jpeg compression. 0 being worst.
var $phpThumb_path = '/_inc/phpthumb/';
var $phpThumb_script_name = 'phpThumb.php';
/**
* PHP5 Constructor
*
* @param array|string $settings Extension settings associative array or an empty string
*/
function __construct($settings='')
{
$this->settings = $settings;
}
/**
* PHP4 Constructor
*
* @see __construct()
*/
function ooc_phpthumb_img($settings='')
{
return __construct($settings);
}
function settings()
{
global $LANG;
$settings = array();
$settings['max_width'] = '600';
$settings['image_quality'] = '70';
$settings['phpThumb_path'] = '/phpthumb/';
$settings['phpThumb_scriptName'] = 'phpThumb.php';
return $settings;
}
function activate_extension()
{
global $DB;
$DB->query($DB->insert_string('exp_extensions',
array (
'extension_id' => '',
'class' => 'ooc_phpthumb_img',
'method' => 'alter_img_tag',
'hook' => 'typography_parse_type_start',
'settings' => '',
'priority' => '10',
'version' => $this->version,
'enabled' => 'y'
)
)
);
}
function alter_img_tag($str, $self, $prefs)
{
$phpThumb_path = $this->phpThumb_path . $this->phpThumb_script_name . '?w=' . $this->max_width . '&q=' . $this->image_quality . '&src;=';
// Do not modify if phpThumb is already used.
if (strpos($phpThumb_path, $str))
{
return $str;
}
$from_pattern = '[img]';
$to_pattern = '[img]' . $phpThumb_path;
return str_replace($from_pattern,$to_pattern,$str);
}
function update_extension($current='')
{
global $DB;
if ($current == '' OR $current == $this->version)
{
return false;
}
$DB->query("UPDATE exp_extensions
SET version = '" . $DB->escape_str($this->version) . "'
WHERE class = 'ooc_phpthumb_img'");
}
function disable_extension()
{
global $DB;
$DB->query("DELETE FROM exp_extensions WHERE class = 'ooc_phpthumb_img'");
}
}
?>
