Skip to content

Commit adcd7bc

Browse files
authored
Merge pull request #484 from wordpress-mobile/issue/resize-pictures-to-smaller-size
Resize pictures to smaller size (800px)
2 parents 0b43327 + 5aaeef2 commit adcd7bc

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import android.widget.PopupMenu
2929
import android.widget.TextView
3030
import android.widget.Toast
3131
import org.wordpress.android.util.AppLog
32+
import org.wordpress.android.util.ImageUtils
3233
import org.wordpress.android.util.PermissionUtils
3334
import org.wordpress.android.util.ToastUtils
3435
import org.wordpress.aztec.*
@@ -183,7 +184,6 @@ class MainActivity : AppCompatActivity(),
183184
val options = BitmapFactory.Options()
184185
options.inDensity = DisplayMetrics.DENSITY_DEFAULT
185186
bitmap = BitmapFactory.decodeFile(mediaPath, options)
186-
187187
insertImageAndSimulateUpload(bitmap, mediaPath)
188188
}
189189
REQUEST_MEDIA_PHOTO -> {
@@ -229,14 +229,16 @@ class MainActivity : AppCompatActivity(),
229229
}
230230

231231
fun insertImageAndSimulateUpload(bitmap: Bitmap?, mediaPath: String) {
232+
val bitmapResized = ImageUtils.getScaledBitmapAtLongestSide(bitmap, aztec.visualEditor.maxImagesWidth)
232233
val (id, attrs) = generateAttributesForMedia(mediaPath, isVideo = false)
233-
aztec.visualEditor.insertImage(BitmapDrawable(resources, bitmap), attrs)
234+
aztec.visualEditor.insertImage(BitmapDrawable(resources, bitmapResized), attrs)
234235
insertMediaAndSimulateUpload(id, attrs)
235236
}
236237

237238
fun insertVideoAndSimulateUpload(bitmap: Bitmap?, mediaPath: String) {
239+
val bitmapResized = ImageUtils.getScaledBitmapAtLongestSide(bitmap, aztec.visualEditor.maxImagesWidth)
238240
val (id, attrs) = generateAttributesForMedia(mediaPath, isVideo = true)
239-
aztec.visualEditor.insertVideo(BitmapDrawable(resources, bitmap), attrs)
241+
aztec.visualEditor.insertVideo(BitmapDrawable(resources, bitmapResized), attrs)
240242
insertMediaAndSimulateUpload(id, attrs)
241243
}
242244

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
7878
val VISIBILITY_KEY = "VISIBILITY_KEY"
7979
val IS_MEDIA_ADDED_KEY = "IS_MEDIA_ADDED_KEY"
8080
val RETAINED_HTML_KEY = "RETAINED_HTML_KEY"
81+
82+
val DEFAULT_IMAGE_WIDTH = 800
8183
}
8284

8385
private var historyEnable = resources.getBoolean(R.bool.history_enable)
@@ -130,6 +132,8 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
130132

131133
var verticalParagraphMargin: Int = 0
132134

135+
var maxImagesWidth: Int = 0
136+
133137
interface OnSelectionChangedListener {
134138
fun onSelectionChanged(selStart: Int, selEnd: Int)
135139
}
@@ -235,6 +239,11 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
235239

236240
styles.recycle()
237241

242+
// set the pictures max size to the min of screen width/height and DEFAULT_IMAGE_WIDTH
243+
val minScreenSize = Math.min(context.resources.displayMetrics.widthPixels,
244+
context.resources.displayMetrics.heightPixels)
245+
maxImagesWidth = Math.min(minScreenSize, DEFAULT_IMAGE_WIDTH)
246+
238247
if (historyEnable && historySize <= 0) {
239248
throw IllegalArgumentException("historySize must > 0")
240249
}
@@ -748,6 +757,7 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
748757

749758
private fun loadImages() {
750759
val spans = this.text.getSpans(0, text.length, AztecImageSpan::class.java)
760+
751761
spans.forEach {
752762
val callbacks = object : Html.ImageGetter.Callbacks {
753763

@@ -770,17 +780,12 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
770780
}
771781
}
772782
}
773-
774-
// maxidth set to the biggest of screen width/height to cater for device rotation
775-
val maxWidth = Math.max(context.resources.displayMetrics.widthPixels,
776-
context.resources.displayMetrics.heightPixels)
777-
imageGetter?.loadImage(it.getSource(), callbacks, maxWidth)
783+
imageGetter?.loadImage(it.getSource(), callbacks, this@AztecText.maxImagesWidth)
778784
}
779785
}
780786

781787
private fun loadVideos() {
782788
val spans = this.text.getSpans(0, text.length, AztecVideoSpan::class.java)
783-
784789
spans.forEach {
785790
val callbacks = object : Html.VideoThumbnailGetter.Callbacks {
786791

@@ -803,8 +808,7 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
803808
}
804809
}
805810
}
806-
807-
videoThumbnailGetter?.loadVideoThumbnail(it.getSource(), callbacks, context.resources.displayMetrics.widthPixels)
811+
videoThumbnailGetter?.loadVideoThumbnail(it.getSource(), callbacks, this@AztecText.maxImagesWidth)
808812
}
809813
}
810814

0 commit comments

Comments
 (0)