1

I have an image located in sites/all/themes/mytheme/images directory. I'm displaying it programmatically with attached image style like:

  $style =  array(
    'style_name' => 'my_style',
    'path' => drupal_get_path('theme', 'mytheme') . '/images/' . $filename,
    'width' => 30,
    'height' => 30,
    'alt' => $alt,
    'title' => $title,
  );

  return theme('image_style', $style);

First error: 404:

"NetworkError: 404 Not Found - http://j.dev/sites/default/files/styles/my_style/public/sites/all/themes/mytheme/images/img.png?itok=qsTbAc8I"

Log report:

Source image at public://sites/all/themes/mytheme/images/img.png not found while trying to generate derivative image at public://styles/my_style/public/sites/all/themes/mytheme/images/img.png.

I'm sure that img.png exists in sites/all/themes/mytheme/images

File permissions:

  • sites/all/themes/mytheme/images: 777
  • sites/default/files/styles/my_style: 777
1

core module Image Style allow only images from

public:// file system (sites/default/files).

For using image styles with any other images, you should install the Imagecache External module.

Imagecache External is a utility module that allows you to store external images on your server and apply your own imagecache (D6) / Image Styles (D7).

0

Your code will not work with the images located inside the theme directory.

From the specific Drupal documentation

path: The path of the image file relative to the Drupal files directory. This function does not work with images outside the files directory nor with remotely hosted images. This should be in a format such as images/image.jpg', or using a stream wrapper such as 'public://images/image.jpg'.

Reference

0

It's actually possible to generate image style with the trick which image module uses to generate image style preview (see theme_image_style_preview()):

$original_path = drupal_get_path('theme', 'mytheme') . '/images/' . $filename;
$thumb_file = image_style_path('my_style', $original_path);
if (!file_exists($thumb_file)) {
  $style = image_style_load('my_style');
  image_style_create_derivative($style, $original_path, $thumb_file);
}

$style =  array(
  'style_name' => 'my_style',
  'path' => $original_path,
  'width' => 30,
  'height' => 30,
  'alt' => $alt,
  'title' => $title,
);
return theme('image_style', $style);

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.