The function:

/**
 * Copy of image_style_path() for use with remote images.
 *
 * When image_style_path() is give a file like 'http://example.com/image.png'
 * it is converted into 'http://styles/stylename/http/example.com/image.png'
 * which will fail image_style_deliver().
 */
function remote_stream_wrapper_image_style_path($style_name, $uri) {
  // Force the image style to be returned with the default file scheme, but
  // with the file's original scheme in the path.
  return file_default_scheme() . '://styles/' . $style_name . '/' . file_uri_scheme($uri) . '/' . file_uri_target($uri);
}

Builds too many subdirectories, and they hare hard to manage and maintain.

I would prefer to use only the file_default_scheme() . '://styles/' . $style_name and then prepend file_uri_scheme($uri) converting slashes into something else to preserve the file name unity.

If you are not considering this an option, can i modify the code here for my site and it will work?

CommentFileSizeAuthor
#3 remote_stream_wrapper.code_.1696146-3.patch1016 byteshelmo

Comments

egarias’s picture

Please, any help?

the paths created by the images are:
imagepath/http/site.com/xxxx/xxxx/xxxx ...

wouldn't it be better to reduce such paths??

egarias’s picture

I have no feedback but I will put an example:

This is one of my images paths:

<img typeof="foaf:Image" src="http://mysite.com/sites/default/files/styles/large/http/www.vilaweb.cat//media/continguts/000/034/282/thumbnails/thumb_474__4.jpg" width="474" height="320" alt="">

Wouldn't it be better to have:
http/www.vilaweb.cat//media/continguts/000/034/282/thumbnails/thumb_474__4.jpg

replaced by something like

www.vilaweb.cat_media_continguts_000_034_282_thumbnails_thumb_474__4.jpg

Thanks, and waiting for some feedback, I am trying to make some patches but can find the solution

helmo’s picture

StatusFileSize
new1016 bytes

I got bitten by this, a remote site with dirty( not clean :)) url's. e.g.

http://example.com/assetimage2.jsp?id=A-B-C-D.42&aria/maxwidth_288

This caused the creation of a directory for every file. Calling the image 'maxwidth_288' in every directory :)

The resulting watchdog errors were a bit misleading...but acurate.

Failed to create style directory: public://styles/....

A test on the shell gave me more info:

mkdir: cannot create directory `32000': Too many links

Explanation: http://superuser.com/questions/298420/cannot-mkdir-too-many-links)

I've attached a (hackish) patch that rewrites the path to avoid this issue.
The side effect is though, that my html still contains links to the dirty derivative path on my own server. So the check in .htaccess to see if the literal file exists no longer works.

I hope that this inspires someone to come up with a better solution.

Ideas:
* Could we just sha sum the path
* Could we just use the fid

clemens.tolboom’s picture

The report of @egarias is about the tree too deep.

While @helmo reports about to many directories on the same level.

Changing the Drupal path for a remote image does not help as the manage file table (must) contain the remote URI and is used by ie media module to generate an image url. Setting a display style uses the remote URI to generate the path to the derived image.

So the question to be answered too is can we adjust

function image_style_url($style_name, $path) {
  $uri = image_style_path($style_name, $path);

  // If not using clean URLs, the image derivative callback is only available
  // with the query string. If the file does not exist, use url() to ensure
  // that it is included. Once the file exists it's fine to fall back to the
  // actual file path, this avoids bootstrapping PHP once the files are built.
  if (!variable_get('clean_url') && file_uri_scheme($uri) == 'public' && !file_exists($uri)) {
    $directory_path = file_stream_wrapper_get_instance_by_uri($uri)->getDirectoryPath();
    return url($directory_path . '/' . file_uri_target($uri), array('absolute' => TRUE));
  }

  return file_create_url($uri);
}

to generate a better path for remote images.

Is this a chicken and egg problem?