Beispiel #1
0
/**
 * Resize image to speciffic dimetions.
 *
 * Magick - do not touch!
 *
 * Evaluate new width and height.
 * $img - image meta array ($img[0] - image url, $img[1] - width, $img[2] - height).
 * $opts - options array, supports w, h, zc, a, q.
 *
 * @param array $img
 * @param 
 * @return array
 */
function dt_get_resized_img( $img, $opts ) {

	$opts = apply_filters( 'dt_get_resized_img-options', $opts, $img );

	if ( !is_array( $img ) || !$img || (!$img[1] && !$img[2]) ) {
		return false;
	}

	if ( !is_array( $opts ) || !$opts ) {

		if ( !isset( $img[3] ) ) {

			$img[3] = image_hwstring( $img[1], $img[2] );
		}

		return $img;
	}

	if ( !isset($opts['hd_convert']) ) {
		$opts['hd_convert'] = true;
	}

	$defaults = array( 'w' => 0, 'h' => 0 , 'zc' => 1, 'z'	=> 1 );
	$opts = wp_parse_args( $opts, $defaults );

	$w = absint( $opts['w'] );
	$h = absint( $opts['h'] );

	// If zoomcropping off and image smaller then required square
	if ( 0 == $opts['zc'] && ( $img[1] <= $w  && $img[2] <= $h ) ) {

		return array( $img[0], $img[1], $img[2], image_hwstring( $img[1], $img[2] ) );

	} elseif ( 3 == $opts['zc'] || empty ( $w ) || empty ( $h ) ) {

		if ( 0 == $opts['z'] ) {
			dt_constrain_dim( $img[1], $img[2], $w, $h, true );
		} else {
			$p = absint( $img[1] ) / absint( $img[2] );
			$hx = absint( floor( $w / $p ) ); 
			$wx = absint( floor( $h * $p ) );
			
			if ( empty( $w ) ) {
				$w = $wx;
			} else if ( empty( $h ) ) {
				$h = $hx;
			} else {
				if ( $hx < $h && $wx >= $w ) {
					$h = $hx;
				} elseif ( $wx < $w && $hx >= $h ) {
					$w = $wx;
				}
			}
		}

		if ( $img[1] == $w && $img[2] == $h ) {
			return array( $img[0], $img[1], $img[2], image_hwstring( $img[1], $img[2] ) );
		}

	}

	$img_h = $h;
	$img_w = $w;

	if ( $opts['hd_convert'] && dt_retina_on() && dt_is_hd_device() ) {
		$img_h *= 2;
		$img_w *= 2;
	}

	if ( 1 == $opts['zc'] ) {

		if ( $img[1] >= $img_w && $img[2] >= $img_h ) {

			// do nothing

		} else if ( $img[1] <= $img[2] && $img_w >= $img_h ) { // img=portrait; c=landscape

			$cw_new = $img[1];
			$k = $cw_new/$img_w;
			$ch_new = $k * $img_h;

		} else if ( $img[1] >= $img[2] && $img_w <= $img_h ) { // img=landscape; c=portrait

			$ch_new = $img[2];
			$k = $ch_new/$img_h;
			$cw_new = $k * $img_w;

		} else {

			$kh = $img_h/$img[2];
			$kw = $img_w/$img[1];
			$kres = max( $kh, $kw );
			$ch_new = $img_h/$kres;
			$cw_new = $img_w/$kres;

		}

		if ( isset($ch_new, $cw_new) ) {
			$img_h = absint(floor($ch_new));
			$img_w = absint(floor($cw_new));
		}

	}

	$file_url = aq_resize( $img[0], $img_w, $img_h, true, true, false );

	if ( !$file_url ) {
		$file_url = $img[0];
	}

	return array(
		$file_url,
		$w,
		$h,
		image_hwstring( $w, $h )
	);
}
Beispiel #2
0
/**
 * add timthumb with args to img url
 * evaluate new width and height
 * $img - image meta array ($img[0] - image url, $img[1] - width, $img[2] - height)
 * $opts - options array, supports w, h, zc, a, q (see timthumb documentation)
 * @params: $img = array(), $opts = array()
 * return: array
 */
function dt_get_resized_img($img, $opts = array())
{
    if (!is_array($img)) {
        $img[0] = get_template_directory_uri() . '/images/noimage.jpg';
        $img[1] = $img[2] = 1000;
        $img[3] = image_hwstring($img[1], $img[2]);
    }
    if (empty($opts) || !is_array($opts)) {
        return $img;
    }
    $defaults = array('w' => 0, 'h' => 0, 'zc' => 1, 'force_zc' => 0);
    $opts = wp_parse_args($opts, $defaults);
    $w = $opts['w'];
    $h = $opts['h'];
    if ($img[1] > $w && $img[2] > $h) {
        if (3 == $opts['zc'] || empty($w) || empty($h)) {
            dt_constrain_dim($img[1], $img[2], $w, $h, true);
        }
    } elseif (empty($w) || empty($h)) {
        $prop = $img[1] / $img[2];
        if (empty($w)) {
            $w = $h * $prop;
        } else {
            $h = $w / $prop;
        }
    } elseif (!$opts['zc'] && !$opts['force_zc']) {
        return array($img[0], $img[1], $img[2], image_hwstring($img[1], $img[2]));
    }
    $args['src'] = dt_clean_thumb_url($img[0]);
    if (isset($opts['zc'])) {
        $args['zc'] = $opts['zc'];
    }
    if (!empty($opts['w'])) {
        $args['w'] = $opts['w'];
    }
    if (!empty($opts['h'])) {
        $args['h'] = $opts['h'];
    }
    if (isset($opts['a'])) {
        $args['a'] = $opts['a'];
    }
    if (isset($opts['q'])) {
        $args['q'] = $opts['q'];
    }
    if (isset($opts['r_src']) && !empty($opts['r_src'])) {
        $args['r_src'] = dt_clean_thumb_url($opts['r_src']);
    }
    return array(esc_url(add_query_arg($args, get_template_directory_uri() . '/timthumb.php')), $w, $h, image_hwstring($w, $h));
}