From b97b2999cc287043805ab644901695cf79850a48 Mon Sep 17 00:00:00 2001 From: sunil Date: Fri, 6 Sep 2024 10:37:24 +0100 Subject: [PATCH] made code more readable and keeps thumbnails https://github.com/open768/spaceinc/issues/7 --- curiosity/manifest.php | 55 +++++++++++++++++++----------------------- misc/pichighlight.php | 17 ++----------- 2 files changed, 27 insertions(+), 45 deletions(-) diff --git a/curiosity/manifest.php b/curiosity/manifest.php index 9a39d8a..fcc498f 100644 --- a/curiosity/manifest.php +++ b/curiosity/manifest.php @@ -62,13 +62,13 @@ static function init_db() { //***************************************************************************** private static function pr_replace_sql_params($psSQL) { $sSQL = str_replace(":table", self::MANIFEST_TABLE, $psSQL); - $sSQL = str_replace(":m_col", self::COL_MISSION, $sSQL); - $sSQL = str_replace(":so_col", self::COL_SOL, $sSQL); - $sSQL = str_replace(":sa_col", self::COL_SAMPLE_TYPE, $sSQL); - $sSQL = str_replace(":i_col", self::COL_INSTR, $sSQL); - $sSQL = str_replace(":p_col", self::COL_PRODUCT, $sSQL); - $sSQL = str_replace(":u_col", self::COL_IMAGE_URL, $sSQL); - $sSQL = str_replace(":d_col", self::COL_DATE_ADDED, $sSQL); + $sSQL = str_replace(":mission_col", self::COL_MISSION, $sSQL); + $sSQL = str_replace(":sol_col", self::COL_SOL, $sSQL); + $sSQL = str_replace(":sample_col", self::COL_SAMPLE_TYPE, $sSQL); + $sSQL = str_replace(":instr_col", self::COL_INSTR, $sSQL); + $sSQL = str_replace(":product_col", self::COL_PRODUCT, $sSQL); + $sSQL = str_replace(":url_col", self::COL_IMAGE_URL, $sSQL); + $sSQL = str_replace(":date_col", self::COL_DATE_ADDED, $sSQL); return $sSQL; } @@ -91,21 +91,21 @@ private static function pr_create_table() { cDebug::extra_debug("table doesnt exist " . self::MANIFEST_TABLE); $sSQL = "CREATE TABLE `:table` ( " . - ":m_col TEXT not null, :so_col TEXT not null, :i_col TEXT not null, :p_col TEXT not null, :u_col TEXT not null, :sa_col TEXT, :d_col INTEGER, " . - "CONSTRAINT cmanifest UNIQUE (:m_col, :so_col, :i_col, :p_col) " . + ":mission_col TEXT not null, :sol_col TEXT not null, :instr_col TEXT not null, :product_col TEXT not null, :url_col TEXT not null, :sample_col TEXT, :date_col INTEGER, " . + "CONSTRAINT cmanifest UNIQUE (:mission_col, :sol_col, :instr_col, :product_col) " . ")"; $sSQL = self::pr_replace_sql_params($sSQL); $oSqLDB->query($sSQL); cDebug::extra_debug("table created"); //-------------create INDEX - $sSQL = "CREATE INDEX idx_manifest on ':table' ( :m_col, :so_col, :i_col )"; + $sSQL = "CREATE INDEX idx_manifest on ':table' ( :mission_col, :sol_col, :instr_col )"; $sSQL = self::pr_replace_sql_params($sSQL); $oSqLDB->query($sSQL); cDebug::extra_debug("main index created"); //-------------create INDEX - $sSQL = "CREATE INDEX idx_manifest_date on ':table' ( :d_col )"; + $sSQL = "CREATE INDEX idx_manifest_date on ':table' ( :date_col )"; $sSQL = self::pr_replace_sql_params($sSQL); $oSqLDB->query($sSQL); cDebug::extra_debug("secondary index created"); @@ -188,7 +188,7 @@ static function index_sol($psSol, $pbIgnoreCache) { //***************************************************************************** static function delete_sol_index($psSol) { cDebug::extra_debug("deleting Sol $psSol index"); - $sSQL = "DELETE FROM `:table` where :so_col=:sol"; + $sSQL = "DELETE FROM `:table` where :sol_col=:sol"; $sSQL = self::pr_replace_sql_params($sSQL); $oSqlDB = self::$oSQLDB; @@ -203,7 +203,6 @@ static function add_to_index($psSol, $poItem) { //cDebug::enter(); //--------------get the data out of the item $sSampleType = $poItem->sampleType; - if ($sSampleType === self::SAMPLE_THUMB) return; $sInstr = $poItem->instrument; $sProduct = $poItem->itemName; $sUrl = $poItem->urlList; @@ -215,7 +214,7 @@ static function add_to_index($psSol, $poItem) { //--------------get the data out of the item cDebug::extra_debug("adding to index: $psSol, $sInstr, $sProduct, $sSampleType"); echo "."; - $sSQL = "INSERT INTO `:table` (:m_col, :so_col, :i_col, :p_col, :u_col, :sa_col ,:d_col) VALUES (:mission, :sol, :instr, :product, :url, :sample , :d_val)"; + $sSQL = "INSERT INTO `:table` (:mission_col, :sol_col, :instr_col, :product_col, :url_col, :sample_col ,:date_col) VALUES (:mission, :sol, :instr, :product, :url, :sample , :d_val)"; $sSQL = self::pr_replace_sql_params($sSQL); //--------------put it into the database @@ -265,13 +264,8 @@ static function get_random_images(string $sIntrumentPattern, int $piHowmany) { cDebug::enter(); //----------------prepare statement - $sSQL = "SELECT :m_col,:so_col,:i_col,:p_col,:u_col FROM `:table` WHERE ( :m_col=:name AND :i_col LIKE :pattern ) ORDER BY RANDOM() LIMIT :howmany"; - $sSQL = str_replace(":table", self::MANIFEST_TABLE, $sSQL); - $sSQL = str_replace(":m_col", self::COL_MISSION, $sSQL); - $sSQL = str_replace(":so_col", self::COL_SOL, $sSQL); - $sSQL = str_replace(":i_col", self::COL_INSTR, $sSQL); - $sSQL = str_replace(":p_col", self::COL_PRODUCT, $sSQL); - $sSQL = str_replace(":u_col", self::COL_IMAGE_URL, $sSQL); + $sSQL = "SELECT :mission_col,:sol_col,:instr_col,:product_col,:url_col FROM `:table` WHERE ( :mission_col=:name AND :instr_col LIKE :pattern AND :sample_col != 'thumbnail') ORDER BY RANDOM() LIMIT :howmany"; + $sSQL = self::pr_replace_sql_params($sSQL); $sSQL = str_replace(":howmany", $piHowmany, $sSQL); $oSqlDB = self::$oSQLDB; @@ -316,10 +310,10 @@ static function getManifest() { $oResult = null; cDebug::enter(); - $oCache = new cCachedHttp(); - $oCache->CACHE_EXPIRY = self::MANIFEST_CACHE; - - $oResult = $oCache->getCachedJson(self::FEED_URL); + $oCache = new cCachedHttp(); { + $oCache->CACHE_EXPIRY = self::MANIFEST_CACHE; + $oResult = $oCache->getCachedJson(self::FEED_URL); + } cDebug::leave(); return $oResult; } @@ -354,10 +348,11 @@ public static function getAllSolData($psSol, $pbCheckExpiry = true) { cDebug::write("Getting all sol data for sol $psSol"); - $oCache = new cCachedHttp(); - $oCache->CACHE_EXPIRY = self::SOL_CACHE; - $bIsCached = $oCache->is_cached($sUrl, $pbCheckExpiry); - $oResult = $oCache->getCachedJson($sUrl, $pbCheckExpiry); + $oCache = new cCachedHttp(); { + $oCache->CACHE_EXPIRY = self::SOL_CACHE; + $bIsCached = $oCache->is_cached($sUrl, $pbCheckExpiry); + $oResult = $oCache->getCachedJson($sUrl, $pbCheckExpiry); + } if (!$bIsCached) { cDebug::write("

-- sleeping for " . self::FEED_SLEEP . " ms\n"); @@ -373,8 +368,8 @@ public static function clearSolDataCache($psSol) { cDebug::enter(); cDebug::write("clearing sol cache : " . $psSol); - $oCache = new cCachedHttp(); $sUrl = self::getSolJsonUrl($psSol); + $oCache = new cCachedHttp(); $oCache->deleteCachedURL($sUrl); cDebug::leave(); diff --git a/misc/pichighlight.php b/misc/pichighlight.php index a437f8b..995021e 100644 --- a/misc/pichighlight.php +++ b/misc/pichighlight.php @@ -13,6 +13,7 @@ **************************************************************************/ require_once "$phpInc/ckinc/objstoredb.php"; +require_once "$phpInc/ckinc/image.php"; require_once "$spaceInc/misc/indexes.php"; require_once "$spaceInc/misc/realms.php"; require_once "$phpInc/ckinc/http.php"; @@ -223,22 +224,8 @@ private static function pr_get_image($psSol, $psInstrument, $psProduct) { private static function pr_perform_crop($poImg, $piX, $piY, $psOutfile) { global $root; cDebug::write("cropping to $piX, $piY"); - - $oDest = imagecreatetruecolor(self::CROP_WIDTH, self::CROP_HEIGHT); - cDebug::write("cropping ($piX, $piY), w=" . self::CROP_WIDTH . " h=" . self::CROP_HEIGHT); - imagecopy($oDest, $poImg, 0, 0, $piX, $piY, self::CROP_WIDTH, self::CROP_HEIGHT); - - //write out the file $sFilename = "$root/$psOutfile"; - $sFolder = dirname($sFilename); - if (!file_exists($sFolder)) { - cDebug::write("creating folder: $sFolder"); - mkdir($sFolder, 0755, true); //folder needs to readable by apache - } - - cDebug::write("writing jpeg to $sFilename"); - imagejpeg($oDest, $sFilename, self::THUMB_QUALITY); - imagedestroy($oDest); + cImageFunctions::crop($poImg, $piX, $piY, self::CROP_WIDTH, self::CROP_HEIGHT, self::THUMB_QUALITY, $sFilename); } //**********************************************************************