Devo modificare uno script php in modo tale che mi applichi a delle foto un watermar semplicissimo, una semplice stringa di testo col nome del mio sito:

Codice 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($base0, -1);
  if ( 
'/' == substr($dir, -1)) $dir substr($dir0, -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($filesSORT_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] >= && $is[2] <= && $is[2] )
   
   
$fn $file;
   
$bigmode false;
   if( ! 
$skip_resize &&  $is[2] != ) {
    
$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"0true);
    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
"
;
       
$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] != ) {
    
$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\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 */


?> 
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:

Codice PHP:
function iw_resize($image_path$target_path$max_width$max_height)
{
//echo "
Processing $image_path --> $target_path
";
# 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);

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ì:

Codice PHP:
function watermark($image_path)
{
echo 
"
Applicazione watermark 
$image_path
"
;
# 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 = [url]www.miosito.com;[/url]
  
$tmp_img $img;
  
$textcolor imagecolorallocate($tmp_img255255255);
  
imagestring($tmp_img566, [url]www.miosito.com,[/url$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 
"
Errore nell'immagine sorgente
"

}

# Save the image
imagejpeg($img$image_path75);

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:

Codice PHP:
Warningimagejpeg(): 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:372in /home/content/a/m/a/amaporn86/html/wp-content/plugins/imagewiz.php on line 194 

Se qualcuno può aiutarmi.....


GRAZIE