Jump to content


Photo

Change Font Size In Php Image Watermark Script Help Needed

cant get it to take new size

  • Please log in to reply
1 reply to this topic

#1 kahodges

kahodges

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts
  • LocationFlorida

Posted 21 November 2012 - 09:01 PM

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;
}
?>


#2 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 21 November 2012 - 09:25 PM

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().


Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com