I'm doing a form programmatically. I need to give an option to upload an image and give an option to resize it ( like cropping option ).

I don't know how to do it, I know that I need to upload the image to a directory and also I suppose that I need to show a relation between the node and the image in the database.

I've created a field called field_img.

So, how can I do it?

This is the form that I've done but I don't know how to give a relation in the database...

form['group_anuncio']['imagen'] = array(
        '#title' => t('Image'),
        '#type' => 'managed_file',
        '#upload_validators' => array('file_validate_extensions' => array('jpeg jpg png gif')),
        '#upload_location' => 'public://sites/default/files',
        '#progress_indicator' => 'throbber',
        '#status' => FILE_STATUS_PERMANENT,
        '#progress_message' => 'One moment while we save your file...',
  • This question appears to be off-topic because it is about code you wrote, for which you want to write better code, or you want to understand why it doesn't work, but you didn't show the code you are using, or the part of the code that is relevant for the question.
    – Mołot
    May 6, 2014 at 10:27
  • I would like to know how to give the option to upload an image in a form. I don't know where to start... I've seen that I can create the field like a field type or managed_file...
    – more770
    May 6, 2014 at 10:35
  • You said you are "doing a form programmatically". That implies you have a code for that, don't you? if no, then what king of form is it?
    – Mołot
    May 6, 2014 at 10:36

2 Answers 2


I would strongly suggest to use nodes and one of these modules
Comparison of image cropping and resizing modules

If you dont want nodes then you still should be able to use one of crop modules which works with image fields if you use entities, you can use Entity Construction Kit (ECK) module to create custom entity types


The best practice is to use the File Entity module with the entity approach. When you have installed it, the points are:

  • Create a form input (you've made it)
  • Store the new file in a temp folder
  • Create a new File() object
  • Use the save() method

This method will store your file like an object, and will be easy to recover it as you want

  • With #upload_location variable I can say where I want to upload the file, I suposse that is like the second step that you have told. But, I don't know who to do it the thrird and the fourth ones. Have you got any example? Thank you.
    – more770
    May 7, 2014 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.