/** * @param array $options * 'watermark' => waImage|string $watermark * 'opacity' => float|int 0..1 * 'align' => self::ALIGN_* const * 'font_file' => null|string If null - will be used some default font. Note: use when watermark option is text * 'font_size' => float Size of font. Note: use when watermark option is text * 'font_color' => string Hex-formatted of color (without #). Note: use when watermark option is text * 'text_orientation' => self::ORIENTATION_* const. Note: use when watermark option is text * @throws waException * @return mixed */ protected function _watermark($options) { $watermark = false; $opacity = 0.5; $align = self::ALIGN_BOTTOM_RIGHT; $font_file = null; $font_size = 12; $font_color = '888888'; $text_orientation = self::ORIENTATION_HORIZONTAL; extract($options, EXTR_IF_EXISTS); $opacity = min(max($opacity, 0), 1); imagealphablending($this->image, true); if ($watermark instanceof waImage) { $type = $watermark->type; $gd_watermark = $this->createGDImageResourse($watermark->file, $type); $width = ifset($options['width'], $watermark->width); $height = ifset($options['height'], $watermark->height); $offset = $this->calcWatermarkOffset($width, $height, $align); if ($width != $watermark->width || $height != $watermark->height) { $watermark_resized = $this->_create($width, $height); if (function_exists('imagecopyresampled')) { imagecopyresampled($watermark_resized, $gd_watermark, 0, 0, 0, 0, $width, $height, $watermark->width, $watermark->height); } else { imagecopyresized($watermark_resized, $gd_watermark, 0, 0, 0, 0, $width, $height, $watermark->width, $watermark->height); } imagedestroy($gd_watermark); $gd_watermark = $watermark_resized; } imagecopymerge_alpha($this->image, $gd_watermark, $offset[0], $offset[1], 0, 0, $width, $height, $opacity * 100); imagedestroy($gd_watermark); } else { $text = (string) $watermark; if (!$text) { return; } $margin = round($font_size / 3.6); $font_color = array('r' => '0x' . substr($font_color, 0, 2), 'g' => '0x' . substr($font_color, 2, 2), 'b' => '0x' . substr($font_color, 4, 2), 'a' => floor((1 - $opacity) * 127)); if ($align == self::ALIGN_CENTER) { $rotation = (int) ifempty($options['rotation'], 0); $text_orientation = self::ORIENTATION_HORIZONTAL; } else { if ($text_orientation == self::ORIENTATION_VERTICAL) { $rotation = 90; } else { $rotation = 0; } } if (!empty($font_file) && file_exists($font_file)) { $gd_info = gd_info(); $gd_version = preg_replace('/[^0-9\\.]/', '', $gd_info['GD Version']); if (!empty($gd_info['FreeType Support']) && version_compare($gd_version, '2.0.1', '>=')) { // Free Type $free_type = true; } else { // True Type $free_type = false; // GD1 use pixels, GD2 use points if (version_compare($gd_version, '2.0', '<')) { $font_size = 24 * $font_size / 18; // 24px = 18pt } } if ($free_type) { $metrics = imageftbbox($font_size, 0, $font_file, $text); } else { $metrics = imagettfbbox($font_size, 0, $font_file, $text); } if ($metrics) { $width = $metrics[2] - $metrics[0]; $height = $metrics[1] - $metrics[7]; if ($text_orientation == self::ORIENTATION_VERTICAL) { list($width, $height) = array($height, $width); } $offset = $this->calcWatermarkOffset($width, $height, $align, $margin); $offset = $this->watermarkOffsetFix($offset, $width, $height, $align, $text_orientation, $align == self::ALIGN_CENTER ? $rotation : 0); $color = imagecolorallocatealpha($this->image, $font_color['r'], $font_color['g'], $font_color['b'], $font_color['a']); if ($free_type) { imagefttext($this->image, $font_size, $rotation, $offset[0], $offset[1], $color, $font_file, $text); } else { imagettftext($this->image, $font_size, $rotation, $offset[0], $offset[1], $color, $font_file, $text); } } else { throw new waException(_ws("Can't read font file {$font_file}")); } } else { $font = floor(5 * $font_size / 12); if ($font < 1) { $font = 1; } else { if ($font > 5) { $font = 5; } } $width = imagefontwidth($font) * strlen($text); $height = imagefontheight($font); if ($text_orientation == self::ORIENTATION_VERTICAL) { list($width, $height) = array($height, $width); } $offset = $this->calcWatermarkOffset($width, $height, $align, $margin); if ($rotation != 0) { imagestring_rotate($this->image, $font, $rotation, $offset[0], $offset[1], $text, $font_color['r'], $font_color['g'], $font_color['b'], $font_color['a']); } else { $color = imagecolorallocatealpha($this->image, $font_color['r'], $font_color['g'], $font_color['b'], $font_color['a']); imagestring($this->image, $font, $offset[0], $offset[1], $text, $color); } } } }
/** * @param array $options * 'watermark' => waImage|string $watermark * 'opacity' => float|int 0..1 * 'align' => self::ALIGN_* const * 'font_file' => null|string If null - will be used some default font. Note: use when watermark option is text * 'font_size' => float Size of font. Note: use when watermark option is text * 'font_color' => string Hex-formatted of color (without #). Note: use when watermark option is text * 'text_orientation' => self::ORIENTATION_* const. Note: use when watermark option is text * @return mixed */ protected function _watermark($options) { // export options to php-vars foreach ($options as $name => $value) { ${$name} = $value; } $opacity = min(max($opacity, 0), 1); imagealphablending($this->image, true); if ($watermark instanceof waImage) { $width = $watermark->width; $height = $watermark->height; $offset = $this->calcWatermarkOffset($width, $height, $align); $type = $watermark->type; $watermark = $this->createGDImageResourse($watermark->file, $type); imagecopymerge_alpha($this->image, $watermark, $offset[0], $offset[1], 0, 0, $width, $height, $opacity * 100); imagedestroy($watermark); } else { $text = (string) $watermark; if (!$text) { return; } $font_color = array('r' => '0x' . substr($font_color, 0, 2), 'g' => '0x' . substr($font_color, 2, 2), 'b' => '0x' . substr($font_color, 4, 2), 'a' => floor((1 - $opacity) * 127)); if ($font_file && file_exists($font_file)) { $metrics = imagettfbbox($font_size, 0, $font_file, $text); if ($metrics) { $width = $metrics[2] - $metrics[0]; $height = $metrics[1] - $metrics[7]; if ($text_orientation == self::ORIENTATION_VERTICAL) { list($width, $height) = array($height, $width); } $offset = $this->calcWatermarkOffset($width, $height, $align); $offset = $this->watermarkOffsetFix($offset, $width, $height, $text_orientation); $color = imagecolorallocatealpha($this->image, $font_color['r'], $font_color['g'], $font_color['b'], $font_color['a']); imagettftext($this->image, $font_size, $text_orientation == self::ORIENTATION_VERTICAL ? 90 : 0, $offset[0], $offset[1], $color, $font_file, $text); } else { throw new waException(_ws("Can't read font file {$font_file}")); } } else { $font = floor(5 * $font_size / 12); if ($font < 1) { $font = 1; } else { if ($font > 5) { $font = 5; } } $width = imagefontwidth($font) * strlen($text); $height = imagefontheight($font); if ($text_orientation == self::ORIENTATION_VERTICAL) { list($width, $height) = array($height, $width); } $offset = $this->calcWatermarkOffset($width, $height, $align); if ($text_orientation == self::ORIENTATION_VERTICAL) { imagestring_rotate($this->image, $font, 90, $offset[0], $offset[1], $text, $font_color['r'], $font_color['g'], $font_color['b'], $font_color['a']); } else { $color = imagecolorallocatealpha($this->image, $font_color['r'], $font_color['g'], $font_color['b'], $font_color['a']); imagestring($this->image, $font, $offset[0], $offset[1], $text, $color); } } } }