Crea logo drupal_set_header - imagecreatetruecolor - imagecolorallocate - imagettftext - imagettftext

1 contenuto / 0 new
Crea logo drupal_set_header - imagecreatetruecolor - imagecolorallocate - imagettftext - imagettftext

Questo script funziona. Chi mi aiuta a migliorarlo?
Per provarlo basta fare un copia incolla in un modulo di test. Vorrei visualizzare l'anteprima correttamente con il font selezionato e creare un bel logo dopo il submit.

<?php
function modulo_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/help#modulo":
      $output = '<p>'.  t("Permette di gestire il modulo ") .'</p>';
  }
  return $output;
}
function modulo_init() {
  drupal_add_js(drupal_get_path('module', 'modulo') .'/includes/modulo.js');
}
/**
* Valid permissions for this module
* @return array An array of valid permissions for the aziende module
*/
function modulo_perm() {
  return array('access modulo', 'post modulo', 'search modulo', 'administer modulo');
}
function modulo_block($op = 'list', $delta = 0, $edit = array()) {
  global $user;
  $block = array();
    switch($op) {
        case "list":
          $block = array();
          $block[0]["info"] = t("Featured Companies");
          return $block;
        break;
        case "view":
              $block_content = '';
              $imageshow = drupal_get_path("module", "aziende") . "/pictures/businessman_02.jpg";
              $imageshow = base_path() . $imageshow;
              $img = "<img src='$imageshow' class='feed-image' width='180' border='1' vspace='0' hspace='0' alt='Aziende' title='Aziende'>";
              $block_content .= l($img, 'aziende', array('html' => 'true'));
              $options = array( "attributes" => array("title" => t("Da qui si puo accedere") ) );
              $link1 = l( t('Aziende'), 'aziende', $options);
              $block_content .= "<div class="more-link">" . $link1 . "</div>";
              $block['subject'] = t("Featured Companies");
              $block['content'] = $block_content;
              return $block;
        break;
  }
}
function modulo_menu() {
  $items = array();
  $items['modulo/crealogoazienda'] = array(
    'title' => t('Crea logo'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('modulo_crealogoazienda_form'),
    'access arguments' => array('post modulo'),
    'type' => MENU_NORMAL_ITEM,
   );
  $items['modulo/crealogoazienda_make'] = array(
    'title' => t('Visualizza logo'),
    'page callback' => 'modulo_crealogoazienda_make',
    'access arguments' => array('post modulo'),
    'type' => MENU_CALLBACK,
   );
  return $items;
}
function modulo_crealogoazienda_form(){
$form['#submit'][] = 'modulo_crealogoazienda_form_submit';
$form['#redirect'] = 'modulo/crealogoazienda_make';
  $browser = $_SERVER['HTTP_USER_AGENT'];
  if (preg_match("/Win/", $browser)) { $os = "Win"; }
  if (preg_match("/Linux/", $browser)) { $os = "Linux"; }
  if (preg_match("/Mac/", $browser)) { $os = "Mac"; }
  $form['text'] = array(
    '#type' => 'textfield',
    '#title' => t('Scritta'),
    '#maxlength' => 60,
    '#required' => TRUE,
    '#default_value' => 'My Company',
  );
  if ($os=="Win") $font_path = "C:/Windows/Fonts/";
  if ($os=="Mac") $font_path = "/var/root/Library/Fonts/";
  if ($os=="Linux") $font_path = "/usr/share/fonts";
  $dh = opendir($font_path);
  $cont=0;
  while (($file1 = readdir($dh)) !== FALSE) {
    if($file1 != '..' && $file1 !='.' && $file1 !=''){
      if (ereg(".ttf|.TTF|.fon|.FON",$file1)) {
        $options[$file1] = $file1;
      }
     $cont++;
    }
   }
  closedir($dh);
  $form['previewfont'] = array(
   '#type' => 'item',
   '#title' => '',
   '#value' => '',
   '#prefix' => '<div id="attach-wrapper">.',
   '#suffix' => '</div>',
  );
  $form['font'] = array(
    '#type' => 'select',
    '#title' => t("Scegli il tipo di carattere"),
    '#required' => FALSE,
    '#options' =>  $options,
    '#default_value' => variable_get('elementfonttype', 'arial.ttf'),
    '#attributes' => array('onchange' => 'selCharacter();'),
  );
  $options = array('20' => '20', '30' => '30', '40' => '40', '50' => '50', '60' => '60', '70' => '70');
  $form['size'] = array(
    '#type' => 'select',
    '#title' => t("Scegli la grandezza del carattere"),
    '#required' => FALSE,
    '#options' =>  $options,
    '#default_value' => variable_get('elementsizefont', 30),
    '#attributes' => array('onchange' => 'selCharacter();'),
  );
  $form['color'] = array(
    '#type' => 'colorpicker',
    '#title' => t('Clicca e trascina per selezionare il colore desiderato della scritta'),
    '#default_value' => 'de1d3d',
    '#attributes' => array('style' => 'background-color:#FFFFFF', 'onchange' => 'selCharacter();'),
  );
  $form['outline'] = array(
    '#type' => 'colorpicker',
    '#title' => t('Clicca e trascina per selezionare il colore desiderato outline'),
    '#default_value' => 'CCCCCC',
    '#attributes' => array('style' => 'background-color:#FFFFFF', 'onchange' => 'selCharacter();'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Crea'),
    '#prefix' => '<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>',
    '#suffix' => '<br>',
  );
return $form;
}
function modulo_crealogoazienda_form_submit($form, &$form_state){
   $text = $form_state['values']['text'];
   $font = $form_state['values']['font'];
   $size = $form_state['values']['size'];
   $color = $form_state['values']['color']; $color = "#".$color;
   $outline = $form_state['values']['outline']; $outline = "#".$outline;
    // Set the content-type
    drupal_set_header("Content-type: image/png");
    // Create the image
    $theimage = imagecreatetruecolor(300, 170);
    // Create some colors
    $white = imagecolorallocate($theimage, 255, 255, 255);
    $grey = imagecolorallocate($theimage, 128, 128, 128);
    $black = imagecolorallocate($theimage, 0, 0, 0);
    imagefilledrectangle($theimage, 0, 0, 399, 29, $outline);
    ////array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
    $font = "C:\WINDOWS\Fonts" . $font;
    // Add some shadow to the text
    imagettftext($theimage, $size, 0, 61, 101, $grey, $font, $text);
    // Add the text
    imagettftext($theimage, $size, 0, 50, 90, $white, $font, $text);
    imagepng($theimage, 'sites/default/files/aziende_loghi/' . $text . '.png');
    //imagedestroy($theimage);
   $_SESSION['text'] = $text;
}
function modulo_crealogoazienda_make() {
  $pathdest = base_path() . file_directory_path() . '/aziende_loghi/' . $_SESSION['text'] . '.png';
  $output = "<p align='center'><img src='$pathdest' alt='$font'/></p>";
  return $output;
}

Questo il file .js:

function selCharacter() {
  var font = document.getElementById('edit-font');
  var size = document.getElementById('edit-size');
  var wrapper = document.getElementById('attach-wrapper');
  var color = document.getElementById('edit-color');
  var outline = document.getElementById('edit-outline');
  var fonttext = font.options[font.selectedIndex].text;
  var fontvalue = font.options[font.selectedIndex].value;
  var sizetext = size.options[size.selectedIndex].text;
  var sizevalue = size.options[size.selectedIndex].value;
  wrapper.innerHTML = '<p style="font-family:'+fonttext+', sans-serif; font-style:normal; font-size:'+sizetext+'pt;"><font face="'+fonttext+'">'+fonttext+'</p>';
}

Grazie Ciao!
Danzisiweb