I created a custom form in Drupal 7 with the ability to upload an image by using file_save_upload(). The image file has been uploaded and a fid has been assigned to it. How do I render this image into the different image styles I have setup?

  • i went and used image magick instead as i needed to overwrite the existing images during upload and resize. my answer has the code. Feb 18, 2016 at 22:04

2 Answers 2

print theme('image_style', array(
  'style_name' => 'your_style_name',
  'path' => $your_file->uri,


  • what if i just wanted these resized images to be moved to a new folder? Feb 18, 2016 at 20:49
  • 1
    What do you mean ? The resized images are already put in different folders based on there style name. Like : sites/default/files/styles/your_style_name/
    – CamelCode
    Feb 18, 2016 at 20:51
  • yes i know. but what if i want to rename these files and also move them to a new folder? Feb 18, 2016 at 20:56
  • If you move a file from its style folder, it will be generated again on the next theme_image_style call.
    – CamelCode
    Feb 18, 2016 at 21:00
  • 1
    If you don't want to do it the Drupal way, have a look at PHP GD or ImageMagick
    – CamelCode
    Feb 18, 2016 at 21:05

I went and used image magick to generate the images vs using drupal core functions. Since I didn't have the image styles setup. I needed a way to generate these resized images and then place them in the proper folder with the proper name. First I create a form field for file uploading. And in the _submit function for the custom form. I have the following below. "file_thumb" is the name of my form file field.

thfile is grabbing the existing thumbnail filename so it can be overwritten with imagemagick.

->writeImage function will write the file to its new location.

//Generate new images for CUSTOM image file uploaded
$file_thumb = file_save_upload('file_thumb', array(), 'public://');

if ($file_thumb) {
  $thfile = $node->field_thumbnail[$node->language][0]['value'];

  $grid_thumb = new Imagick(get_absolute_file_root() . $file_thumb->filename);
  $grid_thumb->thumbnailImage(200, 200, true);
  $grid_thumb->writeImage(get_absolute_file_root() . 'grid-thumbnail/' . $thfile);

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.