-1

I dynamicaly generate a php page with the information I added in a previous form. I want to add (not upload) an image in my form from my desktop and then display it in my generated page. I'm not asking you to do it but I just want to know if it's possible or not.

Thank you

Mathieu Fe
  • 13
  • 2

1 Answers1

1

Yes

(but not permanently)

That's the answer. More specific what you have to do:

Let the user select the file via a form input="file", read the file via JavaScript and output the blob on the site.

Here's a good tutorial: http://www.html5rocks.com/en/tutorials/file/dndfiles/

The main code they are using:

function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
          var span = document.createElement('span');
          span.innerHTML = ['<img class="thumb" src="', e.target.result,
                            '" title="', escape(theFile.name), '"/>'].join('');
          document.getElementById('list').insertBefore(span, null);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);
Community
  • 1
  • 1
idmean
  • 14,540
  • 9
  • 54
  • 83
  • @wumm Thank you so much ! That's a good way to start for me ! May Have I another request for you... I want to read it in the generated page. Can I do that ? I mean, when I check the source, it's data: (nothing else) – Mathieu Fe Aug 25 '14 at 14:02
  • You mean the created img looks like `data:....`? Yes, that's the only way to display it. If you want to get the path to the image that's not possible because of security restrictions. – idmean Aug 25 '14 at 14:04
  • Oh ok I see. So, if I want to generate a page with 3 images from my desktop and then Download the HTML page and the 3 images, I can't ? – Mathieu Fe Aug 25 '14 at 14:07
  • Only with data blobs, not with the path to your desktop, no. – idmean Aug 25 '14 at 14:11
  • Ok @wumm but let's say data blobs are ok. Can I "tell" to the first and the other picture to be renamed like (pic1.jpg, pic2.jpg) and then to be downloaded ? I mean, Should I first upload the image to then download it or I can do it with data blobs. I'm looking for a way to generate a file (html) with images from user desktop without uploading them to my server. The files (html + images) will then be downloaded by the user – Mathieu Fe Aug 25 '14 at 14:16
  • It's possible but it's not supported by all browsers and it's differently implemented. An easy solution: You can create `a` and set `href` to the blob and set `download` to `pic1.jpg`... But the user would have to click on all that links. – idmean Aug 25 '14 at 14:19
  • 1
    @DanFromGermany It's indeed possible to store images in localStorage: http://codepen.io/idmean/pen/nKwcx (I just had to test it) – idmean Aug 25 '14 at 14:47
  • @wumm, could you please detail what you said about "You can create a and set href to the blob and set download to pic1.jp" – Mathieu Fe Aug 25 '14 at 15:05
  • @MathieuFe See this example, instead of the canvas use your images: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Example.3A_Using_the_download_attribute_to_save_a_canvas_as_a_PNG. But I don't believe that's what you are after. You are more looking after something like the file writer API, but it's no longer a standard. Basically creating files using JavaScript that works in all browsers is nearly impossible, you'll most likely need a server running PHP or so. – idmean Aug 25 '14 at 15:08
  • @wumm I checked but it's not really what I'm after. This is what I would like to do : I have a form with text input and image select where I can choose 2 or 3 images from my desktop. Once I've selected my images and wrote something in my input, I generate a page with the text and image (GET POST). My page will display the text and the images I've selected previously. Then after that, I want to download everything but I guess this is a different thing. So, do you thing the first thing is possible ? – Mathieu Fe Aug 25 '14 at 15:35
  • @MathieuFe It's not possible only with JavaScript. You'll need a backend, like PHP or Java. All other methods to do so are discouraged and not supported in all major browsers. – idmean Aug 25 '14 at 15:37
  • Ok thank you @wumm. Do you have a tutorial I could get inspired or read to do that ? – Mathieu Fe Aug 25 '14 at 15:43
  • @MathieuFe Do you know PHP? Or do you only know JavaScript? (Depending on that I can link you an appropriate tutorial.) – idmean Aug 25 '14 at 15:45
  • @wumm I know better PHP than JavaScript :) – Mathieu Fe Aug 25 '14 at 15:49
  • @MathieuFe I recommend you the following procedure: Submit the text and [upload](http://php.net/manual/en/features.file-upload.post-method.php) the images to a server to your PHP script. [Rename](http://php.net/manual/en/function.rename.php) the uploaded files, create a index.html file and move them into a newly created folder. Than [zip](http://stackoverflow.com/questions/4914750/how-to-zip-a-whole-folder-using-php) the directory, remove it and send a header linking to the zip file. `header("Location: " . $zipFile);` (I can't find a good tutorial for your special case) – idmean Aug 25 '14 at 15:53
  • That's seems to be a good idea. Moreover, I affraid it could cause a security problem to let people upload right ? – Mathieu Fe Aug 25 '14 at 15:55
  • It's not a big risk. Let users only upload images, and delete them after zipping. (Also delete the zips, using a cronjob or manually) Then I don't see big risks. – idmean Aug 25 '14 at 15:58
  • Ok ok great ! Do you have a link to see it in action ? – Mathieu Fe Aug 25 '14 at 16:06
  • I just have thrown together some code I hope it works for you: https://gist.github.com/idmean/db59878886d26b41fdea (This one only works on unix systems) – idmean Aug 25 '14 at 16:19
  • Thanks @wumm I'm going to take the option to upload the files to my server. But in the "zip tutorials", I realized that it will zip a file in a folder. It's not what I want because my file is not a unique file. It will be generated with PHP. So when you are on a website, if you "save" the current page, you will save the current page and the images and other files (static) in a directory in your desktop. What I was thinking about was to ZIP the php page I displayed with my input(GET POST). You see what I mean ? Sorry if I'm not clear, my english is very poor – Mathieu Fe Aug 25 '14 at 16:38
  • @MathieuFe You want the users to have the ability to create a page by selecting images and entering text, don't you? And then you want the user to be able to download the site created as zip. Correct? That is exactly what my example does. I'm not sure what zip tutorials you are meaning. – idmean Aug 25 '14 at 16:43
  • Yes @wumm this is exactly what I want to do. I'm not running to Unix systems and don't know what I can do with the file... I'm going to check some tutorials. Thank you so much for your help – Mathieu Fe Aug 25 '14 at 16:49
  • @MathieuFe It was pleasure to help you. I've updated my gist maybe so that it now should work on every system running php for you: https://gist.github.com/idmean/db59878886d26b41fdea – idmean Aug 25 '14 at 16:56