Example #1
0
File: i.php Project: donseba/Piwigo
function try_switch_source(DerivativeParams $params, $original_mtime)
{
    global $page;
    if (!isset($page['original_size'])) {
        return false;
    }
    $original_size = $page['original_size'];
    if ($page['rotation_angle'] == 90 || $page['rotation_angle'] == 270) {
        $tmp = $original_size[0];
        $original_size[0] = $original_size[1];
        $original_size[1] = $tmp;
    }
    $dsize = $params->compute_final_size($original_size);
    $use_watermark = $params->use_watermark;
    if ($use_watermark) {
        $use_watermark = $params->will_watermark($dsize);
    }
    $candidates = array();
    foreach (ImageStdParams::get_defined_type_map() as $candidate) {
        if ($candidate->type == $params->type) {
            continue;
        }
        if ($candidate->use_watermark != $use_watermark) {
            continue;
        }
        if ($candidate->max_width() < $params->max_width() || $candidate->max_height() < $params->max_height()) {
            continue;
        }
        $candidate_size = $candidate->compute_final_size($original_size);
        if ($dsize != $params->compute_final_size($candidate_size)) {
            continue;
        }
        if ($params->sizing->max_crop == 0) {
            if ($candidate->sizing->max_crop != 0) {
                continue;
            }
        } else {
            if ($use_watermark && $candidate->use_watermark) {
                continue;
            }
            //a square that requires watermark should not be generated from a larger derivative with watermark, because if the watermark is not centered on the large image, it will be cropped.
            if ($candidate->sizing->max_crop != 0) {
                continue;
            }
            // this could be optimized
            if ($candidate_size[0] < $params->sizing->min_size[0] || $candidate_size[1] < $params->sizing->min_size[1]) {
                continue;
            }
        }
        $candidates[] = $candidate;
    }
    foreach (array_reverse($candidates) as $candidate) {
        $candidate_path = $page['derivative_path'];
        $candidate_path = str_replace('-' . derivative_to_url($params->type), '-' . derivative_to_url($candidate->type), $candidate_path);
        $candidate_mtime = @filemtime($candidate_path);
        if ($candidate_mtime === false || $candidate_mtime < $original_mtime || $candidate_mtime < $candidate->last_mod_time) {
            continue;
        }
        $params->use_watermark = false;
        $params->sharpen = min(1, $params->sharpen);
        $page['src_path'] = $candidate_path;
        $page['src_url'] = $page['root_path'] . substr($candidate_path, strlen(PHPWG_ROOT_PATH));
        $page['rotation_angle'] = 0;
        return true;
    }
    return false;
}
Example #2
0
/**
 * Deletes derivatives of a particular element
 *
 * @param array $infos ('path'[, 'representative_ext'])
 * @param 'all'|int $type
 */
function delete_element_derivatives($infos, $type = 'all')
{
    $path = $infos['path'];
    if (!empty($infos['representative_ext'])) {
        $path = original_to_representative($path, $infos['representative_ext']);
    }
    if (substr_compare($path, '../', 0, 3) == 0) {
        $path = substr($path, 3);
    }
    $dot = strrpos($path, '.');
    if ($type == 'all') {
        $pattern = '-*';
    } else {
        $pattern = '-' . derivative_to_url($type) . '*';
    }
    $path = substr_replace($path, $pattern, $dot, 0);
    if (($glob = glob(PHPWG_ROOT_PATH . PWG_DERIVATIVE_DIR . $path)) !== false) {
        foreach ($glob as $file) {
            @unlink($file);
        }
    }
}