[eluser]MyBelovedPHP[/eluser]
I experienced troubles setting a text watermark with proper padding, so I took a dive into the `image manipulation class` code.
I found a few bugs and inefficiencies:
Bug:
conditional statement at line 1010:
Code:
if ($this->wm_vrt_alignment == 'B')
while property is set at line 1042:
Code:
$this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
Maybe the padding property is redundant, you can do the same with wm_vrt_offset and wm_hor_offset. On the other hand it's nice to set it with one property, although right alignment is difficult, because text width isn't calculated with much accuracy for TTF fonts.
Nonetheless I have rewritten the code for the function to solve the bugs.
- 15 lines less code;
- a few less conditional statements.
It's shorter and faster!
Code:
function text_watermark()
{
if ( ! ($src_img = $this->image_create_gd()))
{
return FALSE;
}
if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
{
$this->set_error('imglib_missing_font');
return FALSE;
}
// Fetch source image properties
$this->get_image_properties();
// Set RGB values for text and shadow
$this->wm_font_color = str_replace('#', '', $this->wm_font_color);
$this->wm_shadow_color = str_replace('#', '', $this->wm_shadow_color);
$R1 = hexdec(substr($this->wm_font_color, 0, 2));
$G1 = hexdec(substr($this->wm_font_color, 2, 2));
$B1 = hexdec(substr($this->wm_font_color, 4, 2));
$R2 = hexdec(substr($this->wm_shadow_color, 0, 2));
$G2 = hexdec(substr($this->wm_shadow_color, 2, 2));
$B2 = hexdec(substr($this->wm_shadow_color, 4, 2));
$txt_color = imagecolorclosest($src_img, $R1, $G1, $B1);
$drp_color = imagecolorclosest($src_img, $R2, $G2, $B2);
// Set font width and height
// These are calculated differently depending on
// whether we are using the true type font or not
if ($this->wm_use_truetype == TRUE)
{
if ($this->wm_font_size == '')
$this->wm_font_size = '17';
$fontwidth = $this->wm_font_size-($this->wm_font_size/4);
$fontheight = $this->wm_font_size;
}
else
{
$fontwidth = imagefontwidth($this->wm_font_size);
$fontheight = imagefontheight($this->wm_font_size);
}
$this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
$this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
// clear dropshadow if no use dropshadow
if ($this->wm_use_drop_shadow == FALSE)
$this->wm_shadow_distance = 0;
// Set verticle alignment
switch ($this->wm_vrt_alignment)
{
case "T" :
$y_axis = $this->wm_padding + $this->wm_vrt_offset + $fontheight;
break;
case "M":
$y_axis = floor($this->orig_height/2)+($fontheight/2);
break;
case "B":
$y_axis = $this->orig_height - $this->wm_vrt_offset - $this->wm_padding - $this->wm_shadow_distance;
break;
}
// Set horizontal alignment
switch ($this->wm_hor_alignment)
{
case "L":
$x_axis = $this->wm_padding + $this->wm_hor_offset;
break;
case "R":
$x_axis = $this->orig_width - ($fontwidth*strlen($this->wm_text))- $this->wm_shadow_distance - $this->wm_padding - $this->wm_hor_offset;
break;
case "C":
$x_axis = floor(($this->orig_width -$fontwidth*strlen($this->wm_text))/2);
break;
}
// set dropshadow
$x_shad = $x_axis + $this->wm_shadow_distance;
$y_shad = $y_axis + $this->wm_shadow_distance;
// Add the text to the source image
if ($this->wm_use_truetype)
{
if ($this->wm_use_drop_shadow)
imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
}
else
{
if ($this->wm_use_drop_shadow)
imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
}
// Output the final image
if ($this->dynamic_output == TRUE)
{
$this->image_display_gd($src_img);
}
else
{
$this->image_save_gd($src_img);
}
imagedestroy($src_img);
return TRUE;
}