0

I have a folder in my sites/default/files directory filled with product jpg's gathered from an external server and back end stock take system.

The files look like this...

  • Bread.jpg
  • Bakery.jpg
  • Cakes+and+Puddings.jpg
  • Chocolate+Bars.jpg
  • Chocolates+%26+Treats.jpg
  • Christmas+Fruit+%26+Veg.jpg

I am using theme_image_style in a custom block template to print the images using an image style from Drupal 7's core image handler like so...

<?php
    print theme_image_style(
        array(
            'style_name' => 'category_header', 
            'path' => 'product_group_images/' . $productcategoryurl . '.jpg',  
            'attributes' => array(
            'class' => 'category-header-image'
                 ),
            'width' => NULL, 
            'height' => NULL,
        )
    );

?>

Where $productcategoryurl is the image name inside the product_group_images folder.

This all works great where the image is only one word, like bread.jpg or bakery.jpg

The trouble I am having is the "+" or "&" (%26) in the image names.

I have tried str_replace to swap the spaces in the product name with "+", but in the path, the "+" is always encoded, and the image is never saved in the new style.

So my path comes out like....

  • /sites/default/files/product_group_images/Chocolate%2BBars.jpg

Instead of...

  • /sites/default/files/product_group_images/Chocolate+Bars.jpg

And therefore, no image is created in the /sites/all/files/styles directory.

Is there a way using the "path" in the array to actually use the "+" as a space without encoding it?

I hope ive explained that well enough, its late!

0

I would use a hyphen "-" instead of plus "+" as it is URL safe and the standard MO on Drupal

5
  • Yeah that would be ideal. But I'm stuck with a folder of these images and file names which is populated from the external site.
    – Collins
    Oct 22 '14 at 7:34
  • It seems I missed what you are trying to do with the str_replace() function, are you not renaming the files?
    – Darvanen
    Oct 22 '14 at 23:04
  • The stock updates from the factory through an external system, where staff upload the latest product photo. Then on a cron run, all photos are gathered from the other system and placed in a folder in the sites/default folder, so I have no control over the names of the files until they reach that folder. I am trying to do it with those file names, as manually changing the names is not an option as stock is always updated and changed seasonally due to the farms supplying and their yield each year.
    – Collins
    Oct 23 '14 at 8:50
  • I see what you mean. I am also given the name of the product in the product array. So I am matching the name "Chocolate bars" to the filename "Chocolate+bars.jpg" by using str_replace. The problem is the path name is getting encoded with %2B and i dont think it is using the file name to create the cropped image cache image
    – Collins
    Oct 23 '14 at 8:53
  • Oh now I understand, yeah this answer doesn't apply at all, will delete it in 24 hours or if you answer, whichever comes sooner.
    – Darvanen
    Oct 23 '14 at 23:56
0

You might find it easiest to set up a scheduled task that synchronises the files to another location and changes the file names to match the product name along the way.

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.