0

I'm using managed_file in a form to uplaod multiple images (D7).

$form['img_upload1'] = array(
'#title' => t('Image'),
'#type' => 'managed_file',
'#upload_validators' => array(
    'file_validate_extensions' => array('gif png jpg jpeg')),
'#default_value' => variable_get('img_upload1', ''),
'#upload_location' => 'public://uploads_users/mydir/'.$userId,
);

Allowed file-extension are checked when the user chooses an image and in the form-validate-function the filesize is checked. The images should have a max hight of 730px or max width of 970px.

I can find the dimesions of the uploaded picture bud don't know how to resize it. Is there a way to add a scaling-function (an image-style?) to the managed_file-formfield or to the validation-function to resize the image before ore after the upload?

Thanks

UPDATE Thank you for your hints. The idea behind all was to have the original files available in a defined dimensions for an other purpose. Bud I see the benefits of using the "Drupal-way".

2
  • 1
    Is there any reason you are trying to scale the image on upload rather than using Drupal's built in image styles to display the images at the correct size?
    – splatio
    Commented Feb 9, 2012 at 11:20
  • I have no experience here but I'll just mention image_style_url in case it's helpful.
    – arkz
    Commented Feb 9, 2012 at 11:25

1 Answer 1

1

There's no need to scale your images when you upload them, the usual method is to serve a cached version of the scaled image when it's displayed. This is usually accomplished by setting up an image style in the Drupal admin UI that has the effects you need, and then using theme_image_style() to output that image:

$file = file_load($fid);
$options = array(
  'style_name' => 'image-style-machine-name',
  'alt' => 'Alternative Text',
  'path' => $file->uri
);

$image_output = theme('image_style', $options);

Doing it this way gives the added benefit of your having the original uploaded file available at any time (for example to link through to as a full-sized version of an image, or in a lightbox, etc.)

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.