Jump to content

watermark and resizing


cosmicsea

Recommended Posts

Hi I am using this script for water marking images from here

http://911-need-code-help.blogspot.com/2008/11/watermark-your-images-with-another.html

which works great. I also tried using this resizer here

http://911-need-code-help.blogspot.com/2008/10/resize-images-using-phpgd-library.html

because it works great too but trying to get both of them to work together wasn't working for me. The 2 codes are very similar and the farthest I got was being able to produce 2 images at the same time "one watermarked and one resized" I could never actually get it to resize and then watermark.

 

I ended up using another piece of code that i found and and it seems to work great for resizing after the image is watermarked but not before. I keep trying to resize it before the watermark and nothing works for me.

 

 

Anyone know what i can do to achieve this? or get the 2 scripts in the links above to flow together?

 

Here is my working code for watermarking then resizing.

 

 


  //--------------------------------
  // CREATE WATERMARK FUNCTION
  //--------------------------------

  define( 'WATERMARK_OVERLAY_IMAGE', 'http://localhost/d.png' );
  define( 'WATERMARK_OVERLAY_OPACITY', 70 );
  define( 'WATERMARK_OUTPUT_QUALITY', 90 );

  function create_watermark( $source_file_path, $output_file_path )
  {
    list( $source_width, $source_height, $source_type ) = getimagesize( $source_file_path );

    if ( $source_type === NULL )
    {
      return false;
    }

    switch ( $source_type )
    {
      case IMAGETYPE_GIF:
        $source_gd_image = imagecreatefromgif( $source_file_path );
        break;
      case IMAGETYPE_JPEG:
        $source_gd_image = imagecreatefromjpeg( $source_file_path );
        break;
      case IMAGETYPE_PNG:
        $source_gd_image = imagecreatefrompng( $source_file_path );
        break;
      default:
        return false;
    }

    $overlay_gd_image = imagecreatefrompng( WATERMARK_OVERLAY_IMAGE );
    $overlay_width = imagesx( $overlay_gd_image );
    $overlay_height = imagesy( $overlay_gd_image );


imagecopymerge(
$source_gd_image,
$overlay_gd_image,
floor(($source_width - $overlay_width) / 2),
floor(($source_height - $overlay_height) / 2),
0,
0,
$overlay_width,
$overlay_height,
WATERMARK_OVERLAY_OPACITY
); 
/*  Right corner
      imagecopymerge(
      $source_gd_image,
      $overlay_gd_image,
      $source_width - $overlay_width,
      $source_height - $overlay_height,
      0,
      0,
      $overlay_width,
      $overlay_height,
      WATERMARK_OVERLAY_OPACITY
    ); */

    imagejpeg( $source_gd_image, $output_file_path, WATERMARK_OUTPUT_QUALITY );

    imagedestroy( $source_gd_image );
    imagedestroy( $overlay_gd_image );
  }









//--------------------------------
  // FILE PROCESSING FUNCTION
  //--------------------------------

  define( 'UPLOADED_IMAGE_DESTINATION', '/xampp/htdocs/admin/original/' );
  define( 'PROCESSED_IMAGE_DESTINATION', '/xampp/htdocs/images/' );
    define( 'THUMBNAIL_IMAGE_DESTINATION', '/xampp/htdocs/images/' );

  function process_image_upload( $Field )
  {
    $temp_file_path = $_FILES[ $Field ][ 'tmp_name' ];
    $temp_file_name = $_FILES[ $Field ][ 'name' ];

    list( , , $temp_type ) = getimagesize( $temp_file_path );

    if ( $temp_type === NULL )
    {
      return false;
    }

    switch ( $temp_type )
    {
      case IMAGETYPE_GIF:
        break;
      case IMAGETYPE_JPEG:
        break;
      case IMAGETYPE_PNG:
        break;
      default:
        return false;
    }
$rand=time();
    $dash = "-";
    $uploaded_file_path = UPLOADED_IMAGE_DESTINATION . $rand . $dash . $temp_file_name;
    $processed_file_path = PROCESSED_IMAGE_DESTINATION . $rand . $dash . preg_replace( '/\\.[^\\.]+$/', '.jpg', $temp_file_name );



  //  $uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;

    move_uploaded_file( $temp_file_path, $uploaded_file_path );

    $result = create_watermark( $uploaded_file_path, $processed_file_path );






//This is where its does the resize..

  include 'test.php';

$image = new Resize_Image;

$image->new_width = 350;
$image->new_height = 350;

$image->image_to_resize = "$processed_file_path"; // Full Path to the file

$image->ratio = true; // Keep Aspect Ratio?

// Name of the new image (optional) - If it's not set a new will be added automatically

$image->new_image_name = 'sunset_wallpaper_thumbnail';

//Path where the new image should be saved. If it's not set the script will output the image without saving it 

$image->save_folder = '/xampp/htdocs/images/';

$process = $image->resize();

if($process['result'] && $image->save_folder)
{
echo 'The new image ('.$process['new_file_path'].') has been saved.';
}

//End of resize


    if ( $result === false )
    {
      return false;
  
    }
    else
    {
      return array( $uploaded_file_path, $processed_file_path );
    }
    
  }

  //--------------------------------
  // END OF FUNCTIONS
  //--------------------------------

  $result = process_image_upload( 'photo' );
  


  if ( $result === false )
  {

    echo '<br>An error occurred during file processing.';
  }
  else
  {
echo "<h1>Submission Successful</h1>";
echo "You will now be redirected back to the last page you visited.  Thanks!";
//echo "<meta http-equiv='refresh' content='2;URL=/admin/'>";
  }


    
  

Link to comment
Share on other sites

Actually I guess watermarking first would not be a big deal as long as I stretched the watermark to fit the image. I have a png with text that I want to stretch across the whole bottom of the image. Does anyone know how to do this? I guess I would need the watermark to stetch to fit the source image on the bottom and then resize it and it will look ok.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.