How to resize image size in javascript?

javascript resize image [reduce size] and upload

  • Is it possible to resize the image with javascript or Flash ? My requirement: A user uploading a image with 10MB in size,i want to re size the image on client side using Javascript or flash,after resizing the image,it need to be uploaded to the server. If it is possible i can save the bandwidth. I am using uploadify for upload images and Codeigniter on the Server Side. is there any other methods to do this ? Note : Reference to some Libraries are really helpful. Thank you.

  • Answer:

    Yes, it is possible in Flash Player 10 and later. This is an old blog post, from when Flash Player 10 and the FileReference.load() function was new, back in 2008, and it doesn't cover all steps needed, but it's a start: http://www.mikechambers.com/blog/2008/08/20/reading-and-writing-local-files-in-flash-player-10/ You would probably also need to look into how to resize a bitmap in ActionScript, how to encode the bitmap as JPEG or PNG (using https://github.com/mikechambers/as3corelib) and how to upload the result to the server. Edit: http://www.plupload.com seems to have support for resizing. There's also http://resize-before-upload.com/

Red at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Yes it's possible to resize an image in javascript using the HTML5 canvas API. You, however, will need to save it somewhere after being resized. The first API you will need to check is http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-drawimage. The second one is http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-canvas-todataurl. Depending on what you want to do, you can either: Make the user save the smaller image using this http://www.nihilogic.dk/labs/canvas2image/. Store it yourself using a server-side language, to convert a Base64 Encoded Image to an actual image file, and save it on your server. Check out Example 1. Example 1: PHP5 based solution: <?php define('UPLOAD_DIR', 'images/'); $img = $_POST['img']; $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $data = base64_decode($img); $file = UPLOAD_DIR . uniqid() . '.png'; $success = file_put_contents($file, $data); print $success ? $file : 'Unable to save the file.'; ?> UPDATE #2: As stated in the comments below, this method requires a http://caniuse.com/#search=File%20API to work. Use the http://stackoverflow.com/a/8341309/1029952 instead to support both modern and non-html5 browsers.

Pierre

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.