<commit_before><?php

// $Id$

if(!isset($config))
	require_once '../../util/configLoader.inc';
$config =&configLoader::getInstance();

require_once $config->get('sysRoot').'alpha/util/handle_error.inc';

require_once $config->get('sysRoot').'alpha/model/types/String.inc';
require_once $config->get('sysRoot').'alpha/model/types/Integer.inc';
require_once $config->get('sysRoot').'alpha/model/types/Double.inc';
require_once $config->get('sysRoot').'alpha/model/types/Enum.inc';
require_once $config->get('sysRoot').'alpha/model/types/Boolean.inc';

/**
* Scalable image custom widget
* 
* @package Alpha Widgets
* @author <NAME> <<EMAIL>>
* @copyright 2007 <NAME>
*
*/
class image
{
	/**
	 * the title of the image for alt text
	 * @var string
	 */
	var $title;
	
	/**
	 * the path to the source image
	 * @var string
	 */
	var $source;
	
	/**
	 * the width of the image (can also be a javascript function)
	 * @var String
	 */
	var $width;
	
	/**
	 * the height of the image (can also be a javascript function)
	 * @var String
	 */
	var $height;
	
	/**
	 * the file type of the source image
	 * @var Enum
	 */
	var $sourceType;
	
	/**
	 * the quality of the image generated (0.00 to 1.00, 0.75 by default)
	 * @var Double
	 */
	var $quality;
	
	/**
	 * flag to determine if the image will scale to match resolution (0 by default)
	 * a default resoultion of 1024x768 is assumed for scalable images.
	 * @var Boolean 
	 */
	var $scale;
	
	/**
	 * flag if you want only create the image in the cache but not render it to the browser
	 * @var Boolean
	 */
	var $cache_only;
	
	/**
	 * the auto-generated name of the cache file for the image
	 * @var string
	 */
	var $filename;
	
	/**
	 * the constructor (the parameters for this can also be set in GET vars
	 * @param string $source the path to the source image
	 * @param string $width
	 * @param string $height
	 * @param string $sourceType
	 * @param double $quality
	 * @param Boolean $scale;
	 * @param Boolean $render_image;
	 */
	function image($source="", $width=0, $height=0, $sourceType="png", $quality=0.75, $scale=0, $cache_only=0) {
		global $config;
		
		$this->source = $source;
		$this->width = new String($width);
		$this->height = new String($height);
		$this->sourceType = new Enum(array("gif",
										"jpg",
										"png"));
		$this->sourceType->set_value($sourceType);
		$this->quality = new Double($quality);
		$this->scale = new Boolean($scale);	
		$this->cache_only = new Boolean($cache_only);
					
		if (isset($_GET["source"])) $this->source = $_GET["source"];
		if (isset($_GET["width"])) $this->width->set_value($_GET["width"]);
		if (isset($_GET["height"])) $this->height->set_value($_GET["height"]);
		if (isset($_GET["sourceType"])) $this->sourceType->set_value($_GET["sourceType"]);
		if (isset($_GET["quality"])) $this->quality->set_value($_GET["quality"]);
		
		$this->filename = $config->get('sysRoot').'cache/images/'.basename($this->source, ".".$this->sourceType->get_value()).'_'.$this->width->get_value().'x'.$this->height->get_value().'.jpg';
	
		// if GET vars where provided, then render the image, otherwise render the JavaScript call for the image creation
		if (isset($_GET["source"]) || $this->cache_only->get_value())
			$this->render_image();
		else
			$this->render();
	}
	
	/**
	 * renders the actual image using GD library calls	 
	 */
	function render_image() {
		global $config;
		
		// check the image cache first before we proceed
		if ($this->check_cache()) {
			$this->load_cache();			
		}else{
			// now get the old image
			switch ($this->sourceType->get_value()) {
				case "gif":
					$old_image = imagecreatefromgif($config->get('sysRoot').$this->source);<commit_msg>Added the set_filename() method which aims to reduce namespace issues with cached images when the image widget is used widely in the CMS<commit_after><?php

// $Id$

if(!isset($config))
	require_once '../../util/configLoader.inc';
$config =&configLoader::getInstance();

require_once $config->get('sysRoot').'alpha/util/handle_error.inc';

require_once $config->get('sysRoot').'alpha/model/types/String.inc';
require_once $config->get('sysRoot').'alpha/model/types/Integer.inc';
require_once $config->get('sysRoot').'alpha/model/types/Double.inc';
require_once $config->get('sysRoot').'alpha/model/types/Enum.inc';
require_once $config->get('sysRoot').'alpha/model/types/Boolean.inc';

/**
* Scalable image custom widget
* 
* @package Alpha Widgets
* @author <NAME> <<EMAIL>>
* @copyright 2007 <NAME>
*
*/
class image
{
	/**
	 * the title of the image for alt text
	 * @var string
	 */
	var $title;
	
	/**
	 * the path to the source image
	 * @var string
	 */
	var $source;
	
	/**
	 * the width of the image (can also be a javascript function)
	 * @var String
	 */
	var $width;
	
	/**
	 * the height of the image (can also be a javascript function)
	 * @var String
	 */
	var $height;
	
	/**
	 * the file type of the source image
	 * @var Enum
	 */
	var $sourceType;
	
	/**
	 * the quality of the image generated (0.00 to 1.00, 0.75 by default)
	 * @var Double
	 */
	var $quality;
	
	/**
	 * flag to determine if the image will scale to match resolution (0 by default)
	 * a default resoultion of 1024x768 is assumed for scalable images.
	 * @var Boolean 
	 */
	var $scale;
	
	/**
	 * flag if you want only create the image in the cache but not render it to the browser
	 * @var Boolean
	 */
	var $cache_only;
	
	/**
	 * the auto-generated name of the cache file for the image
	 * @var string
	 */
	var $filename;
	
	/**
	 * the constructor (the parameters for this can also be set in GET vars
	 * @param string $source the path to the source image
	 * @param string $width
	 * @param string $height
	 * @param string $sourceType
	 * @param double $quality
	 * @param Boolean $scale;
	 * @param Boolean $render_image;
	 */
	function image($source="", $width=0, $height=0, $sourceType="png", $quality=0.75, $scale=0, $cache_only=0) {
		global $config;
		
		$this->source = $source;
		$this->width = new String($width);
		$this->height = new String($height);
		$this->sourceType = new Enum(array("gif",
										"jpg",
										"png"));
		$this->sourceType->set_value($sourceType);
		$this->quality = new Double($quality);
		$this->scale = new Boolean($scale);	
		$this->cache_only = new Boolean($cache_only);
					
		if (isset($_GET["source"])) $this->source = $_GET["source"];
		if (isset($_GET["width"])) $this->width->set_value($_GET["width"]);
		if (isset($_GET["height"])) $this->height->set_value($_GET["height"]);
		if (isset($_GET["sourceType"])) $this->sourceType->set_value($_GET["sourceType"]);
		if (isset($_GET["quality"])) $this->quality->set_value($_GET["quality"]);
		
		$this->set_filename($this->source);
		
		// if GET vars where provided, then render the image, otherwise render the JavaScript call for the image creation
		if (isset($_GET["source"]) || $this->cache_only->get_value())
			$this->render_image();
		else
			$this->render();
	}
	
	/**
	 * setter for the file name, creates a sub-directory under /cache for attachment images when required
	 */
	function set_filename($source) {
		global $config;
		
		if(!strpos($source, 'attachments/article_')) {
			$this->filename = $config->get('sysRoot').'cache/images/'.basename($this->source, ".".$this->sourceType->get_value()).'_'.$this->width->get_value().'x'.$this->height->get_value().'.jpg';
		}else{
			// make a cache dir for the article
			$cache_dir = $config->get('sysRoot').'cache/images/article_'.substr($source, strpos($source, 'attachments/article_')+20, 11);
			if(!file_exists($cache_dir)) {
				$success = mkdir($cache_dir);
			
				if (!$success) {
					$error = new handle_error($_SERVER["PHP_SELF"],'Unable to create the folder '.$cache_dir.' for the cache image, source file is '.$source.'.' ,'set_filename()','framework');
				}
				
				// ...and set write permissions on the folder
				$success = chmod($cache_dir, 0777);
					
				if (!$success) {
					$error = new handle_error($_SERVER["PHP_SELF"],'Unable to set write permissions on the folder '.$cache_dir.'.','set_filename()','framework');
				}
			}
			
			// now set the filename to include the new cache directory
			$this->filename = $cache_dir.'/'.basename($this->source, ".".$this->sourceType->get_value()).'_'.$this->width->get_value().'x'.$this->height->get_value().'.jpg';
		}
	}
	
	/**
	 * renders the actual image using GD library calls	 
	 */
	function render_image() {
		global $config;
		
		// check the image cache first before we proceed
		if ($this->check_cache()) {
			$this->load_cache();			
		}else{
			// now get the old image
			switch ($this->sourceType->get_value()) {
				case "gif":
					$old_image = imagecreatefromgif($config->get('sysRoot').$this->source);
{'fraction_non_alphanumeric': 0.17400275103163687, 'fraction_numerical': 0.007336084364970197, 'mean_word_length': 3.7418478260869565, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 108, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4301336', 'n_tokens_mistral': 3101, 'n_tokens_neox': 2715, 'n_words': 770}