Last updated March 26, 2011. Created by drupalthinkers on July 23, 2009.
Edited by silverwing. Log in to edit this page.
There is a big need in all of our projects to google search enable our images. One of the important criteria for the same is the image file name.
We did not have educated teams to upload or name images separately. Hence we used this module to automate the process. The Image file name was the first few characters of the associated image title field, with a provision to override. In the admin interface the admin can set the number of characters, and also the image fields with which this feature can be associated. This works only with CCK module.
alter_image_name.info
; $Id: text.info,v 1.2.2.3 2009/06/18 23:06:32 drupalthinkers Exp $
name = Alter Image Name
description = Module for renaming the image name when upload
; Information added by drupal.org packaging script on 2007-08-16
version = "5.x-1.20"
project = "googleImage"
datestamp = "1245283200"alter_image_name.js
$(document).ready(function (){
if(document.getElementById("edit-title")){
$("#edit-title").blur(function (){
$("#rename-div").each(function (){
$(this).find("input").val(onValidate($("#edit-title").val()));
});
});
}
});
function onValidate(validate_str){
validate_str=validate_str.replace(/ /g,"_");
return validate_str;
}alter_image_name.module
<?php
# Implementation hook_menu
DEFINE("DEFAULT_FILENAME","enter_your_filename_here");
function alter_image_name_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/image-fields',
'title' => t('Content Image fields'),
'callback' => 'drupal_get_form',
'callback arguments' => array('rename_fields_admin_settings'),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'description' => t('Image rename field settings.'),
);
}
return $items;
}
function alter_image_name_init(){
if(arg(1)=="content" && arg(2)=="type"){
variable_del("image_row");
}
}
function rename_fields_admin_settings(){
$form=array();
$sql="SELECT field_name,label FROM {node_field_instance} WHERE widget_type='image' GROUP BY field_name";
$rs=db_query($sql);
$image_set=variable_get("image_row",'');
$row_count=db_num_rows($rs);
if(count($image_set)==$row_count){
$check=false;
}else{
$check=true;
$image_rest=array_flip($image_set);
}
$form['image_fieldset']=array("#type"=>"fieldset");
while($row=db_fetch_object($rs)){
if($check && $image_rest[$row->field_name]!=''){
$form['image_fieldset'][$row->field_name]=array(
"#type"=>"checkbox",
"#title"=>$row->label,
"#attributes"=>array(
"checked"=>$flag
)
);
}else{
$form['image_fieldset'][$row->field_name]=array(
"#type"=>"checkbox",
"#title"=>$row->label,
);
}
}
$form['Submit']=array("#type"=>"submit","#value"=>"Submit");
return $form;
}
function rename_fields_admin_settings_submit($form_id,$form_values){
$image_set=array();
foreach($form_values as $key => $values){
if($values=="1"){
$image_set[]=$key;
}
}
variable_set("image_row",$image_set);
}
function alter_image_name_form_alter($form_id, &$form) {
$sql_image_fields="SELECT field_name,type
FROM {node_field}
WHERE type='image'";
$image_row=variable_get("image_row",'');
if($image_row==""){
$image_rs=db_query($sql_image_fields);
$image_row=array();
if(db_num_rows($image_rs)>0){
while($image_obj=db_fetch_object($image_rs)){
$image_row[]=$image_obj->field_name;
}
variable_set("image_row",$image_row);
}
}
if(!empty($image_row) && is_array($image_row)){
foreach($image_row as $key => $value){
if(!empty($form[$value]) && is_array($form[$value])){
foreach($form[$value] as $key1 => $value1){
if(!empty($value1) && is_array($value1)){
foreach($value1 as $key2 => $value2){
if($key2=="#type" && $value2=="file"){
$form["image_renamed_$key1"]=array("#type"=>"textfield",
'#title'=>$form[$value]['#title']." Rename",
'#attributes' => array('disabled'),
"#description"=>"",
"#weight"=>$form[$value]['#weight'],
'#value' => ($form[$value][0]['filename']!='')?($form[$value][0]['filename']['#value']):(DEFAULT_FILENAME),
"#prefix"=>"<div id='rename-div'>",
"#suffix"=>"</div>");
}
}
}
}
}
}
}
}
function alter_image_name_nodeapi(&$node, $op){
drupal_add_js(drupal_get_path('module', 'alter_image_name') .'/alter_image_name.js', 'module');
$image_row=variable_get("image_row",'');
switch($op){
case "submit":
case "prepare":
case "insert":
case "update":
if(!empty($_FILES['files']['name'])){
foreach($image_row as $key => $value){
foreach($_FILES['files']['name'] as $key1 => $value1){
if(trim($value1)&& $_POST["image_renamed_$key1"]!='' && trim($_POST["image_renamed_$key1"])!=DEFAULT_FILENAME){
$type_dt="-".date("dmy").substr($_FILES['files']['name'][$key1],strlen($_FILES['files']['name'][$key1])-4,strlen($_FILES['files']['name'][$key1]));
if($key1==$value."_upload")
$_FILES['files']['name'][$key1]=build_name(substr($_POST["image_renamed_$key1"],0,25)).$type_dt;
}
}
}
}
}
}
function build_name($replace_str){
$replace=array('?','@','%','!','$','*',')',
'(','^','{','}','[',']',';',
':',',','>','<','"',"'","/",
"\\"," ","_");
$frist_char=array("-","_","0","1","2","3","4","5","6","7","8","9");
$replace_str=str_replace($replace,"-",$replace_str);
$replace_str=str_replace($frist_char,"A",substr($replace_str,0,1)).substr($replace_str,1);
return $replace_str;
}
?>