<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/>.
*
*/
/**
 * Class for basic image manipulation
 */
class OC_Image {
	protected $resource = false; // tmp resource.
	protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident.
	protected $mimeType = "image/png"; // Default to png
	protected $bitDepth = 24;
	protected $filePath = null;

	private $fileInfo;

	/**
	* @brief Get mime type for an image file.
	* @param string|null $filepath The path to a local image file.
	* @returns string The mime type if the it could be determined, otherwise an empty string.
	*/
	static public function getMimeTypeForFile($filePath) {
		// exif_imagetype throws "read error!" if file is less than 12 byte
		if (filesize($filePath) > 11) {
			$imageType = exif_imagetype($filePath);
		} else {
			$imageType = false;
		}
		return $imageType ? image_type_to_mime_type($imageType) : '';
	}

	/**
	* @brief Constructor.
	* @param string|resource $imageref The path to a local file, a base64 encoded string or a resource created by an imagecreate* function.
	* @returns bool False on error
	*/
	public function __construct($imageRef = null) {
		//OC_Log::write('core',__METHOD__.'(): start', OC_Log::DEBUG);
		if(!extension_loaded('gd') || !function_exists('gd_info')) {
			OC_Log::write('core', __METHOD__.'(): GD module not installed', OC_Log::ERROR);
			return false;
		}

		if (\OC_Util::fileInfoLoaded()) {
			$this->fileInfo = new finfo(FILEINFO_MIME_TYPE);
		}

		if(!is_null($imageRef)) {
			$this->load($imageRef);
		}
	}

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

	/**
	* @brief Returns the MIME type of the image or an empty string if no image is loaded.
	* @returns int
	*/
	public function mimeType() {
		return $this->valid() ? $this->mimeType : '';
	}

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

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

	/**
	* @brief Returns the width when the image orientation is top-left.
	* @returns int
	*/
	public function widthTopLeft() {
		$o = $this->getOrientation();
		OC_Log::write('core', 'OC_Image->widthTopLeft() Orientation: '.$o, OC_Log::DEBUG);
		switch($o) {
			case -1:
			case 1:
			case 2: // Not tested
			case 3:
			case 4: // Not tested
				return $this->width();
			case 5: // Not tested
			case 6:
			case 7: // Not tested
			case 8:
				return $this->height();
		}
		return $this->width();
	}

	/**
	* @brief Returns the height when the image orientation is top-left.
	* @returns int
	*/
	public function heightTopLeft() {
		$o = $this->getOrientation();
		OC_Log::write('core', 'OC_Image->heightTopLeft() Orientation: '.$o, OC_Log::DEBUG);
		switch($o) {
			case -1:
			case 1:
			case 2: // Not tested
			case 3:
			case 4: // Not tested
				return $this->height();
			case 5: // Not tested
			case 6:
			case 7: // Not tested
			case 8:
				return $this->width();
		}
		return $this->height();
	}

	/**
	* @brief Outputs the image.
	* @returns bool
	*/
	public function show($mimeType=null) {
		if($mimeType === null) {
			$mimeType = $this->mimeType();
		}
		header('Content-Type: '.$mimeType);
		return $this->_output(null, $mimeType);
	}

	/**
	* @brief Saves the image.
	* @returns bool
	* @param string $filePath
	*/

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

	/**
	* @brief Outputs/saves the image.
	*/
	private function _output($filePath=null, $mimeType=null) {
		if($filePath) {
			if (!file_exists(dirname($filePath)))
				mkdir(dirname($filePath), 0777, true);
			if(!is_writable(dirname($filePath))) {
				OC_Log::write('core',
					__METHOD__.'(): Directory \''.dirname($filePath).'\' is not writable.',
					OC_Log::ERROR);
				return false;
			} elseif(is_writable(dirname($filePath)) && file_exists($filePath) && !is_writable($filePath)) {
				OC_Log::write('core', __METHOD__.'(): File \''.$filePath.'\' is not writable.', OC_Log::ERROR);
				return false;
			}
		}
		if (!$this->valid()) {
			return false;
		}

		$imageType = null;
		if($mimeType !== null) {
			switch($mimeType) {
				case 'image/gif':
					$imageType = IMAGETYPE_GIF;
					break;
				case 'image/jpeg':
					$imageType = IMAGETYPE_JPEG;
					break;
				case 'image/png':
					$imageType = IMAGETYPE_PNG;
					break;
				case 'image/x-xbitmap':
					$imageType = IMAGETYPE_XBM;
					break;
				case 'image/bmp':
					$imageType = IMAGETYPE_BMP;
					break;
				default:
					$imageType = IMAGETYPE_PNG;
					break;
			}
		} else {
			$imageType = $this->imageType;
		}

		switch($imageType) {
			case IMAGETYPE_GIF:
				$retVal = imagegif($this->resource, $filePath);
				break;
			case IMAGETYPE_JPEG:
				$retVal = imagejpeg($this->resource, $filePath);
				break;
			case IMAGETYPE_PNG:
				$retVal = imagepng($this->resource, $filePath);
				break;
			case IMAGETYPE_XBM:
				$retVal = imagexbm($this->resource, $filePath);<commit_msg>OC_Image::_output() - throw exception instead of falling back to png<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/>.
*
*/
/**
 * Class for basic image manipulation
 */
class OC_Image {
	protected $resource = false; // tmp resource.
	protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident.
	protected $mimeType = "image/png"; // Default to png
	protected $bitDepth = 24;
	protected $filePath = null;

	private $fileInfo;

	/**
	* @brief Get mime type for an image file.
	* @param string|null $filepath The path to a local image file.
	* @returns string The mime type if the it could be determined, otherwise an empty string.
	*/
	static public function getMimeTypeForFile($filePath) {
		// exif_imagetype throws "read error!" if file is less than 12 byte
		if (filesize($filePath) > 11) {
			$imageType = exif_imagetype($filePath);
		} else {
			$imageType = false;
		}
		return $imageType ? image_type_to_mime_type($imageType) : '';
	}

	/**
	* @brief Constructor.
	* @param string|resource $imageref The path to a local file, a base64 encoded string or a resource created by an imagecreate* function.
	* @returns bool False on error
	*/
	public function __construct($imageRef = null) {
		//OC_Log::write('core',__METHOD__.'(): start', OC_Log::DEBUG);
		if(!extension_loaded('gd') || !function_exists('gd_info')) {
			OC_Log::write('core', __METHOD__.'(): GD module not installed', OC_Log::ERROR);
			return false;
		}

		if (\OC_Util::fileInfoLoaded()) {
			$this->fileInfo = new finfo(FILEINFO_MIME_TYPE);
		}

		if(!is_null($imageRef)) {
			$this->load($imageRef);
		}
	}

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

	/**
	* @brief Returns the MIME type of the image or an empty string if no image is loaded.
	* @returns int
	*/
	public function mimeType() {
		return $this->valid() ? $this->mimeType : '';
	}

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

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

	/**
	* @brief Returns the width when the image orientation is top-left.
	* @returns int
	*/
	public function widthTopLeft() {
		$o = $this->getOrientation();
		OC_Log::write('core', 'OC_Image->widthTopLeft() Orientation: '.$o, OC_Log::DEBUG);
		switch($o) {
			case -1:
			case 1:
			case 2: // Not tested
			case 3:
			case 4: // Not tested
				return $this->width();
			case 5: // Not tested
			case 6:
			case 7: // Not tested
			case 8:
				return $this->height();
		}
		return $this->width();
	}

	/**
	* @brief Returns the height when the image orientation is top-left.
	* @returns int
	*/
	public function heightTopLeft() {
		$o = $this->getOrientation();
		OC_Log::write('core', 'OC_Image->heightTopLeft() Orientation: '.$o, OC_Log::DEBUG);
		switch($o) {
			case -1:
			case 1:
			case 2: // Not tested
			case 3:
			case 4: // Not tested
				return $this->height();
			case 5: // Not tested
			case 6:
			case 7: // Not tested
			case 8:
				return $this->width();
		}
		return $this->height();
	}

	/**
	* @brief Outputs the image.
	* @returns bool
	*/
	public function show($mimeType=null) {
		if($mimeType === null) {
			$mimeType = $this->mimeType();
		}
		header('Content-Type: '.$mimeType);
		return $this->_output(null, $mimeType);
	}

	/**
	* @brief Saves the image.
	* @returns bool
	* @param string $filePath
	*/

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

	/**
	* @brief Outputs/saves the image.
	*/
	private function _output($filePath=null, $mimeType=null) {
		if($filePath) {
			if (!file_exists(dirname($filePath)))
				mkdir(dirname($filePath), 0777, true);
			if(!is_writable(dirname($filePath))) {
				OC_Log::write('core',
					__METHOD__.'(): Directory \''.dirname($filePath).'\' is not writable.',
					OC_Log::ERROR);
				return false;
			} elseif(is_writable(dirname($filePath)) && file_exists($filePath) && !is_writable($filePath)) {
				OC_Log::write('core', __METHOD__.'(): File \''.$filePath.'\' is not writable.', OC_Log::ERROR);
				return false;
			}
		}
		if (!$this->valid()) {
			return false;
		}

		$imageType = null;
		if($mimeType !== null) {
			switch($mimeType) {
				case 'image/gif':
					$imageType = IMAGETYPE_GIF;
					break;
				case 'image/jpeg':
					$imageType = IMAGETYPE_JPEG;
					break;
				case 'image/png':
					$imageType = IMAGETYPE_PNG;
					break;
				case 'image/x-xbitmap':
					$imageType = IMAGETYPE_XBM;
					break;
				case 'image/bmp':
					$imageType = IMAGETYPE_BMP;
					break;
				default:
					throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format');
					break;
			}
		} else {
			$imageType = $this->imageType;
		}

		switch($imageType) {
			case IMAGETYPE_GIF:
				$retVal = imagegif($this->resource, $filePath);
				break;
			case IMAGETYPE_JPEG:
				$retVal = imagejpeg($this->resource, $filePath);
				break;
			case IMAGETYPE_PNG:
				$retVal = imagepng($this->resource, $filePath);
				break;
			case IMAGETYPE_XBM:
				$retVal = imagexbm($this->resource, $filePath);
{'fraction_non_alphanumeric': 0.15007824726134586, 'fraction_numerical': 0.006103286384976526, 'mean_word_length': 3.4255540166204987, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 79, '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': '4401091', 'n_tokens_mistral': 4930, 'n_tokens_neox': 4248, 'n_words': 1260}