Sunday, April 6, 2014

Image upload preview using my JQuery Upload Plugin

Adding a simple image preview during the upload process can be easily done by doing some tweaking to my Upload Plugin demos.

Here is the modified demo: http://danielm.herokuapp.com/demos/dnd/image-preview.php

The magic happens on onNewFile(id, file); our new file callback recives an HTML5 File object as parameter which can be used with the FileReader to accomplish our goal.

The code is very self explanatory:

    onNewFile: function(id, file){

      // Checks if this feature if availabe to the user
      if (typeof FileReader !== "undefined"){
            
        var reader = new FileReader();

        // Our <img> object
        var img = $('your-image-dom-object');

        reader.onload = function (e) {
           img.attr('src', e.target.result);
        }

        reader.readAsDataURL(file);

      }
  }

Modern browsers like Chrome/Firefox or IE10 support this feature; which is enough since those are also my uploader plugin requirements.

Saturday, March 29, 2014

Ruby on Rails in Fedora 20

So this post is gonna be a short one; and as i said before there is several ways to install RoR.
The important thing is: do not mix up things; and by mix up i mean trying to use packages already built by your distro like rubygems and then installing the gems on the userspace; while that may work on some cases it may also lead into weird issues.

For beginners installing Rails on Fedora 20 is simple as doing:
$ sudo yum install ruby rubygems rubygem-rails

That way you have all you need to get things going; also you can look up for other gems on the repositories by the name rubygem-*.

And the mandatory hello world app:
$ rails new hello-world
.... have some patient here...
$ cd hello-world
$ rails server
Open: http://localhost:3000

As you get more involved into rails you may want to try to set up your dev enviroment with other tools like RVM. (I wrote a post about this long time ago; but it's kinda outdated and may be a nice idea for a future post)

Some nice resources about Rails and Ruby in general:

Sunday, February 9, 2014

Using the Symfony OptionsResolver interface with Custom Forms

My beloved Symfony changed a lot since last time we seen. Now when trying to update some projects to 2.4 many things broke including my hearth. But after the initial annoyance of any migration i realize some things were better, even new. This is the case of the new OptionsResolver component.
This is used by several components, but the one i liked was when we apply it to custom Form classes.
See, back in time if we wanted to pass some parameters to our Form class we had to use the native PHP constructors, do some fancy stuff.... include some checks and your initial simple class became a big mess.

So this is a new approach you could try:

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class GalleryEditor extends AbstractType
{
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
    // Access your options here using something like: $options['db']
  }

  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
    // I think function names are self-explanatory here
    $resolver
      ->setRequired(array(
         'db'
      ))
      ->setAllowedTypes(array(
         'db' => 'Doctrine\DBAL\Connection'
    ));
    // Other ones you could use are:
    // - setOptional
    // - setDefaults
  }

  //----
}

For much I hate code-snippet posts i really liked this one.... also to not let my blog die for another 6 months (again)