Wow, that was not nearly as well-formatted as I had hoped. Hope it's
still comprehensible. In any case, the import parts are simply:
1. use Router::parseExtensions() to parse image extensions and pass
them to $RequestHandler (perhaps define the extensions array as a
model property to maintain DRY);
2. use $RequestHandler->setContent() to set the content-types for the
various extensions
3. and I forgot to mention this in the previous post, but copy the
ajax layout to /app/views/layout/image/default.ctp, and set $this-
>layout = 'image' in the controller action;
4. create simple pass-thru view(s) (since they're all the same, you
could just use something like $this->render('image') instead of
Router::parseExtensions()'s default view locations);
5. use $html->image instead of $html->tag; it will accept both strings
and arrays as image locations.

On May 14, 8:57 am, calvin <cal...@rottenrecords.com> wrote:
> Maybe I'm missing something here, but why is Router:url even needed?
> The only thing you needed to do was to switch the layout to one
> appropriate for displaying binary data (e.g. a blank layout like ajax)
> and set debug to 0 to suppress the SQL output. All of this could have
> been done by the RequestHandler component, e.g.:
>
> /**
>  * In /app/config/routes.php:
>  */
> Router::parseExtensions('png', 'jpg', 'jpeg', 'svg');
>
> /**
>  * In AppController:
>  */
> var $components = array('RequestHandler');
>
> /**
>  * In AppController->beforeFilter():
>  */
> $this->RequestHandler->setContent('png', 'image/png');
> $this->RequestHandler->setContent('jpg', 'image/jpeg');
> $this->RequestHandler->setContent('jpeg', 'image/jpeg');
> $this->RequestHandler->setContent('svg', 'image/svg+xml');
>
> /**
>  * In ImagesController->view():
>  */
> $ext = $this->RequestHandler->ext;
> $exts = array('png', 'jpg', 'jpeg', 'svg');
> if(in_array($ext, $exts)){
>     $filename = "$id.$ext";
>     $image = $this->Image->findByFilename($filename);} else {
>
>     // regular view action
>     if (!$id) {
>         $this->Session->setFlash(sprintf(__('Invalid %s', true),
> 'image'));
>         $this->redirect(array('action' => 'index'));
>     }
>     $image = $this->Image->read(null, $id);}
>
> $this->set(compact('image'));
> /**
>  * Create (or just create one and make the rest symbolic links):
>  * /app/views/images/png/view.ctp
>  * /app/views/images/jpg/view.ctp
>  * /app/views/images/jpeg/view.ctp
>  * /app/views/images/svg/view.ctp
>  * with:
>  */
> echo $image['blob_file'];
>
> /**
>  * To display an image on a page, just use:
>  */
> echo $html->image('/images/view/'.$userInfo['Photo'][0]['filename'],
> array('alt' => $userInfo['Photo'][0]['caption']))?>
> // $html->image() also takes arrays as the image path; no need for
> Router::url(), which, by the way, you can also call simply by $this-
>
> >url().
>
> Though, I think the media view should have a way of outputting files
> from the database or binary data directly passed to it.
>
> On May 12, 9:42 pm, Ed Propsner <crotchf...@gmail.com> wrote:
>
>
>
> > Thanks Martin, it worked just fine.
>
> > It turns out that with all I have tried I was actually close.
>
> > $this->layout = 'ajax'
> > AND
> > Router::url
>
> > made all the difference in the world.
>
> > I nearly fell out of my chair when the image displayed :)
>
> > - Ed
>
> > On Wed, May 12, 2010 at 11:54 PM, Martin Radosta 
> > <martinrado...@gmail.com>wrote:
>
> > >  An approach
>
> > > in the controller:
>
> > > function show_image($recordId) {
> > >     $this->set('data', $this->Model->findById($recordId));
> > >     $this->layout = 'ajax'
> > > }
>
> > > the view show_image.ctp:
>
> > > Configure::write('debug', 0);
> > > header("Content-type: image/jpeg"); //assume jpg image
> > > echo $data['Model']['blob_file'];
>
> > > your view:
>
> > > echo $html->tag('img', array(
> > >     'src' => Router::url(array('controller' => 'your_controller', 'action'
> > > => 'show_image', $userInfo['Photo'][0]['id']))
> > >     )
> > > );
>
> > > Regards
>
> > > MARTIN
>
> > > On 05/13/2010 12:26 AM, Ed Propsner wrote:
>
> > > Besides the size there should be no diff between "binary" and "blob", I 
> > > was
> > > just thinking that perhaps Cake handled them differently.
> > > I played around with media view and it doesn't seem to be what I'm looking
> > > for.
>
> > >  I have it narrowed down to the headers and I can get the photo to display
> > > but it has to be in it's own view and even then I have to echo the image
> > > directly in the controller action ... this isn't working for me. I never
> > > usually store photos directly to the database but in this instance i feel 
> > > it
> > > better serves my purpose without taking a hit on performance.
>
> > >  Ideally I would like to access the image through it's $hasMany assoc and
> > > nest it in an image tag but I'm at a loss for how to pull that off with
> > > Cake.
>
> > > On Wed, May 12, 2010 at 3:12 PM, Ed Propsner <crotchf...@gmail.com> wrote:
>
> > >> I've been toying with this one for a while now and I'm not really getting
> > >> anywhere.
>
> > >>  I'm trying to display a photo stored in db as type "blob". With
> > >> conventional PHP I would normally store the photo with type "binary" and
> > >> never had any issues ... Blob is not going so well.
> > >> I figure the problem has something to do with content type but MVC is
> > >> confusing me a bit when it comes to headers or media views. It seems
> > >> straightforward enough but I think I'm going about the wrong way (I'm not
> > >> using media view or headers at this point).
>
> > >>  In my Users Model I set up a $hasMany assoc.
>
> > >>   var $hasMany = array(
> > >>         'Photo' => array(
> > >>             'className'     => 'Photo',
> > >>             'foreignKey'    => 'user_id',
> > >>             'conditions'    => array('Photo.profile' => 1, 
> > >> 'Photo.private'
> > >> => 0),
> > >>             'limit'        => '1',
> > >>             'dependent'=> true
> > >>         )
> > >>     );
>
> > >>  Rendering the photo is the problem.
>
> > >>  > View
>
> > >>  $html->tag('img', array(
> > >>                                   'src' => $userInfo['Photo'][0]['file']
> > >>                                  )
> > >>                 )
>
> > >>  the "file" part of the array is empty:
>
> > >>   [Photo] => Array
> > >>         (
> > >>             [0] => Array
> > >>                 (
> > >>                     [file] =>
> > >>                     [user_id] => 1
> > >>                 )
>
> > >>         )
>
> > >>  I tried a lot of different things but nothing worked the way I want.
> > >> What is the proper way to go about this?
>
> > >>  - Ed
>
> > >  Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp
> > > others with their CakePHP related questions.
>
> > > You received this message because you are subscribed to the Google Groups
> > > "CakePHP" group.
> > > To post to this group, send email to cake-php@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > cake-php+unsubscr...@googlegroups.com For more options, visit this group
> > > athttp://groups.google.com/group/cake-php?hl=en
>
> > >  Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp
> > > others with their CakePHP related questions.
>
> > > You received this message because you are subscribed to the Google Groups
> > > "CakePHP" group.
> > > To post to this group, send email to cake-php@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > cake-php+unsubscr...@googlegroups.com<cake-php%2bunsubscr...@googlegroups.com>For
> > >  more options, visit this group at
> > >http://groups.google.com/group/cake-php?hl=en
>
> > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp others 
> > with their CakePHP related questions.
>
> > You received this message because you are subscribed to the Google Groups 
> > "CakePHP" group.
> > To post to this group, send email to cake-php@googlegroups.com
> > To unsubscribe from this group, send email to
> > cake-php+unsubscr...@googlegroups.com For more options, visit this group 
> > athttp://groups.google.com/group/cake-php?hl=en
>
> Check out the new CakePHP Questions sitehttp://cakeqs.organd help others with 
> their CakePHP related questions.
>
> You received this message because you are subscribed to the Google Groups 
> "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com
> To unsubscribe from this group, send email to
> cake-php+unsubscr...@googlegroups.com For more options, visit this group 
> athttp://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to