This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C-2823] Improve autogenerated image logic (#3729)
- Loading branch information
1 parent
37d76af
commit de56d62
Showing
26 changed files
with
297 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useCallback } from 'react' | ||
|
||
import { useSelector } from 'react-redux' | ||
|
||
import { ID } from 'models/Identifiers' | ||
import { SquareSizes } from 'models/ImageSizes' | ||
import { useAppContext } from 'src/context' | ||
import { getCollection } from 'store/cache/collections/selectors' | ||
import { getTrack } from 'store/cache/tracks/selectors' | ||
import { CommonState } from 'store/index' | ||
import { removeNullable } from 'utils/typeUtils' | ||
|
||
export const useGeneratePlaylistArtwork = (collectionId: ID) => { | ||
const collection = useSelector( | ||
(state: CommonState) => getCollection(state, { id: collectionId })! | ||
) | ||
|
||
const collectionTracks = useSelector((state: CommonState) => { | ||
const trackIds = collection.playlist_contents.track_ids.map( | ||
({ track }) => track | ||
) | ||
const tracks = trackIds | ||
.map((trackId) => getTrack(state, { id: trackId })) | ||
.filter(removeNullable) | ||
.slice(0, 4) | ||
|
||
if (tracks.length < 4) { | ||
return tracks.slice(0, 1) | ||
} | ||
|
||
return tracks | ||
}) | ||
|
||
const { imageUtils, audiusBackend } = useAppContext() | ||
|
||
return useCallback(async () => { | ||
if (collectionTracks.length === 0) { | ||
return { url: '', file: undefined } | ||
} | ||
|
||
const trackArtworkUrls = await Promise.all( | ||
collectionTracks.map(async (track) => { | ||
const { cover_art_sizes, cover_art } = track | ||
return await audiusBackend.getImageUrl( | ||
cover_art_sizes || cover_art, | ||
SquareSizes.SIZE_1000_BY_1000 | ||
) | ||
}) | ||
) | ||
|
||
return await imageUtils.generatePlaylistArtwork(trackArtworkUrls) | ||
}, [collectionTracks, audiusBackend, imageUtils]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Jimp from 'jimp' | ||
import RNFetchBlob from 'rn-fetch-blob' | ||
|
||
const { | ||
fs: { dirs, writeFile } | ||
} = RNFetchBlob | ||
|
||
const canvasWidth = 1000 // Adjust the width and height as per your requirements | ||
const canvasHeight = 1000 | ||
const imageWidth = canvasWidth / 2 | ||
const imageHeight = canvasHeight / 2 | ||
|
||
const mimeType = Jimp.MIME_JPEG | ||
|
||
export async function generatePlaylistArtwork(imageUrls: string[]) { | ||
const images = await Promise.all( | ||
imageUrls.map((imageUrl) => Jimp.read(imageUrl)) | ||
) | ||
const newImage = new Jimp(canvasWidth, canvasHeight) | ||
|
||
for (let i = 0; i < 4; i++) { | ||
const image = images[i] | ||
if (image) { | ||
image.resize(imageWidth, imageHeight) | ||
|
||
// Calculate the x and y position based on the quadrant | ||
const x = i % 2 === 0 ? 0 : imageWidth | ||
const y = i < 2 ? 0 : imageHeight | ||
|
||
// Composite the image onto the canvas | ||
newImage.composite(image, x, y) | ||
} | ||
} | ||
|
||
const fileName = 'playlist-artwork' | ||
const url = `${dirs.DocumentDir}/${fileName}.jpg` | ||
const imageContents = await newImage.getBase64Async(mimeType) | ||
const [, base64Contents] = imageContents.split(',') | ||
await writeFile(url, base64Contents, 'base64') | ||
|
||
return { | ||
url, | ||
file: { uri: url, name: fileName, type: mimeType } as unknown as File | ||
} | ||
} |
Oops, something went wrong.