0

I’m developing a website under Zend Framework 1.12 where users can upload pictures to illustrate their profile.

I’m trying to implement a function that will check, every time a miniature is displayed, if it exists and save it in different sizes if not (the objective is to accelerate browsing by reducing HTTP request size):

  1. The registered user upload his full size original picture (e.g. 2400 x 2400px 1MB)
  2. The picture is saved in a folder named USER_SIZE on the server
  3. Other users navigate on the website:
    • home page or any other page
    • page with the list of users where I want to display 50x50 miniature for each user
    • detailed user profile where I want to display a 100x100 miniature picture

To do this I was hoping to use a customized router that would transform picture URL and automatically will scale / crop and save the miniatures if it doesn’t already exists.

Public page (the view) :

<html>
<body>
  ...
  <img src="<?php echo $this->url(array("method" => "scale", "params" => "50x50", "filename" => $userFileName), "image")">
  ...
</body>
</html>

Custom route :

<?php

require_once 'Zend/Controller/Router/Route/Abstract.php';

Class Application_Controllers_Route_Image extends Zend_Controller_Router_Route_Abstract
{
  public static function getInstance(Zend_Config $config)
  {
    return new self($config->route);
  }

  // NOT WORKING
  public function match($path)
  {
    // Check if the URL is a miniature picture URL
    if (preg_match("#miniatures/([^/]+)/([^/]*)/(.+)#"){
      // resize picture and save miniature
      ...
    }
  }

  // WORKING
  public function assemble($data = array)
  {
    // Return a new path name for the miniature that should have been
    // resized and saved in a different folder for which the name is constructed
    // with params used in the view
    return sprint("miniatures/%s/%s/%s", $data["method"], $data["params"], $data["filename"]
  }
}

Of course all this code is very simplified. I have been able to make the custom route work to generate the miniature URL and receive something like miniatures/scale/50x50/userpicture.jpg as a result of the match function. However, I’m totally unable to capture the call to the router in the match function. This is very poorly documented in Zend documentation and I didn’t find any ideas with Google or StackExchange.

Do you know when and how the match function is called in the workflow? Does anybody has done something similar that could help?

l_r
  • 1,060
  • 12
  • 23
  • Not sure what you mean by not being able to 'capture the call' to the match function. If you view your `miniatures/xxx` URL in a browser, does your custom route match or not? – Tim Fountain Nov 01 '15 at 13:31
  • Hi! no, if I directly type a URL that looks like `miniatures/xxx`, the match function is not called. – l_r Nov 01 '15 at 16:53
  • Okay, how are you adding your custom route to the router? – Tim Fountain Nov 01 '15 at 16:59
  • In the _bootstrap.php_ file, I have a `protected function _initRoute()` in which I added `$this->getResource("FrontController")->getRouter()->addRoute("image", new Application_Controllers_Route_Image());` then in my _controllers_ folder I have my _image.php_ file which implements my `Application_Controllers_Route_Image`class. – l_r Nov 01 '15 at 17:02
  • I'm not sure that the problem is not related to the fact that img src path is not going through the zend _public.php_ file and thus not calling the _bootstrap.php_ file – l_r Nov 01 '15 at 17:19
  • What do you get when you view the `miniatures/xxx` URL in the browser? I sounds like either it's being matched by another route, or that request isn't going through ZF at all. – Tim Fountain Nov 01 '15 at 18:46
  • I get the image. I agree, it looks like it's not going through ZF. I should be missing something. – l_r Nov 01 '15 at 18:50
  • 1
    The default ZF htaccess will ignore files that already exist. If you're creating these resized images at the same URL as your route (but in the public folder), subsequent requests will bypass ZF. This is not necessarily a bad thing, but I think that's why it's not working as you expect. – Tim Fountain Nov 01 '15 at 19:01

1 Answers1

0

Thank you Tim you're right!

The default ZF htaccess will ignore files that already exist. If you're creating these resized images at the same URL as your route (but in the public folder), subsequent requests will bypass ZF. This is not necessarily a bad thing, but I think that's why it's not working as you expect.

The problem was related to the htaccess in which I had a URL rewriting parameter for IMG files save in the public folder subdirectory.

I also had to store the resized images outside the public folder.

l_r
  • 1,060
  • 12
  • 23