/** * Record where reduced images are being used so that we can delete them later if they are no longer referenced * ... no guarantee the reduced image won't be copy & pasted into other pages.. page copies would need to track the data as well * */ static function ResizedImageUse($list_before, $list_after) { global $dataDir; //subtract uses no longer $subtract_use = $list_before; foreach ($list_before as $index => $sizes) { if (isset($list_after[$index])) { $subtract_use[$index] = array_diff($list_before[$index], $list_after[$index]); } } //add uses $add_use = $list_after; foreach ($add_use as $index => $sizes) { if (isset($list_before[$index])) { $add_use[$index] = array_diff($list_after[$index], $list_before[$index]); } } //save info for each image $all_imgs = array_keys($subtract_use + $add_use); foreach ($all_imgs as $index) { $edited = false; $usage = gp_resized::GetUsage($index); //add uses if (isset($add_use[$index]) && count($add_use[$index])) { $edited = true; foreach ($add_use[$index] as $size) { if (isset($usage[$size])) { $usage[$size]['uses']++; } else { $usage[$size]['uses'] = 1; $usage[$size]['created'] = time(); } $usage[$size]['touched'] = time(); } } //remove uses $set_to_zero = array(); if (isset($subtract_use[$index]) && is_array($subtract_use[$index])) { $edited = true; foreach ($subtract_use[$index] as $size) { if (isset($usage[$size])) { $usage[$size]['uses']--; } else { $usage[$size]['uses'] = 0; $usage[$size]['created'] = time(); //shouldn't happen } $usage[$size]['uses'] = max($usage[$size]['uses'], 0); if ($usage[$size]['uses'] == 0) { $set_to_zero[] = $size; } $usage[$size]['touched'] = time(); } } //if uses < 1, delete the file, but not the record foreach ($set_to_zero as $size) { list($width, $height) = explode('x', $size); //make sure the image still exists if (!isset(gp_resized::$index[$index])) { continue; } $img = gp_resized::$index[$index]; $info = gp_resized::ImageInfo($img, $width, $height); if (!$info) { continue; } $full_path = $dataDir . '/data/_resized/' . $index . '/' . $info['name']; if (file_exists($full_path)) { @unlink($full_path); } } //order usage by sizes: small to large uksort($usage, array('gp_edit', 'SizeCompare')); if ($edited) { gp_resized::SaveUsage($index, $usage); } } }
/** * Delete unused images * if uses < 1, delete the file, but not the record * */ private static function DeleteUnused($index, $size) { global $dataDir; list($width, $height) = explode('x', $size); //make sure the image still exists if (!isset(\gp_resized::$index[$index])) { continue; } $img = \gp_resized::$index[$index]; $info = \gp_resized::ImageInfo($img, $width, $height); if (!$info) { continue; } $full_path = $dataDir . '/data/_resized/' . $index . '/' . $info['name']; if (file_exists($full_path)) { @unlink($full_path); } }