1

I have created a thumbnail like image style in Drupal 7 (glossy_footer_sublogo).Also i have coded a module that has a block to display the 'logo.png' file that comes with my theme and converted by 'glossy_footer_sublogo' style. after enabling the block and clearing cache no image is displayed although the 'sites/all/themes/glossy/' path created in my style directory. Following are the codes that i used to display the block:

/**
 * Implementation of hook_block_view().
 */
function glossy_helper_block_view($delta = '') {
  switch ($delta) {
    case 'glossy_footer_sublogo':
            $img_url = 'public://sites/all/themes/glossy/logo.png';
            $style = 'footer_sublogo';

            return array(
                'subject' => '',
                'content' => "<img src=" . image_style_url($style, $img_url) . ">",
            );
      break;
  }
}

Can anyone point me to what is the problem? Thanks!

2

If you go to admin/config/media/file-system what is your public path set as?

If your public path is set as sites/default/files then I think your code is generating the following link:

http://yourdomain/sites/default/files/sites/all/themes/glossy/logo.png

Try changing your $img_url to the following

$img_url = drupal_get_path('theme', 'glossy') . '/logo.png';

On another note, is the block rendering at all (just without the image)? Your implementation of hook_block_view looks incorrect.

I think it should be more like the following:

/**
 * Implementation of hook_block_view().
 */
function glossy_helper_block_view($delta = '') {
  $block = array();

    switch ($delta) {
      case 'glossy_footer_sublogo':
              $img_url = 'public://sites/all/themes/glossy/logo.png';
              $style = 'footer_sublogo';

              $block['subject'] = ''
              $block['content'] = "<img src=" . image_style_url($style, $img_url) . ">",

              );
       break;
    }

  return $block
}

Edit

Change $img_url as I previously mentioned, but rewrite your block content as follows:

$block['content'] = '<img src="' . image_style_url($style, $img_url) . '" />';

There aren't any quote marks around the image URL that is being returned by the function.

| improve this answer | |
  • Thanks for answering, The implementation of hook_block_view is totally correct. after changing the $img_url path nothing changes. – Dropletz Nov 18 '11 at 17:24
  • The implementation you have probably does work, but it is not structured the way that the Drupal docs recommends. I have adjusted my answer to fit with what I've just realised the problem could be! – Chapabu Nov 18 '11 at 18:48
  • problem solved: the 'image_style_url($style, $img_url)' function can only accept images that are placed in public folder not out of it. – Dropletz Nov 20 '11 at 9:29
  • You could answer the question yourself and mark it as the correct answer so that other's can find it as a resource. – Malks Dec 19 '11 at 1:46
1

The public:// stream wrapper maps to your site's public files directory: trying to access themes outside of it isn't going to work. That is, this will never work:

$img_url = 'public://sites/all/themes/glossy/logo.png';

Because it gets expanded to:

$img_url = DRUPAL_ROOT . '/sites/<sitename>/files/sites/all/themes/glossy/logo.png';

Instead, to build the correct path to your theme's file, you need to use drupal_get_path() like Chapabu mentioned:

$img_url = drupal_get_path('theme', 'glossy') . '/logo.png';

However, as you noted in the comments, image_style_url() requires a stream wrapper URL, and more specifically, a public:// stream wrapper URL.

Creating a styled version of your theme's logo by copying the logo

So, the easiest way to do what you want is to copy your theme's logo to your public files directory and use image_style_url() that way:

$img_path = file_unmanaged_copy($img_url, 'public://', FILE_EXISTS_REPLACE);
$block['content'] = theme('image', array('path' => image_style_url($style, $img_path)));

Creating a styled version of your theme's logo without copying the logo

Alternatively, you could manually create the derivative using image_style_create_derivative():

$img_destination = 'public://logo-' . $style . '.png';
image_style_create_derivative($style, $img_url, $img_destination);

Then reference the file's URL by calling file_stream_wrapper_get_instance_by_uri():

$image = file_stream_wrapper_get_instance_by_uri($img_destination);
$block['content'] = theme('image', array('path' => $image->getExternalUrl()));

In either case, you want to make sure at some point you add some logic to deal with repeatedly copying the logo (or generating the derivative) every time someone accesses the block uncached.

| improve this answer | |
0

Use below two function to create images based on image style.

//new_style is image style 
//image_uri is public path of existing image
 $new_image_destination=image_style_path('new_style',$image_uri); 
    $new_image_path=image_style_create_derivative(image_style_load('new_style'),$image_uri,$new_image_destination);

//use drupal_realpath to get public uri as below
//$new_image_public_uri= drupal_realpath($new_image_destination);
| improve this answer | |

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.