PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 20, 2022

[FIXED] File system path for images

 January 20, 2022     cakephp, cakephp-2.9     No comments   

Issue

I'm writing a custom helper that extends HtmlHelper and overriding the \HtmlHelper::image() method to calculate the image dimensions and add them as HTML attributes. What I have so far works fine for regular pictures:

public function image($path, $options = array()) {
    if (!array_key_exists('width', $options) && !array_key_exists('height', $options)) {
        $stamp = Configure::read('Asset.timestamp');
        Configure::write('Asset.timestamp', false);

        $path = $this->assetUrl($path, $options + array('pathPrefix' => Configure::read('App.imageBaseUrl')));
        list($width, $height) = @getimagesize(rtrim(WWW_ROOT, '\\/') . $path);
        if (!is_null($width)) {
            $options['width'] = $width;
        }
        if (!is_null($height)) {
            $options['height'] = $height;
        }

        Configure::write('Asset.timestamp', $stamp);
    }
    return parent::image($path, $options);
}

… but has these flaws:

  • Pictures from plug-ins can't be located on disk (and they should), e.g.:

    echo $this->Html->image('/debug_kit/img/cake.icon.png', array('alt' => 'CakePHP'));
    

    … produces this file system path:

    …\src\webroot/debug_kit/img/cake.icon.png
    

    … thus getimagesize() fails because actual location is:

    …\src\Plugin\DebugKit\webroot\img\cake.icon.png"
    
  • External pictures (which should be ignored) go through the full process:

    echo $this->Html->image('http://placekitten.com/200/300');
    …\src\webroothttp://placekitten.com/200/300
    

I've been looking for a builtin method to convert a CakePHP picture URL (in any format accepted by \HtmlHelper::image() into a file system path (o something like null when doesn't apply) but I couldn't find any. Native features that need a disk path, such as \Helper::assetTimestamp() are wrapped in tons of non-reusable code.

Is there an elegant solution?


Solution

I'd say that there are pretty much only 3 options:

  • Submit a patch to add asset filesystem path retrieval functionality to the core.
  • Copy a lot of code from the helper (assetUrl(), webroot(), and assetTimestamp()).
  • Use web requests for local URLs (ideally combined with caching).


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing