<commit_before><?php

/**
* ownCloud
*
* @author <NAME>
* @copyright 2011 <NAME> <<EMAIL>>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*
*/

/** From user comments at http://dk2.php.net/manual/en/function.exif-imagetype.php
 * Don't know if it can come in handy?
if ( ! function_exists( 'exif_imagetype' ) ) {
    function exif_imagetype ( $filename ) {
        if ( ( list($width, $height, $type, $attr) = getimagesize( $filename ) ) !== false ) {
            return $type;
        }
    return false;
    }
}
*/

function ellipsis($str, $maxlen) {
	if (strlen($str) > $maxlen) {
		$characters = floor($maxlen / 2);
		return substr($str, 0, $characters) . '...' . substr($str, -1 * $characters);
	}
	return $str;
}

/**
 * Class for image manipulation
 * Ideas: imagerotate, chunk_split(base64_encode())
 *
 */
class OC_Image {
	static private $resource = false; // tmp resource.
	static private $destroy = false; // if the resource is created withing the object.
	static private $imagetype = IMAGETYPE_PNG; // Default to png if file type isn't evident.
	static private $filepath = null;
	/**
	* @brief Constructor.
	* @param $imageref The path to a local file, a base64 encoded string or a resource created by an imagecreate* function.
	*					If a resource is passed it is the job of the caller to destroy it using imagedestroy($var)
	* @returns bool False on error
	*/
	function __construct($imageref = null) {
		//OC_Log::write('core','OC_Image::__construct, start', OC_Log::DEBUG);
		if(!extension_loaded('gd') || !function_exists('gd_info')) {
		//if(!function_exists('imagecreatefromjpeg')) {
			OC_Log::write('core','OC_Image::__construct, GD module not installed', OC_Log::ERROR);
			return false;
		}
		if(!is_null($imageref)) {
			self::load($imageref);
		}
	}

	/**
	* @brief Destructor.
	*/
	function __destruct() {
		if(is_resource(self::$resource) && self::$destroy) {
			imagedestroy(self::$resource); // Why does this issue a warning.
		}
	}

	/**
	* @brief Determine whether the object contains an image resource.
	* @returns bool
	*/
	public function valid() { // apparently you can't name a method 'empty'...
		$ret = is_resource(self::$resource);
		return $ret;
	}

	/**
	* @brief Returns the MIME type of the image or an empty string if no image is loaded.
	* @returns int
	*/
	public function mimeType() {
		return is_resource(self::$resource) ? image_type_to_mime_type(self::$imagetype) : '';
	}

	/**
	* @brief Returns the width of the image or -1 if no image is loaded.
	* @returns int
	*/
	public function width() {
		return is_resource(self::$resource) ? imagesx(self::$resource) : -1;
	}

	/**
	* @brief Returns the height of the image or -1 if no image is loaded.
	* @returns int
	*/
	public function height() {
		return is_resource(self::$resource) ? imagesy(self::$resource) : -1;
	}

	/**
	* @brief Outputs the image.
	* @returns bool
	*/
	public function show() {
		return $this->_output();
	}

	/**
	* @brief Saves the image.
	* @returns bool
	*/

	public function save($filepath=null) {
		if($filepath === null && $this->filepath === null) {
			OC_Log::write('core','OC_Image::save. save() called with no path.', OC_Log::ERROR);
			return false;
		} elseif($filepath === null && $this->filepath !== null) {
			$filepath = $this->filepath;
		}
		return $this->_output($filepath, true);
	}

	/**
	* @brief Outputs/saves the image.
	*/
	private function _output($filepath=null, $really=false) {
		header('Content-Type: '.self::mimeType());
		if($really === false) {
			$filepath = null; // Just being cautious ;-)<commit_msg>Content type header was sent when saving image to disk.<commit_after><?php

/**
* ownCloud
*
* @author <NAME>
* @copyright 2011 <NAME> <<EMAIL>>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*
*/

/** From user comments at http://dk2.php.net/manual/en/function.exif-imagetype.php
 * Don't know if it can come in handy?
if ( ! function_exists( 'exif_imagetype' ) ) {
    function exif_imagetype ( $filename ) {
        if ( ( list($width, $height, $type, $attr) = getimagesize( $filename ) ) !== false ) {
            return $type;
        }
    return false;
    }
}
*/

function ellipsis($str, $maxlen) {
	if (strlen($str) > $maxlen) {
		$characters = floor($maxlen / 2);
		return substr($str, 0, $characters) . '...' . substr($str, -1 * $characters);
	}
	return $str;
}

/**
 * Class for image manipulation
 * Ideas: imagerotate, chunk_split(base64_encode())
 *
 */
class OC_Image {
	static private $resource = false; // tmp resource.
	static private $destroy = false; // if the resource is created withing the object.
	static private $imagetype = IMAGETYPE_PNG; // Default to png if file type isn't evident.
	static private $filepath = null;
	/**
	* @brief Constructor.
	* @param $imageref The path to a local file, a base64 encoded string or a resource created by an imagecreate* function.
	*					If a resource is passed it is the job of the caller to destroy it using imagedestroy($var)
	* @returns bool False on error
	*/
	function __construct($imageref = null) {
		//OC_Log::write('core','OC_Image::__construct, start', OC_Log::DEBUG);
		if(!extension_loaded('gd') || !function_exists('gd_info')) {
		//if(!function_exists('imagecreatefromjpeg')) {
			OC_Log::write('core','OC_Image::__construct, GD module not installed', OC_Log::ERROR);
			return false;
		}
		if(!is_null($imageref)) {
			self::load($imageref);
		}
	}

	/**
	* @brief Destructor.
	*/
	function __destruct() {
		if(is_resource(self::$resource) && self::$destroy) {
			imagedestroy(self::$resource); // Why does this issue a warning.
		}
	}

	/**
	* @brief Determine whether the object contains an image resource.
	* @returns bool
	*/
	public function valid() { // apparently you can't name a method 'empty'...
		$ret = is_resource(self::$resource);
		return $ret;
	}

	/**
	* @brief Returns the MIME type of the image or an empty string if no image is loaded.
	* @returns int
	*/
	public function mimeType() {
		return is_resource(self::$resource) ? image_type_to_mime_type(self::$imagetype) : '';
	}

	/**
	* @brief Returns the width of the image or -1 if no image is loaded.
	* @returns int
	*/
	public function width() {
		return is_resource(self::$resource) ? imagesx(self::$resource) : -1;
	}

	/**
	* @brief Returns the height of the image or -1 if no image is loaded.
	* @returns int
	*/
	public function height() {
		return is_resource(self::$resource) ? imagesy(self::$resource) : -1;
	}

	/**
	* @brief Outputs the image.
	* @returns bool
	*/
	public function show() {
		return $this->_output();
	}

	/**
	* @brief Saves the image.
	* @returns bool
	*/

	public function save($filepath=null) {
		if($filepath === null && self::$filepath === null) {
			OC_Log::write('core','OC_Image::save. save() called with no path.', OC_Log::ERROR);
			return false;
		} elseif($filepath === null && $this->filepath !== null) {
			$filepath = $this->filepath;
		}
		return $this->_output($filepath, true);
	}

	/**
	* @brief Outputs/saves the image.
	*/
	private function _output($filepath=null, $really=false) {
		if($really === false) {
			header('Content-Type: '.self::mimeType());
			$filepath = null; // Just being cautious ;-)
{'fraction_non_alphanumeric': 0.14377913230548584, 'fraction_numerical': 0.0040635831241783195, 'mean_word_length': 3.735710243350311, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6458856', 'n_tokens_mistral': 2830, 'n_tokens_neox': 2665, 'n_words': 871}