Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it L'avatar di blekm
    Registrato dal
    Jun 2004
    Messaggi
    8,310

    watermark (solo testo) su immagine

    partendo da qua

    http://php.html.it/script/vedi/4508/...age-with-text/

    watermark_text.example.php

    Codice PHP:
    <?php  

    #################################################################################
    # Watermark Image using Text script usage example
    # For updates visit [url]http://www.zubrag.com/scripts/[/url]
    #################################################################################

    // Watermark text
    $text 'zubrag.com';

    // Watermark text color, Hex format. Must start from #
    $color '#000000';

    // Font name. Case sensitive (i.e. Arial not equals arial)
    $font 'arial.ttf';

    // Font size
    $font_size '8';

    // Angle for text rotation. For example 0 - horizontal, 90 - vertical
    $angle 90;

    // Horizontal offset in pixels, from the right
    $offset_x 0;

    // Vertical offset in pixels, from the bottom
    $offset_y 0;

    // Shadow? true or false
    $drop_shadow true;

    // Shadow color, Hex format. Must start from #
    // This may help to make watermark text more distinguishable from image background
    $shadow_color '#909009';

    // 1 - save as file on the server, 2 - output to browser, 3 - do both
    $mode 1;

    // Images folder, must end with slash.
    $images_folder '/www/water/';

    // Save watermarked images to this folder, must end with slash.
    $destination_folder '/www/water/dest/';

    #################################################################################
    #     END OF SETTINGS
    #################################################################################

    // Load functions for image watermarking
    include("watermark_text.class.php");

    // Watermark all the "jpg" files from images folder
    // and save watermarked images into destination folder
    foreach (glob($images_folder."*.jpg") as $filename)
    {
      
    // Image path
      
    $imgpath $filename;

      
    // Where to save watermarked image
      
    $imgdestpath $destination_folder basename($filename);

      
    // create class instance
      
    $img = new Zubrag_watermark($imgpath);

      
    // shadow params
      
    $img->setShadow($drop_shadow$shadow_color);

      
    // font params
      
    $img->setFont($font$font_size);

      
    // Apply watermark
      
    $img->ApplyWatermark($text$color$angle$offset_x$offset_y);

      
    // Save on server
      
    $img->SaveAsFile($imgdestpath);

      
    // Output to browser
      
    $img->Output();
      
      
    // release resources
      
    $img->Free();
    }
    ?>

    watermark_text.class.php

    Codice PHP:
    <?php  

    #################################################################################
    # Watermark Image with Text Class 1.0
    #################################################################################
    # For updates visit [url]http://www.zubrag.com/scripts/[/url]
    #################################################################################
    #
    # REQUIREMENTS:
    # PHP 4.0.6 and GD 2.0.1 or later
    # May not work with GIFs if GD2 library installed on your server 
    # does not support GIF functions in full
    #
    #################################################################################

    class Zubrag_watermark {

      var 
    $font 'arial.ttf';
      var 
    $font_size 16;
      var 
    $angle 0;
      var 
    $offset_x 0;
      var 
    $offset_y 0;
      var 
    $quality 100;
      var 
    $image_type = -1// Image type: 1 = GIF, 2 = JPG, 3 = PNG
      
    var $force_image_type = -1// Change image type? (-1 = same as original, 1 = GIF, 2 = JPG, 3 = PNG)
      
    var $save_to_file true;
      var 
    $drop_shadow false;

      function 
    Zubrag_watermark($image_path='') {
        
    $this->setImagePath($image_path);
      }

      function 
    setImagePath($image_path) {
        
    $this->image_path $image_path;
      }

      function 
    setShadow($drop_shadow,$color='#606060') {
        
    $this->drop_shadow $drop_shadow;
        
    $this->shadow_color $color;
      }

      function 
    setText($text$color) {
        
    $this->text $text;
        
    $this->color $color;
      }

      function 
    setAngle($angle) {
        
    $this->angle $angle;
      }

      function 
    setOffset($x$y) {
        
    $this->offset_x $x;
        
    $this->offset_y $y;
      }

      function 
    setFont($font$size 0) {
        if (!empty(
    $font)) $this->font $font;
        if (
    $size != 0$this->font_size $size;
      }

      function 
    ImageCreateFromType($type,$filename) {
       
    $im null;
       switch (
    $type) {
         case 
    1:
           
    $im ImageCreateFromGif($filename);
           break;
         case 
    2:
           
    $im ImageCreateFromJpeg($filename);
           break;
         case 
    3:
           
    $im ImageCreateFromPNG($filename);
           break;
        }
        return 
    $im;
      }

      function 
    ApplyWatermark($text=''$color=''$angle=0$offset_x=0$offset_y=0) {

        
    $this->setText($text$color);
        
    $this->setAngle($angle);
        
    $this->setOffset($offset_x$offset_y);

        
    // Determine image size and type
        
    $size getimagesize($this->image_path);
        
    $size_x $size[0];
        
    $size_y $size[1];
        
    $image_type $size[2]; // 1 = GIF, 2 = JPG, 3 = PNG

        // Load image
        
    $this->image $this->ImageCreateFromType($image_type$this->image_path);
        
        
    // Translate color to decimal
        
    $color sscanf($this->color'#%2x%2x%2x');
        
    $color_r $color[0];
        
    $color_g $color[1];
        
    $color_b $color[2];

        
    $this->image_type = ($this->force_image_type != -$this->force_image_type $image_type);

        
    /* Calculate TTF text size */
        
    $ttfsize imagettfbbox($this->font_size$this->angle$this->font$this->text);
        
        
    // Add custom insets
        
    $ttfx $this->offset_x max($ttfsize[0],$ttfsize[2],$ttfsize[4],$ttfsize[6]);
        
    $ttfy $this->offset_y max($ttfsize[1],$ttfsize[3],$ttfsize[5],$ttfsize[7]);
        
        
    /* shadow */
        
    if ($this->drop_shadow) {
          
    // Translate color to decimal
          
    $scolor sscanf($this->shadow_color'#%2x%2x%2x');
          
    $scolor_r $scolor[0];
          
    $scolor_g $scolor[1];
          
    $scolor_b $scolor[2];

          
    $text_color imagecolorallocate($this->image$scolor_r$scolor_g$scolor_b);
          
    imagettftext($this->image$this->font_size
            
    $this->angle// angle
            
    $size_x $ttfx 2// left inset
            
    $size_y $ttfy 2// top inset
            
    $text_color$this->font $this->text)
            or die(
    'Cannot render TTF text.');
        }

        
    /* Render text */
        
    $text_color imagecolorallocate($this->image$color_r$color_g$color_b);
        
    imagettftext($this->image$this->font_size
          
    $this->angle// angle
          
    $size_x $ttfx 3// left inset
          
    $size_y $ttfy 3// top inset
          
    $text_color$this->font $this->text)
          or die(
    'Cannot render TTF text.');

      } 
    // ApplyWatermark

      
    function OutputImageInternal($im$filename='') {
     
        
    $res null;
     
        
    // ImageGIF is not included into some GD2 releases, so it might not work
        // output png if gifs are not supported
        
    if(($this->image_type == 1)  && !function_exists('imagegif')) $this->image_type 3;

        switch (
    $this->image_type) {
          case 
    1:
            if (
    $this->save_to_file) {
              
    $res ImageGIF($im,$filename);
            }
            else {
              
    header("Content-type: image/gif");
              
    $res ImageGIF($im);
            }
            break;
          case 
    2:
            if (
    $this->save_to_file) {
              
    $res ImageJPEG($im,$filename,$this->quality);
            }
            else {
              
    header("Content-type: image/jpeg");
              
    $res ImageJPEG($imNULL$this->quality);
            }
            break;
          case 
    3:
            if (
    PHP_VERSION >= '5.1.2') {
              
    // Convert to PNG quality.
              // PNG quality: 0 (best quality, bigger file) to 9 (worst quality, smaller file)
              
    $quality minround($this->quality 10), );
              if (
    $this->save_to_file) {
                
    $res ImagePNG($im$filename$quality);
              }
              else {
                
    header("Content-type: image/png");
                
    $res ImagePNG($imNULL$quality);
              }
            }
            else {
              if (
    $this->save_to_file) {
                
    $res ImagePNG($im$filename);
              }
              else {
                
    header("Content-type: image/png");
                
    $res ImagePNG($im);
              }
            }
            break;
        }
     
        return 
    $res;
     
      }

      function 
    Output() {
        
    $this->save_to_file false;
        
    $this->OutputImageInternal($this->image);
      }

      function 
    SaveAsFile($filename) {
        
    $this->save_to_file true;
        
    $this->OutputImageInternal($this->image$filename);
      }

      function 
    Free() {
        
    imagedestroy($this->image);
      }

    }
    ?>
    non capisco perchè non mi entra in questo ciclo

    foreach (glob($images_folder."*.jpg") as $filename)

    var_dump di glob($images_folder."*.jpg"); mi dice array di 0 elementi, mentre io nella cartella /www/dest/ ho un immagine -> mare.jpg

    qualche idea?
    Il portale sul turismo a Lucca

    Siti web Lucca

    Vendo Tex a colori di Repubblica (primi 100 numeri) - info in pvt.

  2. #2
    Utente di HTML.it L'avatar di blekm
    Registrato dal
    Jun 2004
    Messaggi
    8,310
    ho trovato una variante, che pare funzionare

    Codice PHP:
    <?

    $SourceFile 
    'mare.jpg';
    $DestinationFile 'mare-w.jpg';
    $WaterMarkText 'Copyright';

    watermarkImage ($SourceFile$WaterMarkText$DestinationFile);

    function 
    watermarkImage ($SourceFile$WaterMarkText$DestinationFile) {
       list(
    $width$height) = getimagesize($SourceFile);
       
    $image_p imagecreatetruecolor($width$height);
       
    $image imagecreatefromjpeg($SourceFile);
       
    imagecopyresampled($image_p$image0000$width$height$width$height);
       
    $black imagecolorallocate($image_p000);
       
    $font 'arial.ttf';
       
    $font_size 14;
       
    imagettftext($image_p$font_size01020$black$font$WaterMarkText);
       if (
    $DestinationFile<>'') {
          
    imagejpeg ($image_p$DestinationFile100);
       } else {
          
    header('Content-Type: image/jpeg');
          
    imagejpeg($image_pnull100);
       };
       
    imagedestroy($image);
       
    imagedestroy($image_p);
    };
    ?>

    la domanda è

    imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);

    10 e 20 sono le coordinate, che partono dall'alto a sinistra... in questo modo la scritta è in alto a sinistra, se io la volessi in basso a destra? Non posso mettere una misura precisa, perchè se cambia la dimensione della foto mi salta tutto, no?

    Esiste una maniera per far partire x e y dal basso in destrA?

    qua non ho trovato niente che mi risolva questo dubbio http://it2.php.net/imagettftext
    Il portale sul turismo a Lucca

    Siti web Lucca

    Vendo Tex a colori di Repubblica (primi 100 numeri) - info in pvt.

  3. #3
    Utente di HTML.it L'avatar di brodik
    Registrato dal
    Jan 2009
    Messaggi
    765
    certo, basta che ti ricavi la larghezza e l'altezza dell'immagine in cui andrai ad applicare il watermark e gli sottrai i valori che vuoi..

  4. #4
    Utente di HTML.it L'avatar di blekm
    Registrato dal
    Jun 2004
    Messaggi
    8,310
    hai ragione, non ci avevo pensato

    proverò, grazie
    Il portale sul turismo a Lucca

    Siti web Lucca

    Vendo Tex a colori di Repubblica (primi 100 numeri) - info in pvt.

  5. #5
    Utente di HTML.it L'avatar di blekm
    Registrato dal
    Jun 2004
    Messaggi
    8,310
    sotto consiglio di brodik, ho ricavato le dimensioni dell'immagine a cui applicare il watermark, e dentro il codice modificato cosi

    Codice PHP:
    $height_diviso_2 $height/2;
    $width_diviso_2 $width/2;

    imagettftext($image_p$font_size0$width_diviso_2$height_diviso_2$white$font$WaterMarkText); 
    però ho un problema. In verticale la scritta è sempre centrata, ma in orizzontale no.

    Vorrei poter inserire una qualsiasi scritta di qualunque dimensione e lunghezza, e che questa sia sempre centrata anche in orizzontale (cioè a destra e sinistra stesso spazio), ora ottengo questi risultati





    qualche idea?
    Il portale sul turismo a Lucca

    Siti web Lucca

    Vendo Tex a colori di Repubblica (primi 100 numeri) - info in pvt.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.