Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Just in time image resizing.

Hey Guys,

So i have my Uploads going into a special non-public then i need to access them in app. the uplodas can be images or pdfs etc. bunch of different file types.

I then server them through this function its a work in progress, i really am not a big fan but it is working.

public function indexAction($imageId = null, $resize = 0){

        $file = Files::findFirstById($imageId);    

        $name = BASE_DIR.'/uploads/'.$file->fileName;

        $realName = $file->realFileName;

        $mime = $file->fileMime;

        if ($resize > 0 && exif_imagetype($name) !== NULL){
            $resizeName = $name.'-'.$resize;
            if(!file_exists($resizeName)){
                return $this->smart_resize_image(
                    $name,
                    null,
                    $resize,
                    $resize,
                    true,
                    $resizeName,
                    false,
                    100
                );
            }
            $name = $resizeName;         
        }
        $fp = fopen($name, 'rb');
        $this->view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_NO_RENDER);
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Type: ". $mime);
        header("Content-Length: ". filesize($name));
        header("Content-Disposition: ". 'attachment; filename="' . $realName . '"');
        fpassthru($fp);
        exit;
    }

So an example of this in use is this.

<!-- here i server up a fill size image if clicked on -->
<a href="file/index/1">
    <!-- but the thumbnail is only 150px --> 
    <!-- i also defer the image resizing until it is actualyl requested after upload -->
    <img src="file/index/1/150">
</a>

The problem is that the function seems to be throwing back a response before the image resize completes.

Here is my folder structure for those who care. /app /public /uploads etc



8.3k
Accepted
answer
edited Jul '15

Nerver mind i found my own issue....

I moved the image resizing into the serving function and left a "return" in there :P

    ***return*** $this->smart_resize_image(
                    $name,
                    null,
                    $resize,
                    $resize,
                    true,
                    $resizeName,
                    false,
                    100
                );