3

image_style_url returns the public url of an image in the files/styles folder. Is it possible to create alias to that? What I want is the following:

Call image_style_url from the image style and the uri, which gives me typical public url /sites/default/files/style/[style_name]/path/to/image/[image_file_name]. I want to serve simple url like /image/[randomized_number] to the user that will internally alias to the aforementioned public url. This is similar to asking what pathauto module does for /node/[nid], but for image styles.

Note that I do not want custom url to individual images per se, but rather a way to programmatically create unique unpredictable url aliases for image styles.

  • Interesting question. I don't have a solution. Perhaps trying to alter the stream wrapper(s) for the styles you want to have unique urls could work? – tenken Aug 8 '14 at 19:53
  • Shouldn't url alias work after the actual url is created? altering stream wrappers feels like hacking the core. – Ratul Saha Aug 9 '14 at 6:14
  • image style urls are provided by the image styles module, and i believe a stream wrapper ... it has nothing todo with aliases so far as i know. you can think of it like a system path (that generates a custom image) -- you have to alter the system path to do this somehow. Offhand I thought maybe streamwrappers would be the way to go -- maybe you can use hook_menu_alter ... dunno. – tenken Aug 9 '14 at 6:19
  • the problem with using aliases is that you have to boostrap Drupal to serve those images. I think setting up the web server would be a better choice... – Capi Etheriel Aug 23 '14 at 1:28
1

Is a little difficult because image_style_url() uses internally image_style_path() who creates the path /sites/default/files/style/[style_name]/path/to/...

Maybe you can use File (Field) Paths module:

The File (Field) Paths module extends the default functionality of Drupal's core Upload module, the FileField module and many other File Upload modules by adding the ability to use node tokens in destination paths and filenames.

In simple terms, File (Field) Paths allows you to automatically sort and rename your uploaded files using token based replacement patterns to maintain a nice clean filesystem.

If you only want to change the path alias you can use hook_node_insert() or hook_insert()

/**
 * Implements hook_insert().
 */
function mymodule_node_insert($node) {
    // Set the URL alias
    if (empty($node->path['alias'])) {
        $node->path['alias'] = 'image/' . my_random_function();
    }
}
  • Destination paths is not the problem. Media (and media browser) module already handles the file organization etc pretty well. The problem is about getting alias to that. – Ratul Saha Aug 19 '14 at 12:00
  • Please take a look to the new answer. – Jose Daniel Aug 20 '14 at 16:10
  • This is possibly almost correct, but it works for nodes, not files, I guess. – Ratul Saha Aug 21 '14 at 7:30
1

My answer below assumes that you want to display the image itself and not an image as the main content on a Drupal page (for this you may use Views module).


I am proposing using the File Entity Paths module. From the module:

It does the same, as Filefield Paths, but focus to the file entity, not to the parent entity...

So you create an alias (let's call it IMAGE_ALIAS) to the file without needing the filename or extension (the MIME type is saved with the file upload) and then you can do these:

1) Create a symlink to sites/default/files/YOUR_IMAGES_PATH so you end up with www.example.com/IMAGE_ALIAS (more can be found at https://www.drupal.org/node/53705)

2) Do a rewrite on Drupal root's .htaccess (more can be found at https://www.drupal.org/node/53705)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sites/%{HTTP_HOST}/files/$1 [L]

3) Use another folder instead of common "sites/default/files" eg "files" (on root).

4) Use File Aliases module that does all the above.

  • The question specifically mentions image_style_url. I call this extensively in my theme. Can you please elaborate on how to create alias for styles? Or the alias for the original path will carry over like /styles/[style-name]/[alias]? – Ratul Saha Aug 22 '14 at 8:04
  • About your 4th point, doesn't file alias do this field specific? The alias will be only created when used in an image field, and not for all images? – Ratul Saha Aug 22 '14 at 8:06
  • No it will not carry over the styles/[style-name]/[alias] for node images but you can also create a symlink to that path. But the custom image path will be the same either for entity file or imagefield. – TheodorosPloumis Aug 22 '14 at 10:23
0

Have you tried File Aliases?

The File Aliases module allows you to use token customizable aliases for your uploaded files, giving you the ability to keep your file system organized as per usual while providing clean looking paths (i.e., no more '/sites/default/files/').

I've not tested it nor use it, but it seems to be the right module for your problem.

  • Yes, I have tried. That is almost an old (and a buggy) solution to what media and media browser module can do in the first place. This is about creating aliases, not placing files in different (possibly organized) places. – Ratul Saha Aug 19 '14 at 12:01
  • @RatulSaha In the basic question you are asking about creating Aliases not about saving files on different Folders. – TheodorosPloumis Aug 22 '14 at 0:06
  • Yes, creating aliases is the only concern. Saving files etc has already been taken care of by media and media browser, in my case. Creating alias and serving image files by that alias seems not to have any direct solution. File Aliases only provide this for images in a field, not images overall. – Ratul Saha Aug 23 '14 at 6:05
0

There's a hook for altering the file url when it's being created:

hook_file_url_alter

You should be able to call that hook and then use the path functions to create an alias for a style if it doesn't exist. https://api.drupal.org/api/drupal/includes%21path.inc/7

  • That solves creating the alias. How will visiting the alias lead to actual file? – Ratul Saha Aug 20 '14 at 14:52
  • @RatulSaha Sorry for the (very, super) late response, but you would use hook_menu to create a new function that loads the path. You can look at the way the core file.module does it. – Derimagia Mar 14 '16 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.