2008年10月23日星期四

Re: [fw-db] Getting JPEGs from DB

In one of my applications I built a thumbnail generator for outputting user avatar images in any size needed, using PHP to resample the original image and using Zend_Cache to cache it.

Here's the tail end of my thumbnail action which I think might help you accomplish what you need. But I agree with Matthew, you're probably better off serving the files directly unless you need PHP to be involved with every served image (logging, resampling, etc.)

--- ImagesController.php ---
<?php

class ImagesController extends Zend_Controller_Action
{
    public function thumbnailAction()
    {
        [ ... ]
        // Last modified timestamps
        $modified = filemtime($cacheFileName);
        $modifiedDateGM = gmdate('D, d M Y H:i:s', $modified) . ' GMT';
        
        // Get request headers to check for cache options
        $headers = apache_request_headers();
        
        if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == $modified)) {
            // Client's cache is current, so we just respond '304 Not Modified'.
            $response->setHeader('Cache-Control', 'max-age=3600, must-revalidate', true);
            $response->setHeader('Last-Modified', $modifiedDateGM, true);
            $response->setHttpResponseCode(304);
        } else {
            // Image not cached or cache outdated, we respond '200 OK' and output the image.
            $response->setHeader('Last-Modified', $modifiedDateGM, true);
            
            // Content type
            $response->setHeader('Content-Type', 'image/png', true);
            
            // Content length
            $response->setHeader('Content-Length', filesize($cacheFileName), true);
            
            // Force binary encoding
            $response->setHeader('Content-Transfer-Encoding', 'binary', true);
            
            // 24 hour cache
            $response->setHeader('Cache-Control', 'max-age=3600, must-revalidate', true);
            
            // Set response body
            $response->setBody(file_get_contents($cacheFileName));
        }
        
        // Send response and exit
        $response->sendResponse();
        exit;
    }
}

On Thu, Oct 23, 2008 at 8:22 AM, Matthew Ratzloff <matt@builtfromsource.com> wrote:
You really don't want to do this.  Store the files on the file system and keep data about them in the database instead.  Use a rewrite map or, better, a series of rewrite rules to serve the files directly from your web server instead of using passthrough.  That will bypass your problem entirely and be much more scalable.

-Matt


On Wed, Oct 22, 2008 at 7:54 PM, Mauro Spivak <mauros@gmail.com> wrote:
Hi there,

I'm have a simple form that uploads files to a server and stores them in a MySQL longblob field using the serialize() function. Then, when I want to retrieve them, I just output them with a couple of headers. Now, because of some reason I don't understand, when I do this through Zend Framework It works perfectly with non-image files (such as PDFs, ZIPs, DOCs, etc.) but not with image files (like JPGs, GIFs, PNGs, etc.)

I'll paste part of the code just for reference:

When uploading: 

{...}
$file['contents'] = file_get_contents($form->file->getValue());
{...}

$newFile = New File;

try {
$newFile->insert($file);
} catch (Exception $e) {
die($e->getMessage());
}


When downloading:

$file = New File;
$file = $file->fetchRow("id='".$params['id']."'");
$this->view->file = $file;

header("Content-Type:image/jpeg");
header("Content-Disposition: attachment; filename=".$file->name.".".MimeToExtension($file->type));

$this->view->file = $file;

I'll really appreciate your answer guys.

Mauro.




--
-Hector

没有评论: