How to set image style in your own module in Drupal 7
Some time ago one of our customers asked to port gallery from Drupal 6 site to Drupal 7 one.
The module was not complex, the main difficult that I faced during the process to change Image Cache approach to the Image style one that native in Drupal 7. The main idea is to set resize rules of thumbnails in the gallery. Users should no do that themselves.
Previously in Drupal 6 there was following solution. The Image Cache preset set in the .install file.
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #FF8000">/**<br> * Implementation of hook_install().<br> */<br></span><span style="color: #007700">function </span><span style="color: #0000BB">ms_gallery_install</span><span style="color: #007700">() {<br> </span><span style="color: #FF8000">// Create preset.<br> </span><span style="color: #0000BB">ms_gallery_install_imagecache_presets</span><span style="color: #007700">();<br>}<br><br>function </span><span style="color: #0000BB">ms_gallery_install_imagecache_presets</span><span style="color: #007700">() {<br> </span><span style="color: #FF8000">// First, build an array of all the preset names so we do not make duplicates<br> // Set the argument to TRUE to reset the cache<br> </span><span style="color: #0000BB">$presets </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecache_presets</span><span style="color: #007700">(</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">$preset_names </span><span style="color: #007700">= array();<br><br> </span><span style="color: #FF8000">// If there are any presets<br> </span><span style="color: #007700">if (</span><span style="color: #0000BB">$presets </span><span style="color: #007700">!= </span><span style="color: #DD0000">''</span><span style="color: #007700">) {<br> foreach (</span><span style="color: #0000BB">$presets </span><span style="color: #007700">as </span><span style="color: #0000BB">$preset</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$preset_names</span><span style="color: #007700">[] = </span><span style="color: #0000BB">$preset</span><span style="color: #007700">[</span><span style="color: #DD0000">'presetname'</span><span style="color: #007700">];<br> }<br> }<br><br> </span><span style="color: #FF8000">// Prepare to install ImageCache presets<br> </span><span style="color: #0000BB">$imagecache_presets </span><span style="color: #007700">= array();<br> </span><span style="color: #0000BB">$imagecache_actions </span><span style="color: #007700">= array();<br><br> </span><span style="color: #FF8000">// We are checking to make sure the preset name does not exist before creating<br> </span><span style="color: #007700">if (!</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #DD0000">'ms_gallery'</span><span style="color: #007700">, </span><span style="color: #0000BB">$preset_names</span><span style="color: #007700">)) {<br> </span><span style="color: #0000BB">$imagecache_presets</span><span style="color: #007700">[] = array(<br> </span><span style="color: #DD0000">'presetname' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'ms_gallery'</span><span style="color: #007700">,<br> );<br> </span><span style="color: #0000BB">$imagecache_actions</span><span style="color: #007700">[</span><span style="color: #DD0000">'ms_gallery'</span><span style="color: #007700">][] = array(<br> </span><span style="color: #DD0000">'action' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'imagecache_scale_and_crop'</span><span style="color: #007700">,<br> </span><span style="color: #DD0000">'data' </span><span style="color: #007700">=> array(<br> </span><span style="color: #DD0000">'width' </span><span style="color: #007700">=> </span><span style="color: #0000BB">120</span><span style="color: #007700">,<br> </span><span style="color: #DD0000">'height' </span><span style="color: #007700">=> </span><span style="color: #0000BB">67</span><span style="color: #007700">,<br> ),<br> </span><span style="color: #DD0000">'weight' </span><span style="color: #007700">=> </span><span style="color: #0000BB">0</span><span style="color: #007700">,<br> );<br> }<br> </span><span style="color: #FF8000">// Need to install preset, id will be returned by function,<br> // Then install action add presetid to action prior to install:<br> </span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$imagecache_presets </span><span style="color: #007700">as </span><span style="color: #0000BB">$preset</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$preset </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecache_preset_save</span><span style="color: #007700">(</span><span style="color: #0000BB">$preset</span><span style="color: #007700">);<br> foreach (</span><span style="color: #0000BB">$imagecache_actions</span><span style="color: #007700">[</span><span style="color: #0000BB">$preset</span><span style="color: #007700">[</span><span style="color: #DD0000">'presetname'</span><span style="color: #007700">]] as </span><span style="color: #0000BB">$action</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$action</span><span style="color: #007700">[</span><span style="color: #DD0000">'presetid'</span><span style="color: #007700">] = </span><span style="color: #0000BB">$preset</span><span style="color: #007700">[</span><span style="color: #DD0000">'presetid'</span><span style="color: #007700">];<br> </span><span style="color: #0000BB">imagecache_action_save</span><span style="color: #007700">(</span><span style="color: #0000BB">$action</span><span style="color: #007700">);<br> }<br> </span><span style="color: #0000BB">drupal_set_message</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'ImageCache preset %id: %name and corresponding actions saved.'</span><span style="color: #007700">, array(</span><span style="color: #DD0000">'%id' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$preset</span><span style="color: #007700">[</span><span style="color: #DD0000">'presetid'</span><span style="color: #007700">], </span><span style="color: #DD0000">'%name' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$preset</span><span style="color: #007700">[</span><span style="color: #DD0000">'presetname'</span><span style="color: #007700">])));<br> }<br>}<br></span><span style="color: #0000BB">?></span></span>
Above the part of ms_gallery.install code. I would like to show how to set Image Cache preset programmatically.
In the case of Drupal 7 there is no Image Cache module. Instead it we should use Image Style approach that included in the Drupal 7 core.
I try to find any example in the Google, but the examples that I found does not work.
There is no any other option that to study Drupal.org. And after several hours I have found following beautiful HOOK:hook_image_default_styles()
In terms of this hook to solve my task will be so easy!
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #007700">function </span><span style="color: #0000BB">ms_gallery_image_default_styles</span><span style="color: #007700">() {<br> </span><span style="color: #0000BB">$styles </span><span style="color: #007700">= array();<br><br> </span><span style="color: #0000BB">$styles</span><span style="color: #007700">[</span><span style="color: #DD0000">'carousel_gallery'</span><span style="color: #007700">] = array(<br> </span><span style="color: #DD0000">'effects' </span><span style="color: #007700">=> array(<br> array(<br> </span><span style="color: #DD0000">'name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'image_scale_and_crop'</span><span style="color: #007700">, <br> </span><span style="color: #DD0000">'data' </span><span style="color: #007700">=> array(<br> </span><span style="color: #DD0000">'width' </span><span style="color: #007700">=> </span><span style="color: #0000BB">120</span><span style="color: #007700">,<br> </span><span style="color: #DD0000">'height' </span><span style="color: #007700">=> </span><span style="color: #0000BB">67</span><span style="color: #007700">,<br> </span><span style="color: #DD0000">'upscale' </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">,<br> ), <br> </span><span style="color: #DD0000">'weight' </span><span style="color: #007700">=> </span><span style="color: #0000BB">0</span><span style="color: #007700">,<br> ),<br> ),<br> );<br><br> return </span><span style="color: #0000BB">$styles</span><span style="color: #007700">;<br>}<br></span><span style="color: #0000BB">?></span></span>
I really like Drupal 7 :-)
Blog tags: Planet DrupalDrupal FAQ