Jump to content

Change Font Size In Php Image Watermark Script Help Needed


kahodges

Recommended Posts

This script adds a text watermark to an image. No matter what number I put into this script under $fontsize = ? for changing the font size, it does not change when viewing the image. I've tried adding px and em to the number to see if that would help, but it made the images not show up altogether. Can anyone please help me? Here is the code:

 

<?php
function watermark_text($src_image,$watermarkstring,$watermark_location,$watermark_margin=0)
{
$file = $src_image;
$size = getimagesize($file);
$w = $size[0];
$h = $size[1];
$fontsize = 30;
if(stristr($file,".jpg") || stristr($file,".jpeg"))
{
$image=@imagecreatefromjpeg($file);
}
else if(stristr($file,".png"))
{
$image = @imagecreatefrompng($file);
}
else
{
exit("Sorry, only *.jpg/*jpeg and *.png images are supported");
}
$color = imagecolorallocate($image, 200, 200, 200);
$xi = 0;
$yi = 0;
$strlen_watermark = strlen($watermarkstring);
switch($watermark_location)
{
case "topleft":
$x = $watermark_margin;
$y = $watermark_margin;
break;
case "topright":
$x = $size[0] - $fontsize * strlen($watermarkstring);
$y = $watermark_margin;
break;
case "center":
$x = $size[0]/2 - $fontsize * strlen($watermarkstring)/2;
$y = $size[1]/2 - $fontsize ;
break;
case "bottomleft":
$x = $watermark_margin;
$y = $size[1] - $fontsize - $watermark_margin;
break;
case "bottomright":
$x = $size[0] - $fontsize * strlen($watermarkstring);
$y = $size[1] - $fontsize - $watermark_margin;
break;
default:
$x = 0;
$y = 0;
break;
}
imagestring($image, $fontsize, $x, $y, "$watermarkstring", $color);
if(stristr($file,".jpg") || stristr($file,".jpeg"))
{
header("Content-type: image/jpeg");
imagejpeg($image,NULL,100);
}
else if(stristr($file,".png"))
{
header("Content-type: image/png");
imagepng($image);
}
else
{
exit("Sorry, only *.jpg/*jpeg and *.png images supported");
}
ImageDestroy($image);
}//function
//////////////////////////////////////////////////////////////////////////////////
function watermark_image($image_path_src,$image_path_watermark,$watermark_location,$watermark_margin=0)
{
if(stristr($image_path_src,".jpg") || stristr($image_path_src,".jpeg"))
{
$image=@imagecreatefromjpeg($image_path_src);
}
else if(stristr($image_path_src,".png"))
{
$image = @imagecreatefrompng($image_path_src);
}
else
{
exit("Sorry, only *.jpg/*jpeg and *.png images are supported");
}
$size = getimagesize($image_path_src);
$w = $size[0];
$h = $size[1];
$src_x = 0;
$src_y = 0;
if(preg_match("#\.png$#i",$image_path_watermark))
$watermark_image = imagecreatefrompng($image_path_watermark);
else if(preg_match("#\.gif$#i",$image_path_watermark))
$watermark_image = imagecreatefromgif($image_path_watermark);
else if(preg_match("#\.jpe*g$#i",$image_path_watermark))
$watermark_image = imagecreatefromjpeg($image_path_watermark);
else
exit("Sorry, only *.gif, *.jpg and *.png watermarks are supported");

$size_watermark = getimagesize($image_path_watermark);
$dst_w = $src_w = $size_watermark[0];
$dst_h = $src_h = $size_watermark[1];
switch($watermark_location)
{
case "topleft":
$x = $watermark_margin;
$y = $watermark_margin;
break;
case "topright":
$x = $size[0] - $size_watermark[0] - $config["watermark_margin"];
$y = $watermark_margin;
break;
case "center":
$x = $size[0]/2 - $size_watermark[0]/2;
$y = $size[1]/2 - $size_watermark[1]/2;
break;
case "bottomleft":
$x = $watermark_margin;
$y = $size[1] - $size_watermark[1] - $watermark_margin;
break;
case "bottomright":
$x = $size[0] - $size_watermark[0] - $watermark_margin;
$y = $size[1] - $size_watermark[1] - $watermark_margin;
break;
default:
$x = 0;
$y = 0;
break;
}
$dst_x = $x;
$dst_y = $y;
imagecopymerge($image,$watermark_image,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h,25);
if(stristr($image_path_src,".jpg") || stristr($image_path_src,".jpeg"))
{
header("Content-type: image/jpeg");
imagejpeg($image,NULL,100);
}
else if(stristr($image_path_src,".png"))
{
header("Content-type: image/png");
imagepng($image);
}
else
{
exit("Sorry, only *.jpg/*jpeg and *.png images supported");
}
ImageDestroy($image);
}
/////////////////////////////////////////////
include(dirname(__FILE__)."/../common.ini.php");
$item = $catalog->items->get("WHERE id=".(int)$_GET["id"]);
switch($config["watermark_type"])
{
case "text":watermark_text("../rwx/".$item["file"],$config["watermark_text"],$config["watermark_location"],$config["watermark_margin"]);break;
case "image":watermark_image("../rwx/".$item["file"],"../rwx/".$config["watermark_image"],$config["watermark_location"],$config["watermark_margin"]);break;
}
?>

Link to comment
Share on other sites

The second parameter to the imagestring function isn't a font size, it picks the font number. Valid numbers are 1-5 (numbers outsize this range pick the closest permissible number.) For your use of 30, that would result in the function using built-in font number 5 -

 

Can be 1, 2, 3, 4, 5 for built-in fonts in latin2 encoding (where higher numbers corresponding to larger fonts) or any of your own font identifiers registered with imageloadfont().
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.