<commit_before><?php
namespace Podlove\Model;

use Symfony\Component\Yaml\Yaml;
use \Podlove\Cache\TemplateCache;

/**
 * Image Object
 * 
 * Usage
 * 
 * 	// get url, resized to 100px width, keep aspect ratio
 * 	$image = (new Image($url))->setWidth(100)->url();
 * 
 * 	// get url, resized to 100px width and 50px height, cropped
 * 	$image = (new Image($url))
 *  	->setWidth(100)
 *   	->setHeight(50)
 * 	  	->setCrop(true)
 * 		->url();
 * 
 *   // get image tag with custom alt text and title
 *   $image = (new Image($url))->image(["alt" => "custom alt", "title" => "custom title"]);
 */
class Image {

	// URL/file properties
	private $id;
	private $source_url;
	private $file_name;
	private $file_extension;
	private $upload_basedir;
	private $upload_baseurl;
	
	// image properties
	private $crop   = false;
	private $width  = NULL;
	private $height = NULL;

	// html rendering properties
	private $retina = true;

	/**
	 * Create image object
	 * 
	 * Manage remote image objects. Cache locally so we can resize and serve 
	 * optimized image dimensions.
	 * 
	 * @param string $url  Remote image URL
	 * @param string $name (optional) image file name prefix
	 */
	public function __construct($url, $file_name = '') {
		$this->source_url = trim($url);
		$this->file_name = sanitize_title($file_name);
		
		// manually remove troublemaking invisible character
		// @see https://community.podlove.org/t/solved-kind-of-cover-art-disappears-caching-issue/478/
		// @see https://sendegate.de/t/problem-mit-caching-von-grafiken/2947
		$this->file_name = str_replace("%c2%ad", "", $this->file_name);
		
		$this->file_extension = $this->extract_file_extension();
		$this->id = md5($url . $this->file_name);

		// create subdirectories to avoid too many directories in the root directory
		$id_directory = substr($this->id, 0, 2) . '/' . substr($this->id, 2);

		$this->upload_basedir = self::cache_dir() . $id_directory;
		$this->upload_baseurl = content_url('cache/podlove/') . $id_directory;
	}

	public static function cache_dir() {
		return trailingslashit(WP_CONTENT_DIR) . 'cache/podlove/';
	}

	/**
	 * Delete all image caches.
	 */
	public static function flush_cache() {

		$dir = self::cache_dir();

		if (!file_exists($dir))
			return;

		$it    = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
		$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);<commit_msg>case insensitive invisible character search<commit_after><?php
namespace Podlove\Model;

use Symfony\Component\Yaml\Yaml;
use \Podlove\Cache\TemplateCache;

/**
 * Image Object
 * 
 * Usage
 * 
 * 	// get url, resized to 100px width, keep aspect ratio
 * 	$image = (new Image($url))->setWidth(100)->url();
 * 
 * 	// get url, resized to 100px width and 50px height, cropped
 * 	$image = (new Image($url))
 *  	->setWidth(100)
 *   	->setHeight(50)
 * 	  	->setCrop(true)
 * 		->url();
 * 
 *   // get image tag with custom alt text and title
 *   $image = (new Image($url))->image(["alt" => "custom alt", "title" => "custom title"]);
 */
class Image {

	// URL/file properties
	private $id;
	private $source_url;
	private $file_name;
	private $file_extension;
	private $upload_basedir;
	private $upload_baseurl;
	
	// image properties
	private $crop   = false;
	private $width  = NULL;
	private $height = NULL;

	// html rendering properties
	private $retina = true;

	/**
	 * Create image object
	 * 
	 * Manage remote image objects. Cache locally so we can resize and serve 
	 * optimized image dimensions.
	 * 
	 * @param string $url  Remote image URL
	 * @param string $name (optional) image file name prefix
	 */
	public function __construct($url, $file_name = '') {
		$this->source_url = trim($url);
		$this->file_name = sanitize_title($file_name);
		
		// manually remove troublemaking invisible character
		// @see https://community.podlove.org/t/solved-kind-of-cover-art-disappears-caching-issue/478/
		// @see https://sendegate.de/t/problem-mit-caching-von-grafiken/2947
		$this->file_name = str_ireplace("%c2%ad", "", $this->file_name);
		
		$this->file_extension = $this->extract_file_extension();
		$this->id = md5($url . $this->file_name);

		// create subdirectories to avoid too many directories in the root directory
		$id_directory = substr($this->id, 0, 2) . '/' . substr($this->id, 2);

		$this->upload_basedir = self::cache_dir() . $id_directory;
		$this->upload_baseurl = content_url('cache/podlove/') . $id_directory;
	}

	public static function cache_dir() {
		return trailingslashit(WP_CONTENT_DIR) . 'cache/podlove/';
	}

	/**
	 * Delete all image caches.
	 */
	public static function flush_cache() {

		$dir = self::cache_dir();

		if (!file_exists($dir))
			return;

		$it    = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
		$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
{'fraction_non_alphanumeric': 0.15096642929806714, 'fraction_numerical': 0.011393692777212614, 'mean_word_length': 3.74975845410628, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 4, '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': '2805406', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1625, 'n_words': 397}