开发者

CakePHP - How do you tell a controller's action to output a raw image header?

开发者 https://www.devze.com 2023-03-19 19:04 出处:网络
I\'m recoding an existing project by using the CakePHP framework. Now, in my existing code I have a PHP file (just called thumb.php) which takes a file and the size and yields a resized raw image to t

I'm recoding an existing project by using the CakePHP framework. Now, in my existing code I have a PHP file (just called thumb.php) which takes a file and the size and yields a resized raw image to the browser. This can then be utilized in for instance an image tag or for other purposes. This "thumbs.php" file just uses a header('Content-Type: image/jpeg'); to output a raw image.

Anyway, here it is in action: http://www.dosspirit.net/php/thumb.php?file=268-transport-tycoon-deluxe-3.png&size=640

(You can change the size parameter to like 240 or even 1240 and the image will be resized for you).

Now, I want to replicate this behaviour in cakephp.

So far, I have this setup:

class Scr开发者_Go百科eenshotController extends AppController {
    public function view() {
        (Some code to handle image scaling)
    }
}

Looking at the above code, urls to a screenshot would be like this: http://www.domain/screenshot/view/268-transport-tycoon-deluxe-3.png/640

Now, how can I tell a controller in cakePHP to output the image header instead of going through a view etc., hence mimicing the functionality from the thumbs.php file ?

Any response is appreciated,

sincerely, - bakkelun


There's still PHP in CakePHP. You can do the same thing in your action:

public function view() {
    header('…');
    echo $imageData;
    exit();
}

Having said that, you should use the Controller::header method to output raw headers (for overrideable unit tests). Even better, you should use Media Views to output images the Cake Way (which requires you to write to a file though).

0

精彩评论

暂无评论...
验证码 换一张
取 消