Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ dependencies {
kaptTest(libs.androidx.databinding.compiler)
kaptAndroidTest(libs.androidx.databinding.compiler)

// Coil
implementation(libs.coil)
implementation(libs.coil.svg)

implementation(libs.coordinates2country.android) {
exclude(group = "com.google.android", module = "android")
}
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/fr/free/nrw/commons/CommonsApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import android.os.Build
import android.os.Process
import android.util.Log
import androidx.multidex.MultiDexApplication
import coil.Coil
import coil.ImageLoader
import coil.decode.SvgDecoder
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.imagepipeline.core.ImagePipelineConfig
import fr.free.nrw.commons.auth.LoginActivity
Expand Down Expand Up @@ -66,7 +69,7 @@ import javax.inject.Named
resCommentPrompt = R.string.crash_dialog_comment_prompt
)

class CommonsApplication : MultiDexApplication() {
class CommonsApplication : MultiDexApplication(), coil.ImageLoaderFactory {

@Inject
lateinit var sessionManager: SessionManager
Expand Down Expand Up @@ -139,6 +142,14 @@ class CommonsApplication : MultiDexApplication() {
System.setProperty("in.yuvi.http.fluent.PROGRESS_TRIGGER_THRESHOLD", "3.0")
}

override fun newImageLoader(): ImageLoader {
return ImageLoader.Builder(this)
.components {
add(SvgDecoder.Factory())
}
.build()
}

/**
* Plants debug and file logging tree. Timber lets you plant your own logging trees.
*/
Expand Down
110 changes: 75 additions & 35 deletions app/src/main/java/fr/free/nrw/commons/edit/EditActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.core.net.toUri
import androidx.core.view.WindowInsetsCompat
import androidx.exifinterface.media.ExifInterface
import androidx.lifecycle.ViewModelProvider
import coil.load
import fr.free.nrw.commons.databinding.ActivityEditBinding
import fr.free.nrw.commons.utils.applyEdgeToEdgeBottomInsets
import fr.free.nrw.commons.utils.applyEdgeToEdgeTopPaddingInsets
Expand Down Expand Up @@ -104,9 +105,12 @@ class EditActivity : AppCompatActivity() {
ExifInterface.TAG_GPS_LONGITUDE_REF,
ExifInterface.TAG_GPS_PROCESSING_METHOD,
ExifInterface.TAG_GPS_TIMESTAMP,
ExifInterface.TAG_IMAGE_LENGTH,
ExifInterface.TAG_IMAGE_WIDTH,
ExifInterface.TAG_PHOTOGRAPHIC_SENSITIVITY,
ExifInterface.TAG_MAKE,
ExifInterface.TAG_MODEL,
ExifInterface.TAG_ORIENTATION,
ExifInterface.TAG_WHITE_BALANCE,
ExifInterface.WHITE_BALANCE_AUTO,
ExifInterface.WHITE_BALANCE_MANUAL,
Expand All @@ -116,6 +120,11 @@ class EditActivity : AppCompatActivity() {
sourceExifAttributeList.add(Pair(tag.toString(), attribute))
}

if (imageUri.substringBefore("?").endsWith(".svg", ignoreCase = true)) {
Toast.makeText(this, "SVG files cannot be edited", Toast.LENGTH_SHORT).show()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will review this today, thanks for contributing @Roniscend. Can I know is there any reason we can't edit svg images?

Copy link
Collaborator

@Kota-Jagadeesh Kota-Jagadeesh Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I know is there any reason we can't edit svg images?

@neeldoshii Not sure but based on one of the issues I’m working on, the reason might be that SVG files don’t contain EXIF headers, which could be causing the problem :)

Comment link : #6642 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, our current editing flow relies on EXIF metadata, which svg do not have.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The app exists because smartphones have become the main way people take pictures. By contrast, creating SVG on smartphone is an extremely niche use case. In my experience monitoring the app's uploads, most non-photography files uploaded from the app are copyright violations. So I think it makes sense to focus our limited resources on photography formats. 🙂

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in that case how about we convert svg to png first? Not sure if its possible or not and how much workaround is there will check it once.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have the bandwidth to maintain any SVG or PNG editing features.

We can grey out the edit button and when tapped, it could say something like "To edit non-JPG files, use third-party tools".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can grey out the edit button

@nicolas-raoul this was recently fixed in #6653 as part of #6642 ,and now the edit image button will be disabled for the non JPG files :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one sounds better actually. It's more transparent for our users, otherwise they'll keep wondering when that Edit button appears. I'll create a follow-up issue.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"To edit non-JPG files, use third-party tools".

Great. Also just an idea for suggestion not sure it good or not. How about we provide third party tool link and user can click and go to the website (externally, in app is very hard to maintain as we don't know when to close) and once the user has converted the image to jpg, user can finally upload jpg image.

finish()
return
}
init()
}

Expand All @@ -124,16 +133,28 @@ class EditActivity : AppCompatActivity() {
*
* This function sets up the ImageView for displaying an image, adjusts its view bounds,
* and scales the initial image to fit within the ImageView. It also sets click listeners
* for the "Rotate", "Crop" and "Save" buttons.
* for the "Rotate" and "Save" buttons.
*/
private fun init() {
binding.iv.adjustViewBounds = true
binding.iv.scaleType = ImageView.ScaleType.MATRIX
binding.iv.post {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFile(imageUri, options)

if (imageUri.substringBefore("?").endsWith(".svg", ignoreCase = true)) {
binding.rotateBtn.isEnabled = false
binding.iv.load(imageUri) {
listener(
onSuccess = { _, result ->
val drawable = result.drawable
val scale = binding.iv.measuredWidth.toFloat() / drawable.intrinsicWidth.toFloat()
binding.iv.layoutParams.height = (scale * drawable.intrinsicHeight).toInt()
binding.iv.imageMatrix = scaleMatrix(scale, scale)
}
)
}
} else {
binding.iv.post {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFile(imageUri, options)
val bitmapWidth = options.outWidth
val bitmapHeight = options.outHeight

Expand Down Expand Up @@ -193,7 +214,7 @@ class EditActivity : AppCompatActivity() {
binding.iv.imageMatrix = matrix
}

}
}}
binding.rotateBtn.setOnClickListener {
// Allow rotation while in crop mode - overlay will update after animation
animateImageHeight()
Expand Down Expand Up @@ -416,6 +437,16 @@ class EditActivity : AppCompatActivity() {

var imageRotation = 0

/**
* Data class representing crop coordinates.
*/
private data class CropCoordinates(
val left: Int,
val top: Int,
val width: Int,
val height: Int
)

/**
* Animates the height, rotation, and scale of an ImageView to provide a smooth
* transition effect when rotating an image by 90 degrees.
Expand Down Expand Up @@ -452,28 +483,24 @@ class EditActivity : AppCompatActivity() {

when (rotation) {
0, 180 -> {
imageScale = min(viewWidth / drawableWidth, maxAvailableHeight / drawableHeight)
val fitW = viewWidth / drawableHeight
val fitH = maxAvailableHeight / drawableWidth
newImageScale = min(fitW, fitH)
newViewHeight = min((drawableWidth * newImageScale).toInt(), maxAvailableHeight.toInt())
imageScale = viewWidth / drawableWidth
newImageScale = viewWidth / drawableHeight
newViewHeight = (drawableWidth * newImageScale).toInt()
}
90, 270 -> {
imageScale = min(viewWidth / drawableHeight, maxAvailableHeight / drawableWidth)
val fitW = viewWidth / drawableWidth
val fitH = maxAvailableHeight / drawableHeight
newImageScale = min(fitW, fitH)
newViewHeight = min((drawableHeight * newImageScale).toInt(), maxAvailableHeight.toInt())
imageScale = viewWidth / drawableHeight
newImageScale = viewWidth / drawableWidth
newViewHeight = (drawableHeight * newImageScale).toInt()
}
else -> {
throw
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add catch instead

UnsupportedOperationException(
"rotation can 0, 90, 180 or 270. \${rotation} is unsupported"
"rotation can 0, 90, 180 or 270. $rotation is unsupported"
)
}
}

val animator = ValueAnimator.ofFloat(0f, 1f).setDuration(500L)
val animator = ValueAnimator.ofFloat(0f, 1f).setDuration(1000L)

animator.interpolator = AccelerateDecelerateInterpolator()

Expand All @@ -486,12 +513,6 @@ class EditActivity : AppCompatActivity() {
override fun onAnimationEnd(animation: Animator) {
imageRotation = newRotation % 360
binding.rotateBtn.setEnabled(true)

// If crop mode is active, update the overlay bounds for new rotation
// Use post{} to wait for the layout pass triggered by requestLayout()
if (isCropMode) {
binding.iv.post { updateCropOverlayBounds() }
}
}

override fun onAnimationCancel(animation: Animator) {
Expand Down Expand Up @@ -533,6 +554,35 @@ class EditActivity : AppCompatActivity() {
animator.start()
}

/**
* Rotates and edits the current image, copies EXIF data, and returns the edited image path.
*
* This function retrieves the path of the current image specified by `imageUri`,
* rotates it based on the `imageRotation` angle using the `rotateImage` method
* from the `vm`, and updates the EXIF attributes of the
* rotated image based on the `sourceExifAttributeList`. It then copies the EXIF data
* using the `copyExifData` method, creates an Intent to return the edited image's file path
* as a result, and finishes the current activity.
*/
fun getRotatedImage() {
val filePath = imageUri.toUri().path
val file = filePath?.let { File(it) }

val rotatedImage = file?.let { vm.rotateImage(imageRotation, it, applicationContext.cacheDir) }
if (rotatedImage == null) {
Toast.makeText(this, "Failed to rotate to image", Toast.LENGTH_LONG).show()
}
val editedImageExif: ExifInterface?
if (rotatedImage?.path != null) {
editedImageExif = ExifInterface(rotatedImage.path)
copyExifData(editedImageExif)
}
val resultIntent = Intent()
resultIntent.putExtra("editedImageFilePath", rotatedImage?.toUri()?.path ?: "Error")
setResult(RESULT_OK, resultIntent)
finish()
}

/**
* Copies EXIF data from sourceExifAttributeList to the provided ExifInterface object.
*
Expand Down Expand Up @@ -585,13 +635,3 @@ class EditActivity : AppCompatActivity() {
return scaleFactor
}
}

/**
* Data class to hold crop coordinates.
*/
private data class CropCoordinates(
val left: Int,
val top: Int,
val width: Int,
val height: Int
)
36 changes: 26 additions & 10 deletions app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.widget.LinearLayout
import android.widget.Spinner
import android.widget.TextView
import android.widget.Toast
import coil.load
import androidx.compose.foundation.clickable
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -733,19 +734,34 @@ class MediaDetailFragment : CommonsDaggerSupportFragment(), CategoryEditHelper.C
val imageBackgroundColor: Int = imageBackgroundColor
if (imageBackgroundColor != DEFAULT_IMAGE_BACKGROUND_COLOR) {
binding.mediaDetailImageView.setBackgroundColor(imageBackgroundColor)
binding.mediaDetailImageViewSvg.setBackgroundColor(imageBackgroundColor)
}

binding.mediaDetailImageView.hierarchy.setPlaceholderImage(R.drawable.image_placeholder)
binding.mediaDetailImageView.hierarchy.setFailureImage(R.drawable.image_placeholder)
val mediaUrl = media?.imageUrl
if (mediaUrl != null && mediaUrl.substringBefore("?").endsWith(".svg", ignoreCase = true)) {
binding.mediaDetailImageView.visibility = View.GONE
binding.mediaDetailImageViewSvg.visibility = View.VISIBLE

val controller: DraweeController = Fresco.newDraweeControllerBuilder()
.setLowResImageRequest(ImageRequest.fromUri(if (media != null) media!!.thumbUrl else null))
.setRetainImageOnFailure(true)
.setImageRequest(ImageRequest.fromUri(if (media != null) media!!.imageUrl else null))
.setControllerListener(aspectRatioListener)
.setOldController(binding.mediaDetailImageView.controller)
.build()
binding.mediaDetailImageView.controller = controller
binding.mediaDetailImageViewSvg.load(mediaUrl) {
placeholder(R.drawable.image_placeholder)
error(R.drawable.image_placeholder)
}
} else {
binding.mediaDetailImageViewSvg.visibility = View.GONE
binding.mediaDetailImageView.visibility = View.VISIBLE

binding.mediaDetailImageView.hierarchy.setPlaceholderImage(R.drawable.image_placeholder)
binding.mediaDetailImageView.hierarchy.setFailureImage(R.drawable.image_placeholder)

val controller: DraweeController = Fresco.newDraweeControllerBuilder()
.setLowResImageRequest(ImageRequest.fromUri(if (media != null) media!!.thumbUrl else null))
.setRetainImageOnFailure(true)
.setImageRequest(ImageRequest.fromUri(if (media != null) media!!.imageUrl else null))
.setControllerListener(aspectRatioListener)
.setOldController(binding.mediaDetailImageView.controller)
.build()
binding.mediaDetailImageView.controller = controller
}
}

private fun updateToDoWarning() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.facebook.drawee.view.SimpleDraweeView
import fr.free.nrw.commons.R
import fr.free.nrw.commons.databinding.ItemUploadThumbnailBinding
Expand Down Expand Up @@ -50,7 +52,15 @@ internal class ThumbnailsAdapter(private val callback: Callback) :
fun bind(position: Int) {
val uploadableFile = uploadableFiles[position]
val uri = uploadableFile.getMediaUri()
background.setImageURI(Uri.fromFile(File(uri.toString())))
if (uri.toString().substringBefore("?").endsWith(".svg", ignoreCase = true)) {
background.visibility = View.GONE
binding.ivThumbnailSvg.visibility = View.VISIBLE
binding.ivThumbnailSvg.load(uri)
} else {
background.visibility = View.VISIBLE
binding.ivThumbnailSvg.visibility = View.GONE
background.setImageURI(Uri.fromFile(File(uri.toString())))
}
if (position == callback.getCurrentSelectedFilePosition()) {
val border = GradientDrawable()
border.shape = GradientDrawable.RECTANGLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.exifinterface.media.ExifInterface
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import coil.load
import com.github.chrisbanes.photoview.PhotoView
import fr.free.nrw.commons.CameraPosition
import fr.free.nrw.commons.R
Expand Down Expand Up @@ -52,6 +54,9 @@ import fr.free.nrw.commons.utils.ImageUtils.getErrorMessageForResult
import fr.free.nrw.commons.utils.NetworkUtils.isInternetConnectionEstablished
import fr.free.nrw.commons.utils.ViewUtil.showLongToast
import fr.free.nrw.commons.utils.handleKeyboardInsets
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.io.File
import java.util.ArrayList
Expand Down Expand Up @@ -289,8 +294,11 @@ class UploadMediaDetailFragment : UploadBaseFragment(), UploadMediaDetailsContra
View.VISIBLE
}

// lljtran only supports lossless JPEG rotation, so we disable editing for other formatts
val filePath = uploadableFile?.getFilePath()?.toString() ?: ""
val isSvgFile = filePath.endsWith(".svg", ignoreCase = true) ||
uploadItem?.mediaUri?.toString()?.substringBefore("?")?.endsWith(".svg", ignoreCase = true) == true
llEditImage.visibility = if (isSvgFile) View.GONE else View.VISIBLE
// lljtran only supports lossless JPEG rotation, so we disable editing for other formatts
val isJpeg = filePath.endsWith(".jpeg", ignoreCase = true)
|| filePath.endsWith(".jpg", ignoreCase = true)
llEditImage.visibility = View.VISIBLE
Expand Down Expand Up @@ -394,6 +402,12 @@ class UploadMediaDetailFragment : UploadBaseFragment(), UploadMediaDetailsContra
if (_binding == null) {
return
}
val mediaUri = uploadItem.mediaUri
if (mediaUri != null && mediaUri.toString().substringBefore("?").endsWith(".svg", ignoreCase = true)) {
binding.backgroundImage.load(mediaUri)
} else {
binding.backgroundImage.setImageURI(mediaUri)
}
// If the user has already edited the image, show the edited version
// instead of the original. This prevents the async preProcessImage
// callback from overwriting the edited preview.
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/utils/ImageUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ object ImageUtils {
*/
@JvmStatic
fun checkIfImageIsTooDark(imagePath: String): Int {
if (imagePath.endsWith(".svg", ignoreCase = true)) {
return IMAGE_OK
}
val millis = System.currentTimeMillis()
return try {
var bmp = ExifInterface(imagePath).thumbnailBitmap
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/fragment_media_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
android:layout_gravity="center_horizontal"
app:actualImageScaleType="none" />

<ImageView
android:id="@+id/mediaDetailImageViewSvg"
android:layout_width="wrap_content"
android:layout_height="@dimen/dimen_250"
android:layout_gravity="center_horizontal"
android:scaleType="fitCenter"
android:visibility="gone" />

<ScrollView
android:id="@+id/mediaDetailScrollView"
android:layout_width="match_parent"
Expand Down
Loading