<commit_before><?php
/**
 * Created by PhpStorm.
 * User: robert
 * Date: 7/2/15
 * Time: 10:33 AM
 */

namespace Enpowi\Files;

use Respect\Validation\Validator as v;
use Imagick;

class EntityImage
{
	public static $thumbnailWidth = 220;
	public static $thumbnailHeight = 220;

	public static $basePath = '/protected/';
	public $entityName = null;
	public $entityId = null;
	public $uploadData = [];

	private $oldEntityName = null;
	private $oldEntityId = null;

	public function setEntityName($value)
	{
		if ($this->oldEntityName === $this->entityName) {
			$this->oldEntityName =
			$this->entityName = $value;
		} else {
			$this->entityName = $value;
		}
		return $this;
	}

	public function setEntityId($value)
	{
		if ($this->oldEntityId === $this->entityId) {
			$this->oldEntityId =
			$this->entityId = $value;
		} else {
			$this->entityId = $value;
		}
		return $this;
	}

	public function upload()
	{
		if ($this->entityName === null || $this->entityId === null) {
			throw new \Exception('property entityName and entityId must be set to other than null');
		}

		$dir = $this->dir();
		if (!file_exists($dir)) return false;

		$path = $this->path();
		$tmp = $this->uploadData['tmp_name'];
		$check = getimagesize($tmp);

		if($check === false) {
			return false;
		}

		switch ($check['mime']) {
			case image_type_to_mime_type(IMAGETYPE_GIF):
			case image_type_to_mime_type(IMAGETYPE_PNG):
			case image_type_to_mime_type(IMAGETYPE_JPEG):
				break;
			default:
				return false;
		}

		if (move_uploaded_file($tmp, $path)) {
			$this->thumbnail();
			return true;
		}

		return false;
	}

	public function thumbnail()
	{
		$path = $this->path();

		if (file_exists($path)) {
			$imagick = new Imagick( $path );<commit_msg>extra checks for entity image<commit_after><?php
/**
 * Created by PhpStorm.
 * User: robert
 * Date: 7/2/15
 * Time: 10:33 AM
 */

namespace Enpowi\Files;

use Respect\Validation\Validator as v;
use Imagick;

class EntityImage
{
	public static $thumbnailWidth = 220;
	public static $thumbnailHeight = 220;

	public static $basePath = '/protected/';
	public $entityName = null;
	public $entityId = null;
	public $uploadData = [];

	private $oldEntityName = null;
	private $oldEntityId = null;

	public function setEntityName($value)
	{
		if ($this->oldEntityName === $this->entityName) {
			$this->oldEntityName =
			$this->entityName = $value;
		} else {
			$this->entityName = $value;
		}
		return $this;
	}

	public function setEntityId($value)
	{
		if ($this->oldEntityId === $this->entityId) {
			$this->oldEntityId =
			$this->entityId = $value;
		} else {
			$this->entityId = $value;
		}
		return $this;
	}

	public function upload()
	{
		if ($this->entityName === null || $this->entityId === null) {
			throw new \Exception('property entityName and entityId must be set to other than null');
		}

		$dir = $this->dir();
		if (!file_exists($dir)) return false;

		$path = $this->path();
		$tmp = $this->uploadData['tmp_name'];

		if (!file_exists($tmp)) return false;

		try {
			$check = getimagesize($tmp);
		} catch (\Exception $e) {
			return false;
		}

		if($check === false) {
			return false;
		}

		switch ($check['mime']) {
			case image_type_to_mime_type(IMAGETYPE_GIF):
			case image_type_to_mime_type(IMAGETYPE_PNG):
			case image_type_to_mime_type(IMAGETYPE_JPEG):
				break;
			default:
				return false;
		}

		if (move_uploaded_file($tmp, $path)) {
			$this->thumbnail();
			return true;
		}

		return false;
	}

	public function thumbnail()
	{
		$path = $this->path();

		if (file_exists($path)) {
			$imagick = new Imagick( $path );
{'fraction_non_alphanumeric': 0.15491393670183232, 'fraction_numerical': 0.007773459189339256, 'mean_word_length': 3.6550387596899223, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '3092099', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1277, 'n_words': 300}