• User

    Script per mettere un watermark ad una foto.

    Salve,

    ho il seguente problema, sto cercando uno script php che mi consenta di applicare un semplice watermark testuale a tutte le immagini posizionate in una certa directory sul mio server LINUX.

    Esiste una soluzione GRATIS ?

    A pagamento ho trovato questo: http://www.scriptsjoint.com/watermarkscript.php

    Che a quanto pare farebbe anche molto in più...

    Sapete se esiste uno script che fa sta cosa, ma che è GRATIS ?

    Grazie


  • Moderatore

    Esiste una soluzione GRATIS ?
    Sì. Ti studi le librerei gd oppure imagemagick 🙂


  • User

    ti ringrazio per l'aiuto, io parlavo di qualche scriopt opensource.....,

    visto che di opensource c'è di tutto...


  • User Attivo

    Se ti metti a cercare script già fatti perdi più tempo...

    Qui http://www.php.net/manual/en/ref.image.php] leggi la documentazione sulla libreria GD (la più diffusa senz'altro) e le relative funzioni. Vedi che con un paio di queste al massimo hai risolto, invece di scomodare Google. Proprio come ha detto massy 😉

    CiaoCiao


  • User

    Allora, lo script che devo modificare è il seguente:


    [PHP]function imw_gen_image_post()
    {
    global $wpdb;
    $_exif = function_exists('exif_read_data');

    /* POST Actions */
    if( $_REQUEST['postact'] == 'genpost' )
    {

    // GO!
    $siteurl = get_settings('siteurl');
    $num_perpage = intval($_REQUEST['num_perpage']);
    $skip_resize = intval($_REQUEST['skip_resize']);

    $base = str_replace("\",'/', $_REQUEST['base_url']);
    $path = str_replace("\",'/', $_REQUEST['base_path']);

    if( $path == '' )
    {
    $location = $PHP_SELF . "?page={$_REQUEST['page']}&msg=1";
    header("Location: $location");
    exit();
    }

    $append = $_REQUEST['append'];
    $prepend = $_REQUEST['prepend'];
    $path = str_replace(ABSPATH, '', trim($path));
    $dir = ABSPATH . $path;

    if($base == '' ) {
    $base = trailingslashit($siteurl) . $path;
    }
    if ( '/' == substr($base, -1)) $base = substr($base, 0, -1);
    if ( '/' == substr($dir, -1)) $dir = substr($dir, 0, -1);

    if( !file_exists($dir) ) {
    $location = $PHP_SELF . "?page={$_REQUEST['page']}&msg=2";
    header("Location: $location");
    exit();
    }

    $width = intval($_REQUEST['i_width']); //$width = $width ? $width : 640;
    $height = intval($_REQUEST['i_height']); //$height = $height ? $height : 480

    $html = '';

    $n = 0;
    $files = array();
    if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
    $files[] = $file;
    }
    }
    closedir($handle);
    }
    sort($files, SORT_STRING);

    if ( ! $skip_resize && ! wp_mkdir_p( "$dir/th" ) ) {
    echo sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), "$dir/th");
    die();
    }

    foreach( $files as $file)
    {
    $img = "$dir/$file";

    if( ! is_file($img) ) continue;

    if( ($is = getimagesize($img)) === false ) continue;
    if( $is[2] >= 1 && $is[2] <= 6 && $is[2] )

    $fn = $file;
    $bigmode = false;
    if( ! $skip_resize && $is[2] != 4 ) {
    $fn = 'th/' . strtolower(str_replace('.','_thumb.', $file));
    iw_resize( $img, "$dir/$fn" , $width, $height);

    watermark($img); <---------------------
    
    $bigmode = true;
    

    }

    $prepend1 = str_replace('%file%', $file, $prepend);
    $append1 = str_replace('%file%', $file, $append);
    $prepend1 = str_replace('%width%', $is[0], $prepend1);
    $append1 = str_replace('%width%', $is[0], $append1);
    $prepend1 = str_replace('%height%', $is[1], $prepend1);
    $append1 = str_replace('%height%', $is[1], $append1);
    $prepend1 = str_replace('%url%', "{$base}/{$file}", $prepend1);
    $append1 = str_replace('%url%', "{$base}/{$file}", $append1);

    if($_exif) {
    $exif_data = exif_read_data("$dir/$file", 0, true);
    if( !( $exif_data === false ) )
    {
    $val2 = '';
    foreach ($exif_data as $key => $section) {
    foreach ($section as $name => $val) {
    $k0 = strtolower($key);
    $k1 = strtolower($name);
    $val2 .= strtolower($key.'.'.$name)." = $val<br />";
    $prepend1 = str_replace('%'.$k0.'.'.$k1.'%', $val, $prepend1);
    $append1 = str_replace('%'.$k0.'.'.$k1.'%', $val, $append1);
    }
    }

     $prepend1 = str_replace('%exif%', $val2, $prepend1);
     $append1 = str_replace('%exif%', $val2, $append1);
     
    }
    

    }

    if( $is[2] != 4 ) {
    $is = getimagesize("$dir/$fn");
    $params = " {$is[3]} vspace="4" alt="{$file}"";
    $html .= $prepend1;
    if( $bigmode ) $html .= "<a href="javascript:void(0)" onclick="window.open('{$base}/{$file}');" >";
    $html .= "<img src="{$base}/{$fn}" $params />";
    if( $bigmode ) $html .= "</a>";
    $html .= $append1;
    $n++;
    }else{
    }

    // $n++;
    if( $num_perpage && ($n%$num_perpage==0) ) {
    $html .= "\n<!--more-->\n";
    }
    }

    $_POST['comment_status'] = 'open';
    $_POST['advanced_view'] = 1;
    $_POST['post_category'] = array();
    $_POST['post_status'] = $_REQUEST['post_type'];
    $_POST['content'] = $html;
    $_POST['post_title'] = $_POST['post_title'] ? $_POST['post_title'] : 'Image Wizard Draft';

    $post_ID = write_post();
    if( $post_ID ) {
    $location = "post.php?action=edit&post=$post_ID";
    }
    else
    {
    $location = $PHP_SELF . "?page={$_REQUEST['page']}";
    }
    header("Location: $location");
    exit();

    }
    /* END */

    ?>[/PHP]

    In pratica fa questo si legge tutte le immagini in una cartella, e crea per ciascuna una miniatura, salvandola in un subdirectory della cartella madre tramite la funzione seguente:


    [PHP]function iw_resize($image_path, $target_path, $max_width, $max_height)
    {
    //echo "<br/>Processing $image_path --> $target_path<br/>";

    Load image

    $img = null;
    $ext = strtolower(end(explode('.', $image_path)));
    if ($ext == 'jpg' || $ext == 'jpeg') {
    $img = @imagecreatefromjpeg($image_path);
    } else if ($ext == 'png') {
    $img = @imagecreatefrompng($image_path);

    Only if your version of GD includes GIF support

    } else if ($ext == 'gif') {
    $img = @imagecreatefrompng($image_path);
    }

    If an image was successfully loaded, test the image for size

    if ($img) {

    Get image size and scale ratio

    $width = imagesx($img);
    $height = imagesy($img);
    $scale = min($max_width/$width, $max_height/$height);

    If the image is larger than the max shrink it

    if ($scale < 1) {
    $new_width = floor($scale*$width);
    $new_height = floor($scale*$height);

    Create a new temporary image

    $tmp_img = imagecreatetruecolor($new_width, $new_height);

    Copy and resize old image into new image

    //imagecopyresized imagecopyresampled
    imagecopyresampled($tmp_img, $img, 0, 0, 0, 0,
    $new_width, $new_height, $width, $height);
    imagedestroy($img);
    $img = $tmp_img;
    }
    }

    Create error image if necessary

    if (!$img) {
    $img = imagecreate($max_width, $max_height);
    imagecolorallocate($img,0,0,0);
    $c = imagecolorallocate($img,70,70,70);
    imageline($img,0,0,$max_width,$max_height,$c2);
    imageline($img,$max_width,0,0,$max_height,$c2);
    }

    Save the image

    imagejpeg($img, $target_path);
    }[/PHP]


    Bene allora ho detto mettiamo una funzione watermark($img) che applico a ciscuna delle immagini nel loop principale, (l'ho indicata in nero nel codice - vedi primo blocco)

    La funzione, per quel poco che ne capisco di PHP l'ho scritta così:

    [PHP]function watermark($image_path)
    {
    echo "<br/>Applicazione watermark $image_path<br/>";

    Load image

    $img = null;
    $ext = strtolower(end(explode('.', $image_path)));
    if ($ext == 'jpg' || $ext == 'jpeg') {
    $img = @imagecreatefromjpeg($image_path);
    } else if ($ext == 'png') {
    $img = @imagecreatefrompng($image_path);

    Only if your version of GD includes GIF support

    } else if ($ext == 'gif') {
    $img = @imagecreatefrompng($image_path);
    }

    If an image was successfully loaded, test the image for size

    if ($img) {

    Get image size and scale ratio

    //$larghezza = imagesx($img);
    //$altezza = imagesy($img);
    //$copyright = www.miosito.com;
    $tmp_img = $img;
    $textcolor = imagecolorallocate($tmp_img, 255, 255, 255);
    imagestring($tmp_img, 5, 6, 6, www.miosito.com, $textcolor);
    //$scale = min($max_width/$width, $max_height/$height);
    imagedestroy($img);
    $img = $tmp_img;

    If the image is larger than the max shrink it

    /* if ($scale < 1) {
    $new_width = floor($scale*$width);
    $new_height = floor($scale*$height);

    Create a new temporary image

    $tmp_img = imagecreatetruecolor($new_width, $new_height);

    Copy and resize old image into new image

    //imagecopyresized imagecopyresampled
    imagecopyresampled($tmp_img, $img, 0, 0, 0, 0,
    $new_width, $new_height, $width, $height);
    imagedestroy($img);
    $img = $tmp_img;
    }
    */
    }

    Create error image if necessary

    if (!$img) {
    echo "<br/>Errore nell'immagine sorgente<br/>";
    }

    Save the image

    imagejpeg($img, $image_path, 75);
    }[/PHP]


    In pratica vorrei che: il programma si scorre le immagini, per ciascuna crea la miniatura e poi piglia l'iimagine originale, vi applica l'watermark ( una semplice stringa col nome del mio sito ) e sovrascrive il file originale. Solo questo.

    L'errore che ricevo è il seguente, chiaramente l'watermark non me lo mette:

    [PHP]Warning: imagejpeg(): 101 is not a valid Image resource in /home/content/a/m/a/amaporn86/html/wp-content/plugins/imagewiz.php on line 422

    Warning: Cannot modify header information - headers already sent by (output started at /home/content/a/m/a/amaporn86/html/wp-content/plugins/imagewiz.php:372) in /home/content/a/m/a/amaporn86/html/wp-content/plugins/imagewiz.php on line 194[/PHP]


    Resto in attesa di una dritta che mi illumini su cosa sbaglio.

    GRAZIE