Skip to content

Instantly share code, notes, and snippets.

@amphibian
Created April 13, 2011 20:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amphibian/918376 to your computer and use it in GitHub Desktop.
Save amphibian/918376 to your computer and use it in GitHub Desktop.
Creates internal thumbnails for images uploaded outside of ExpressionEngine. Requires EE 2.1.4 or later.
<?php
// Change your upload directory ID here.
$upload_dir_id = 2;
$this->EE =& get_instance();
$this->EE->load->library('filemanager');
$files = $this->EE->filemanager->fetch_files($upload_dir_id);
foreach($files->files[$upload_dir_id] as $data)
{
$dir = array('server_path' => $data['relative_path']);
if($this->EE->filemanager->create_thumb($dir, $data) == TRUE)
{
echo('<p style="color: green;">Thumbnail created for <strong>'.$data['short_name'].'</strong>.</p>');
}
else
{
echo('<p style="color: red;">Trouble creating thumbnail for <strong>'.$data['short_name'].'</strong>.</p>');
}
}
/*
Paste this in a PHP-enabled template and load it up.
Will only work in EE 2.1.4 and later.
*/
?>
@davermont
Copy link

I'm getting an error on line 7: Call to undefined method Filemanager::fetch_files()

@amphibian
Copy link
Author

You must be running 2.1.3 ... I just checked and fetch_files() is new in the 2.1.4 beta, which is what I coded and tested this on. Sorry about that.

@davermont
Copy link

Thanks, that did the trick

@rkfoster
Copy link

Sorry to be an newb, but is create_thumb() part of the existing code for EE 2.x? In other words, when I upload a file for a usual EE entry in the Publish or Edit interface is there a way to create a thumbnail?

Thanks,
Bob.

@amphibian
Copy link
Author

Hey Bob – EE does create thumbnails when you upload a file, and this function in fact just hooks into that process directly. But these are just internal thumbnails for EE's use (showing a preview on the Edit screen after you revisit the entry) -- they're not for using on the front-end of your site. The function actually has the thumbnail size hard-coded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment