I want contribute some thing here which benefit fo...
# feed
h
I want contribute some thing here which benefit for those developer outside there who looking for how to upload image into server : step 1: create this following below function : in the composeApp/kotlin/{create whatever folder name : ex : bitmapImage}
Copy code
expect fun ImageBitmap.toBase64():String
step 2: implement function here in both side android (androidMain) and ios (iosMain)
Copy code
actual fun ImageBitmap.toBase64(): String {  // folder androidMain
    val bitmap: Bitmap = this.asAndroidBitmap()
    val byteArrayOutputStream = ByteArrayOutputStream()
    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream)
    val byteArray = byteArrayOutputStream.toByteArray()
    return Base64.encodeToString(byteArray, Base64.DEFAULT)
}

actual fun ImageBitmap.toBase64(): String {  // folder iosMain
    val uiImage = this.toUIImage()
    val jpegData = uiImage?.let { UIImageJPEGRepresentation(it, 0.5) } ?: return ""  // Adjust compression quality here (0.0 to 1.0)
    val base64String = jpegData.base64EncodedStringWithOptions(0u)
    return base64String
}

@OptIn(ExperimentalForeignApi::class)
fun ImageBitmap.toUIImage(): UIImage? {
    val width = this.width
    val height = this.height
    val buffer = IntArray(width * height)

    this.readPixels(buffer)

    val colorSpace = CGColorSpaceCreateDeviceRGB()
    val context = CGBitmapContextCreate(
        data = buffer.refTo(0),
        width = width.toULong(),
        height = height.toULong(),
        bitsPerComponent = 8u,
        bytesPerRow = (4 * width).toULong(),
        space = colorSpace,
        bitmapInfo = CGImageAlphaInfo.kCGImageAlphaPremultipliedLast.value
    )

    val cgImage = CGBitmapContextCreateImage(context)
    return cgImage?.let { UIImage.imageWithCGImage(it) }
}
step3: set api send data here :
Copy code
@Serializable
data class ProfilePictureRequestUpdate(val encodeStringImage:String)
calling api
Copy code
client.put{
            val fileName = "sample_image_${Clock.System.now()}"
            url (queryUrl + pathProfileUpdate)
            setBody( MultiPartFormDataContent(
                    formData {
                        append(
                            key = keyUpload,
                            value = profilePicture.encodeStringImage,
                            headers = Headers.build {
                                append(HttpHeaders.ContentType, "image/jpg")
                                append(HttpHeaders.ContentDisposition, "filename=\"$fileName.jpg\"")
                            }
                        )
                    }
                )
            )
            onUpload { bytesSentTotal, contentLength ->
                Napier.d { "Sent $bytesSentTotal bytes from $contentLength" }
            }
        }.body()
That's all, I hope benefit for all of us here who looking for upload the image.
👍 2
👎 2
For get image from gallery or image can use libary com.preat.peekaboo.image.picker. These library will provide your imageBitmap and byteArray.
s
You could also share this as a gist to make it shorter.
👆 3
h
@Stefan Oltmann if you could be get access into it then I will add this information. By the way, I don't want to add in medium page. Because this page make money and force reader to pay in order to continue. I really hate this.
s
A Gist is a snippet of code you share through GitHub. That has nothing to do with Medium. I share your feeling about Medium by the way.
h
Owh i see. how to share this gist. any link to refer to?
s
Get yourself familiar with GitHub.
c
1. Not sure why you base64 encode the binary data and make it ~1/3 larger!? 2. If you base64 encode it, you should not set the content type header to jpg as it actually is not a image you send but
application/octet-stream
h
@Chrimaeon in which platform ios or android? no 2, as for me it is upload to server as encode base64 and server need to decode that before save into data store.
c
On every platform in the world. It’s how base64 works. https://en.wikipedia.org/wiki/Base64
h