Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Uploading images and generating thumbnails.
  • Hey everybody, i'm new to fuel. I'm building a small CMS right now, and my next task is to allow a user to upload images and have thumbnails generated automatically for those images. I just need someone to point me in the right direction, i don't need step by step instructions. I noticed the Image and Upload classes in the core packages, should i start with those? I also need to associate these images with a blog post, so what is the best way to handle two different Models on one form? And while it's on my mind, what is the best way to handle file names when dealing with user uploaded images? Generating a random string?
  • Going through the docs of those two classes would be a good start. They do everything you want. There have been some discussions here about the use of both classes, maybe they can give you some pointers too. Associating depends heavily on how your application is designed, and what it's requirements are. For one application I use a separate assets table, which contains everything uploaded (docs, images, code, zips, etc) with details about what, when, who, etc. I store the files with a random name (the Upload class can do this for you automatically). For thumbnails I keep the filename, and suffix them with "_thumb" so that I don't have to store info about them seperately. Then every bit of code that needs to reference an uploaded asset simply links to this table to get what it needs. The class that drives this also has a method to return HTML, (both as a link and as a clickable image) depending on the type of asset (for images it would show the thumbnail, for a pdf a pdf logo that would download the file). It's currently in a client application under NDA, but I'm looking at releasing this as a package in the future...
  • I've almost got this working, but i'm frequently getting an error when generating thumbnails: ErrorException [ Error ]: Allowed memory size of 33554432 bytes exhausted (tried to allocate 3508 bytes) coming from the gd driver for the Image class. Is there a way to avoid this without setting unlimited memory usage for php? This is what i'm trying to do, and i think my problem is from processing all of the images at once in the foreach, but i can't think of a good way to do it otherwise.
          Upload::save();
          
         foreach(Upload::get_files() as $file){
         
          $filename = $file['saved_as'];
          $image = Model_Post_Image::forge(array(
           'post_id' => $post->id,
           'path' => $filename,
          ));
          
          $image->save();
          
          Image::load(DOCROOT.'assets/img/upload/postimg/'.$filename)
          ->crop_resize(250, 100)->save(DOCROOT.'assets/img/upload/postimg/'.$filename.'_thumb');
         }
    

    Advice would be appreciated.
  • Probably nothing you can do about it, processing images takes a lot of memory. An alternative is to do this offline. Put uploaded images in a 'to be processed' queue, and copy a generic thumbnail image as thumbnail for the uploaded image. That use an oil task (scheduled by cron) that processes the images in the queue from the commandline, replacing the copy by the generated thumbnail. Added benefit is that your users don't have to wait until all processing is finished, and that you can move this to different servers, making your application very scalable.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion