{"id":8149,"date":"2026-09-02T21:34:37","date_gmt":"2026-09-02T21:34:37","guid":{"rendered":"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/"},"modified":"2026-09-02T21:34:37","modified_gmt":"2026-09-02T21:34:37","slug":"resizing-base64-images-with-javascript-a-snap-to-scale","status":"publish","type":"post","link":"https:\/\/kutia.net\/fr\/resizing-base64-images-with-javascript-a-snap-to-scale\/","title":{"rendered":"Redimensionner les images Base64 avec JavaScript\u00a0: un clin d'\u0153il \u00e0 l'\u00e9chelle"},"content":{"rendered":"<p class=\"wp-block-paragraph\">When working with images, it&#8217;s often necessary to resize them to reduce their file size or to fit a specific aspect ratio. And let&#8217;s be real, nobody wants to get stuck with a pixelated image that&#8217;s just not quite the right size.<\/p>\n<p class=\"wp-block-paragraph\">We&#8217;ve got you all covered! In this post, we&#8217;ll explore a JavaScript function that can resize a base64 encoded image. This function is a real lifesaver and will have your images looking picture perfect in no time.<\/p>\n<p class=\"wp-block-paragraph\">The function, called <code>resizeBase64Image<\/code>, takes in a single parameter: the base64 encoded image. It returns a promise that resolves with the resized image in the form of a data URL. This function is a real &#8220;snap&#8221; to use and will make resizing images a breeze.<\/p>\n<p class=\"wp-block-paragraph\">The function first sets a maximum image size limit of 4MB in bytes and then converts it. Additionally, the maximum image size limit will be used as the limit for the final image size.<\/p>\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">const maxSizeInMB = 4;\nconst maxSizeInBytes = maxSizeInMB * 1024 * 1024;<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Next, the function creates an HTML <code>Image<\/code> object and sets its <code>src<\/code> property to the base64 image. It then adds an <code>onload<\/code> event listener to the image, which will be called when the image has finished loading.<\/p>\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">const img = new Image();\nimg.src = base64Image;\nimg.onload = function () {\n   \/\/ Resizing logic goes here\n};<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Inside the <code>onload<\/code> event listener, the function creates an HTML <code>canvas<\/code> element and gets its 2D rendering context. It then stores the original width and height of the image, as well as its aspect ratio.<\/p>\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">const canvas = document.createElement(\"canvas\"); \nconst ctx = canvas.getContext('2d'); \nconst width = img.width; \nconst height = img.height; \nconst aspectRatio = width \/ height;<\/code><\/pre>\n<p class=\"wp-block-paragraph\">To determine the new width and height of the image, the function uses the aspect ratio to calculate the size that would allow the image to fit within the maximum size in bytes. And let me tell you, it&#8217;s a real &#8220;ratio-nale&#8221; approach.<\/p>\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">const newWidth = Math.sqrt(maxSizeInBytes * aspectRatio);\nconst newHeight = Math.sqrt(maxSizeInBytes \/ aspectRatio);<\/code><\/pre>\n<p class=\"wp-block-paragraph\">The function then sets the width and height of the canvas to the new values, and draws the original image on the canvas, scaled to the new size. And just like that, your image is ready for its close-up!<\/p>\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">canvas.width = newWidth;\ncanvas.height = newHeight;\nctx.drawImage(img, 0, 0, newWidth, newHeight);<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Finally, the function encodes the resized image as a JPEG, with a default quality of 80%. It then returns the image in the form of a data URL. And that&#8217;s a &#8220;snap-shot&#8221; of the final result!<\/p>\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">let quality = 0.8; \nlet dataURL = canvas.toDataURL('image\/jpeg', quality); \nresolve(dataURL);<\/code><\/pre>\n<p class=\"wp-block-paragraph\">And that&#8217;s it! With this function, you can easily resize base64.<\/p>\n<p class=\"wp-block-paragraph\">Here&#8217;s the full function:<\/p>\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">resizeBase64Image: <em>function<\/em> (base64Image) {\n  return new Promise((resolve, reject) <em>=&gt;<\/em> {\n    <strong>const<\/strong> maxSizeInMB = 4;\n    <strong>const<\/strong> maxSizeInBytes = maxSizeInMB * 1024 * 1024;\n    <strong>const<\/strong> img = new Image();\n    img.src = base64Image;\n    img.onload = <em>function<\/em> () {\n      <strong>const<\/strong> canvas = document.createElement(\"canvas\");\n      <strong>const<\/strong> ctx = canvas.getContext('2d');\n      <strong>const<\/strong> width = img.width;\n      <strong>const<\/strong> height = img.height;\n      <strong>const<\/strong> aspectRatio = width \/ height;\n      <strong>const<\/strong> newWidth = Math.sqrt(maxSizeInBytes * aspectRatio);\n      <strong>const<\/strong> newHeight = Math.sqrt(maxSizeInBytes \/ aspectRatio);\n      canvas.width = newWidth;\n      canvas.height = newHeight;\n      ctx.drawImage(img, 0, 0, newWidth, newHeight);\n      <strong>let<\/strong> quality = 0.8;\n      <strong>let<\/strong> dataURL = canvas.toDataURL('image\/jpeg', quality);\n      resolve(dataURL);\n    };\n  });\n}<\/code><\/pre>\n<p class=\"wp-block-paragraph\">In conclusion, resizing images can be a hassle, but with the JavaScript function &#8216;resizeBase64Image&#8217;, it&#8217;s a breeze. This function takes in a single parameter, the base64 encoded image, and returns a promise that resolves with the resized image in the form of a data URL. The function also sets a maximum image size limit of 4MB in bytes, which ensures that the final image is of high quality and easy to work with. <\/p>\n<p class=\"wp-block-paragraph\">Give it a try and see how it can improve your image handling process. Happy coding!<\/p>\n<pre class=\"wp-block-code\"><code class=\"\"><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Lorsque vous travaillez avec des images, il est souvent n\u00e9cessaire de les redimensionner pour r\u00e9duire la taille de leur fichier ou pour les adapter \u00e0 un rapport hauteur\/largeur sp\u00e9cifique. <\/p>\n","protected":false},"author":0,"featured_media":4568,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[82],"tags":[],"class_list":["post-8149","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-non-classifiee"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Resizing Base64 Images with JavaScript: A Snap to Scale - Kutia<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Resizing Base64 Images with JavaScript: A Snap to Scale - Kutia\" \/>\n<meta property=\"og:description\" content=\"Lorsque vous travaillez avec des images, il est souvent n\u00e9cessaire de les redimensionner pour r\u00e9duire la taille de leur fichier ou pour les adapter \u00e0 un rapport hauteur\/largeur sp\u00e9cifique.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Kutia\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-02T21:34:37+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kutia.net\\\/resizing-base64-images-with-javascript-a-snap-to-scale-2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kutia.net\\\/resizing-base64-images-with-javascript-a-snap-to-scale-2\\\/\"},\"author\":{\"name\":\"\",\"@id\":\"\"},\"headline\":\"Redimensionner les images Base64 avec JavaScript\u00a0: un clin d'\u0153il \u00e0 l'\u00e9chelle\",\"datePublished\":\"2026-09-02T21:34:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kutia.net\\\/resizing-base64-images-with-javascript-a-snap-to-scale-2\\\/\"},\"wordCount\":9,\"image\":{\"@id\":\"https:\\\/\\\/kutia.net\\\/resizing-base64-images-with-javascript-a-snap-to-scale-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/kutia.net\\\/wp-content\\\/uploads\\\/2023\\\/01\\\/LendiBlog.webp\",\"articleSection\":[\"Non classifi\u00e9(e)\"],\"inLanguage\":\"fr-FR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kutia.net\\\/resizing-base64-images-with-javascript-a-snap-to-scale-2\\\/\",\"url\":\"https:\\\/\\\/kutia.net\\\/resizing-base64-images-with-javascript-a-snap-to-scale-2\\\/\",\"name\":\"Resizing Base64 Images with JavaScript: A Snap to Scale - Kutia\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kutia.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/kutia.net\\\/resizing-base64-images-with-javascript-a-snap-to-scale-2\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/kutia.net\\\/resizing-base64-images-with-javascript-a-snap-to-scale-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/kutia.net\\\/wp-content\\\/uploads\\\/2023\\\/01\\\/LendiBlog.webp\",\"datePublished\":\"2026-09-02T21:34:37+00:00\",\"author\":{\"@id\":\"\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kutia.net\\\/resizing-base64-images-with-javascript-a-snap-to-scale-2\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\\\/\\\/kutia.net\\\/resizing-base64-images-with-javascript-a-snap-to-scale-2\\\/#primaryimage\",\"url\":\"https:\\\/\\\/kutia.net\\\/wp-content\\\/uploads\\\/2023\\\/01\\\/LendiBlog.webp\",\"contentUrl\":\"https:\\\/\\\/kutia.net\\\/wp-content\\\/uploads\\\/2023\\\/01\\\/LendiBlog.webp\",\"width\":2560,\"height\":1344},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kutia.net\\\/#website\",\"url\":\"https:\\\/\\\/kutia.net\\\/\",\"name\":\"Kutia\",\"description\":\"Custom software, team augmentation, IT recruitment\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kutia.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"fr-FR\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Resizing Base64 Images with JavaScript: A Snap to Scale - Kutia","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/","og_locale":"fr_FR","og_type":"article","og_title":"Resizing Base64 Images with JavaScript: A Snap to Scale - Kutia","og_description":"Lorsque vous travaillez avec des images, il est souvent n\u00e9cessaire de les redimensionner pour r\u00e9duire la taille de leur fichier ou pour les adapter \u00e0 un rapport hauteur\/largeur sp\u00e9cifique.","og_url":"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/","og_site_name":"Kutia","article_published_time":"2026-09-02T21:34:37+00:00","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/#article","isPartOf":{"@id":"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/"},"author":{"name":"","@id":""},"headline":"Redimensionner les images Base64 avec JavaScript\u00a0: un clin d'\u0153il \u00e0 l'\u00e9chelle","datePublished":"2026-09-02T21:34:37+00:00","mainEntityOfPage":{"@id":"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/"},"wordCount":9,"image":{"@id":"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/#primaryimage"},"thumbnailUrl":"https:\/\/kutia.net\/wp-content\/uploads\/2023\/01\/LendiBlog.webp","articleSection":["Non classifi\u00e9(e)"],"inLanguage":"fr-FR"},{"@type":"WebPage","@id":"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/","url":"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/","name":"Resizing Base64 Images with JavaScript: A Snap to Scale - Kutia","isPartOf":{"@id":"https:\/\/kutia.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/#primaryimage"},"image":{"@id":"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/#primaryimage"},"thumbnailUrl":"https:\/\/kutia.net\/wp-content\/uploads\/2023\/01\/LendiBlog.webp","datePublished":"2026-09-02T21:34:37+00:00","author":{"@id":""},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/"]}]},{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/kutia.net\/resizing-base64-images-with-javascript-a-snap-to-scale-2\/#primaryimage","url":"https:\/\/kutia.net\/wp-content\/uploads\/2023\/01\/LendiBlog.webp","contentUrl":"https:\/\/kutia.net\/wp-content\/uploads\/2023\/01\/LendiBlog.webp","width":2560,"height":1344},{"@type":"WebSite","@id":"https:\/\/kutia.net\/#website","url":"https:\/\/kutia.net\/","name":"Kutia","description":"Custom software, team augmentation, IT recruitment","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kutia.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"fr-FR"}]}},"_links":{"self":[{"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/posts\/8149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/comments?post=8149"}],"version-history":[{"count":0,"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/posts\/8149\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/media\/4568"}],"wp:attachment":[{"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/media?parent=8149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/categories?post=8149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/tags?post=8149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}