CakePHP - need url to route to tmp directory?

I am trying to create thumbnails using the GD lib in Cake PHP.

I can write a minified thumbnail to the tmp directory and create a sensible url to show the image from the tmp directory:

//set up empty GD resource  
$tmp = imagecreatetruecolor(200*$iw/$ih, 200);
//create thumbnail image in the empty GD resource
imagecopyresampled($tmp, $src, 0, 0, 0, 0,200*$iw/$ih, 200, $iw, $ih);
//build full path to thumbnail in the cakephp tmp directory
$thumbfile = APP.'tmp/thumb.jpg';
//build URL path to the same thumbnail
$thumblink = $this->webroot.'tmp/thumb.jpg';
//write jpeg to the tmp directory
$return=imagejpeg($tmp,$thumbfile);
//write out the image to client browser
echo "<img=\"$thumblink\" alt=\"thumb\" height=\"200\" width=\"200*$iw/$ih\">";

      

The sketch is created and written to the tmp directory, but when I try to access the url, I get the following error:

Error: TmpController could not be found.
Error: Create the class TmpController below in file: app/Controller/TmpController.php

      

Obviously I have a routing error - Cake is trying to invoke the tmp controller instead of looking in the tmp direcectory. How can I fix this, or is there an alternative way to serve up temporary sketches using the GD lib? I plan on creating unique sketches per session or user, but I need to get the job done first.

Routing in Config / routes.php:

Router::connect('/', array('controller' => 'MsCake', 'action' => 'index'));
Router::connect('/pages/*', array('controller' => 'pages'));
CakePlugin::routes();

      

I looked at ThumbnailHelper but that doesn't use the GD Lib. I also need to be able to access files stored in directories that are not accessible by apache from the outside, but I cannot even access the temporary symlinks to get to them. eg.

  • create a temporary symbolic link in the tmp directory pointing to the appropriate file.
  • create an HTML link pointing to the symlink using $ this-> webroot.'tmp / link-to-myfile 'as above.

... and I get the same error as above - Error: TmpController not found.

+3


source to share


2 answers


Do not do that

If you do anything to make files in an accessible tmp web browser, you are seriously compromising the security of your site. Things in the tmp directory should never be accessible on the internet.

Submit your images to the feed

Your best bet is to put your temporary images in the webroot directory - which is the only directory that is usually available on the Internet. For example:



$filename = md5($userId);
$thumbfile = APP.'webroot/img/cache/' . $filename . '.jpg';

...
$url = '/img/cache/' . $filename . '.jpg';

      

Or route to controller action

Alternatively, go to the controller action to handle the request using the view class. Note, however, that serving images with php is not free - there may be a noticeable delay while your request is being processed - where a pointer to a static file does not have this cost / risk as it is the web server that takes care of serving the content.

+4


source


Since this is temporary, you can render this image as a data url rather than in a tmp directory, for example (replace after imagecopyresampled()

call):

ob_start();
imagepng($tmp);
$contents =  ob_get_contents();
ob_end_clean();
imagedestroy($tmp);


//write out the image to client browser
echo "<img src='data:image/png;base64,".base64_encode($contents)."' alt='thumb' height='200' width='".(200*$iw/$ih)."'>";

      



This uses a bit more bandwidth as the image is base64 encoded and not sent as binary.

0


source







All Articles