You are here

protected function PHPVideoToolkit::_addGDWatermark in Video 7

Same name and namespace in other branches
  1. 7.2 libraries/phpvideotoolkit/phpvideotoolkit.php5.php \PHPVideoToolkit::_addGDWatermark()

* Adds watermark to any outputted images via GD instead of using vhooking. * * @access protected *

Parameters

array $files An array of image files.: * @return array

File

libraries/phpvideotoolkit/phpvideotoolkit.php5.php, line 2116

Class

PHPVideoToolkit

Code

protected function _addGDWatermark($files) {

  // 			create the watermark resource and give it alpha blending
  $info = pathinfo($this->_watermark_url);
  switch (strtolower($info['extension'])) {
    case 'jpeg':
    case 'jpg':
      $watermark = imagecreatefromjpeg($this->_watermark_url);
      break;
    case 'gif':
      $watermark = imagecreatefromgif($this->_watermark_url);
      break;
    case 'png':
      $watermark = imagecreatefrompng($this->_watermark_url);
      break;
    default:
      return false;
  }
  imagealphablending($watermark, true);
  imagesavealpha($watermark, true);

  // 			get the watermark dimensions
  $watermark_width = imagesx($watermark);
  $watermark_height = imagesy($watermark);

  // 			$image = imagecreatetruecolor($watermark_width, $watermark_height);
  // 			loop and watermark each file
  $blended_files = array();
  foreach ($files as $file => $filename) {

    // 				detect the file extension and create the resource from them appropriate function
    $info = pathinfo($file);
    $quality = $output_function = null;
    switch (strtolower($info['extension'])) {
      case 'jpeg':
      case 'jpg':
        $quality = 80;
        $output_function = 'imagejpeg';
        $image = imagecreatefromjpeg($file);
        break;
      case 'gif':
        $output_function = 'imagegif';
        $image = imagecreatefromgif($file);
        break;
      case 'png':
        $quality = 9;
        $output_function = 'imagepng';
        $image = imagecreatefrompng($file);
        break;
      default:
        continue 1;
    }

    // 				the dimensions will/should be the same for each image however still best to check
    $image_width = imagesx($image);
    $image_height = imagesy($image);

    // 				calculate where to position the watermark
    $dest_x = 0;
    $dest_y = 0;
    switch ($this->_watermark_options['position']) {
      case 'top-left':
        $dest_x = 0;
        $dest_y = 0;
        break;
      case 'top-middle':
        $dest_x = ($image_width - $watermark_width) / 2;
        $dest_y = 0;
        break;
      case 'top-right':
        $dest_x = $image_width - $watermark_width;
        $dest_y = 0;
        break;
      case 'center-left':
        $dest_x = 0;
        $dest_y = ($image_height - $watermark_height) / 2;
        break;
      case 'center-middle':
        $dest_x = ($image_width - $watermark_width) / 2;
        $dest_y = ($image_height - $watermark_height) / 2;
        break;
      case 'center-right':
        $dest_x = $image_width - $watermark_width;
        $dest_y = ($image_height - $watermark_height) / 2;
        break;
      case 'bottom-left':
        $dest_x = 0;
        $dest_y = $image_height - $watermark_height;
        break;
      case 'bottom-middle':
        $dest_x = ($image_width - $watermark_width) / 2;
        $dest_y = $image_height - $watermark_height;
        break;
      case 'bottom-right':
      default:
        $dest_x = $image_width - $watermark_width;
        $dest_y = $image_height - $watermark_height;
        break;
    }
    $dest_x += $this->_watermark_options['x-offset'];
    $dest_y += $this->_watermark_options['y-offset'];

    // 				copy the watermark to the new image
    imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);

    // 				delete the old image
    unlink($file);

    // 				save the new image in place of the old
    $output_function($image, $file, $quality);

    // 				remove the image resouce
    imagedestroy($image);
    array_push($blended_files, $file);
  }

  // 			remove the watermark resource
  imagedestroy($watermark);
  return $blended_files;
}