Please check my code -
<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$font = 'dum13d100.ttf';
$font_size = 10;
imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
if ($DestinationFile<>'') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
}
$SourceFile = 'myimage.jpg';
$DestinationFile = 'images/myimage2.jpg';
$WaterMarkText = 'CHINMAY';
watermarkImage ($SourceFile,$WaterMarkText,$DestinationFile);
?>
Original Source code -
http://www.learnphp.in/index.php?page=forum&op=viewThread&id=687
Above code is showing -
I want to -
The above image height is 280px
I have increased my text height 250($y angel) from top check below code-
imagettftext($image_p, $font_size, 0, 10, 250, $black, $font, $WaterMarkText);
It works fine but my question is if someone upload 800px or more size image the text will not showing in footer it comes 250px from top.
I need to my watermark image will show padding-button:20px from left of the image.
Please anybody help me how to make height dynamic?
Change your line
imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
to
imagettftext($image_p, $font_size, 0, 10, $height - 20, $black, $font, $WaterMarkText);
You can calculate the height of the image using getimagesize() (key index 1 being the height) then pad from the bottom by taking 20 away from the height.
$height = getimagesize($image)[1];
imagettftext($image_p, $font_size, 0, 10, $height - 20, $black, $font, $WaterMarkText);
Related
So I have a 3x3 pixel image using imagecreate. I want to scale up the image with imagescale while maintaining the look of a 3x3 grid of "pixels". However, the pixels on the right and bottom edge are not the same size.
Here is my code and output image:
<?php
$image = imagecreate(3, 3);
imagecolorallocate($image, 0, 0, 255);
$red = imagecolorallocate($image, 255, 0, 0);
imagesetpixel($image, 0, 0, $red);
imagesetpixel($image, 1, 1, $red);
imagesetpixel($image, 2, 2, $red);
imagepng(imagescale($image, 200, 200, IMG_NEAREST_NEIGHBOUR));
header("Content-Type: image/png");
This is my output:
Notice how the bottom-right pixel is cut off. I kept playing with the numbers for the new dimensions and arrived at 256x256 at which point the pixels are all the same size.
This is the output after using 256x256:
My question is: How can I derive the dimensions to use for the resized image with the effect I described?
Bonus question: Is an alternative method which will allow me to resize to an arbitrary size and keep the pixels approximately the same size?
I would use imagecopyresampled to achieve this.
http://php.net/manual/en/function.imagecopyresampled.php
<?php
$width = 3;
$height = 3;
$image = imagecreate($width, $height);
imagecolorallocate($image, 0, 0, 255);
$red = imagecolorallocate($image, 255, 0, 0);
imagesetpixel($image, 0, 0, $red);
imagesetpixel($image, 1, 1, $red);
imagesetpixel($image, 2, 2, $red);
$new_width = 200;
$new_height = 200;
$dst = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($dst, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($dst);
header("Content-Type: image/png");
I want to save my pngs, but my code does not allow me to create new pngs or overwrite the existing ones. Ideally every time the page is loaded the image would be saved.
<?php
$width = 640;
$height = 480;
$font = 23;
$string = "This is my text";
$im = #imagecreatetruecolor($width, $height);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 0, 0, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "./DroidSerif-Bold.ttf", $string);
$im = imagecreatefrompng("test.png");
imagedestroy($im);
?>
imagecreateFROMpng as it names indicates creates an image object by reading a .PNG file. In order to save the image as a PNG, you must use the imagepng function:
...
imagettftext($im, $font, 0, 0, $font - 3, $lime, "./DroidSerif-Bold.ttf", $string);
imagepng($im, "test.png");
imagedestroy($im);
i want to generate online certificates using GD in php, certificate image format is ready with me. just i want to put candidate name and other details on image as a text in particular area. i have image file in jpg, gif and png format
Here is a Complete Example with text and certificate image
<?php
function LoadGif($imgname)
{
/* Attempt to open */
$im = #imagecreatefromgif($imgname);
// Allocate A Color For The Text
$black = imagecolorallocate($im, 0, 0, 0);
// Set Path to Font File
$font_path = 'ARIALNB.TTF';
// Set Text to Be Printed On Image
$candidate_name = "Chirag jagani";
$speed = "8829";
$percentage = "98%";
$mintues ="4:00";
$date="The 1st Day of July in the Year 2015";
// Print Text On Image
//imagettftext(image, size, varitcal means 90, X cordinates, Y coordinates, $white, $font_path, $speed);
//candidate name
imagettftext($im, 54, 90, 199, 713, $black, $font_path, $candidate_name);
//speed
imagettftext($im, 13, 90, 271, 620, $black, $font_path, $speed);
//percentage
imagettftext($im, 14, 90, 310, 605, $black, $font_path, $percentage);
//mintues
imagettftext($im, 13, 90, 331, 350, $black, $font_path, $mintues);
//date
imagettftext($im, 22, 90, 382, 610, $black, $font_path, $date);
/* See if it failed */
if(!$im)
{
/* Create a blank image */
$im = imagecreatetruecolor (150, 30);
$bgc = imagecolorallocate ($im, 255, 255, 255);
$tc = imagecolorallocate ($im, 0, 0, 0);
imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);
/* Output an error message */
imagestring ($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
return $im;
}
header('Content-Type: image/gif');
$img = LoadGif('main.gif');
imagegif($img);
imagedestroy($img);
?>
let me know if you have any query.
all the best :)
How to create a watermark on an existing image..
//-------------------------------------------------------------------------
$wWidth = 280; // set watermark image width
$wHeight = 50; // set watermark image height
// Create the watermark image
$watermark = imagecreate( $wWidth, $wHeight );
$black = imagecolorallocate( $watermark, 0, 0, 0 ); // define a colour to use
$white = imagecolorallocate( $watermark, 255, 255, 255 ); // define a colour to use
// Create a rectangle and fill it white
imagefilledrectangle( $watermark, 0, 0, $wWidth, $wHeight, $white );
// Make white transparent
#imagecolortransparent ( $watermark, $white );
$font = 'fonts/arial.ttf'; // store path to the font file
$wText = "(c) Chris Maggs ".date('Y'); // set watermark text
$wSize = "20"; // set the watermark font size
// Calculate the size of $wText (requires php/gd with ttf support)
$box = imagettfbbox( $wSize, 0, $font, $wText );
$x = ( $wWidth - ($box[2] - $box[0]) ) / 2;
$y = ( $wHeight - ($box[1] - $box[7]) ) / 2;
$y -= $box[7];
// Add $wText to the image
imagettftext( $watermark, $wSize, 0, $x, $y, $black, $font, $wText );
// Save the text image as temp.png
imagepng( $watermark, "uploads/temp.png" );
// Cleanup the image memory usage
imagedestroy( $watermark );
//-------------------------------------------------------------------------
// Set the path to the image to watermark
$input_image = "resources/tenby.jpg";
// Read in the text watermark image
$watermarkImage = imagecreatefrompng( "uploads/temp.png" );
$watermark_width = imagesx( $watermarkImage ); // width of image resource
$watermark_height = imagesy( $watermarkImage ); // height of image resource
// Create a new image resource
$image = imagecreatefromjpeg( $input_image );
// Find the size of the original image and read it into an array
$size = getimagesize( $input_image );
// Set the positions of the watermark on the image
$dest_x = $size[0] - $watermark_width;
$dest_y = $size[1] - $watermark_height;
// Merge 2 image resources..
imagecopymerge($image, $watermarkImage, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 50);
// OPTION 1 : save the watermarked image as watermarked.jpg
imagejpeg( $image, "uploads/watermarked.jpg" );
// OPTION 2 : output the watermarked image
header("Content-type: image/jpeg");
imagejpeg( $image );
// delete the text watermark image
unlink( "uploads/temp.png");
// Cleanup the image memory usage
imagedestroy( $image );
I'm trying to create an image with a watermark text in it and I want it to be central on the image. The text I want to watermark can be anywhere from 5 characters to 15 characters, therefore I cannot put a final size for the text to fit every image.
This is the code I use to create the watermarked image
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$font = '../fonts/proxima-nova-light.otf';
$font_size = 100;
imagettftext($image_p, $font_size, 0, 303, 190, $black, $font, $WaterMarkText);
if ($DestinationFile<>'') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
};
Which does an excellent job on some texts but when I try it on longer texts it looks bad.
I want to - somehow - calculate the optimal size of text and choose size, x and y from there.
Any ideas?
After doing some research with help from #Sammitch I was able to figure it out. Here is the working code:
<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
$font = 'fonts/your-font-here.ttf';
$font_size = 40;
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$bbox = imagettfbbox($font_size, 0, $font, $WaterMarkText);
$x = $bbox[0] + (imagesx($image) / 2) - ($bbox[4] / 2) + 10;
$y = $bbox[1] + (imagesy($image) / 2) - ($bbox[5] / 2) - 5;
imagettftext($image_p, $font_size, 0, $x, $y, $black, $font, $WaterMarkText);
if ($DestinationFile<>'') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
};
?>
Calculate the position of the watermark in the output image (the
watermark shall be placed in the Center of the Image)
$watermark_pos_x = (imagesx($image)/2) - (imagesx($watermark)/2) - 15;
$watermark_pos_y = (imagesy($image)/2) - (imagesy($watermark)/2) - 10;
Use this value on function.
I have an image and I'm trying to watermark the image using text.
I have done with code part, but no image see been viewed with watermark text.
Here is the code below:
$imagetobewatermark="images/muggu.png";
list($width,$height)=getimagesize($imagetobewatermark);
$imagetobewatermark=imagecreatetruecolor($width,$height);
$mark=imagecreatefrompng($imagetobewatermark);
imagecopy($imagetobewatermark,$mark,0,0,0,0,$width,$height);
$wartermarktext="Muggu";
$font="../font/century gothic.ttf";
$fontsize="15";
$white = imagecolorallocate($imagetobewatermark, 255, 255, 255);
imagettftext($imagetobewatermark, $fontsize, 0, 20, 10, $white, $font, $watermarktext);
header("Content-type:image/png");
imagepng($imagetobewatermark);
imagedestroy($imagetobewatermark);
Tell me If I'm wrong.
Thank you.
One problem I can see straight away is that the $imagetobewatermark variable starts off as a string, then becomes a new blank image object (not an existing image), and when you subsequently create the mark image object, it's not going to work because $imagetobewatermark is no longer a string.
Try:
$imagetobewatermark=imagecreatefrompng("images/muggu.png");
$watermarktext="Muggu";
$font="../font/century gothic.ttf";
$fontsize="15";
$white = imagecolorallocate($imagetobewatermark, 255, 255, 255);
imagettftext($imagetobewatermark, $fontsize, 0, 20, 10, $white, $font, $watermarktext);
header("Content-type:image/png");
imagepng($imagetobewatermark);
imagedestroy($imagetobewatermark);
EDIT:
I failed to notice a typo in your text variable $wartermarktext, which should be $watermarktext.
Correct this and it should work.
You had some spellings wrong and did not use the ressource. Here corrected:
$imagetobewatermark = "muggu.png";
list ($width, $height) = getimagesize($imagetobewatermark);
$res = imagecreatetruecolor($width, $height);
$mark = imagecreatefrompng($imagetobewatermark);
//make sure here to use the ressource, not the filepath
imagecopy($res, $mark, 0, 0, 0, 0, $width, $height);
$watermarktext = "Muggu";
//$font = "../font/century gothic.ttf";
//I copied it to my local test folder
$font = "GOTHIC.TTF";
$fontsize = "15";
//make sure here to use the ressource, not the filepath
$white = imagecolorallocate($res, 255, 255, 255);
//make sure here to use the ressource, not the filepath
imagettftext($res, $fontsize, 0, 20, 10, $white, $font, $watermarktext);
header("Content-type:image/png");
//make sure here to use the ressource, not the filepath
imagepng($res);
//make sure here to use the ressource, not the filepath
imagedestroy($res);
you have to give Public/Absolute Path for Font arial.ttf.
Solution is tested in Laravel 5.8.
you can use this solution. here [ $SourceFile , $DestinationFile ] are Absolute Path of local directory. $imgUrl is HTTP based URL. Watermark will be placed at the top of the image.
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile,$imgUrl) {
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 255, 255, 255);
$font = public_path('fonts/arial.ttf');
$font_size = 8;
imagettftext($image_p, $font_size, 0, 10, 20, $black,$font , $WaterMarkText);
if ($DestinationFile <> '') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
return $imgUrl;
}
Try this.
$imagetobewatermark = "images/muggu.png";
$watermarktext = "Muggu";
list($width, $height) = getimagesize($imagetobewatermark);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefrompng($imagetobewatermark);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$font = "../font/century gothic.ttf";
$font_size = 15;
$white = imagecolorallocate($image_p, 255, 255, 255);
imagettftext($image_p, $font_size, 0, 20, 20, $white, $font, $watermarktext);
header("Content-Type: image/png");
imagepng($image_p, null, 0);
imagedestroy($image);
imagedestroy($image_p);
Try this code, It helps you to provide the watermark for the images.
<?php
header('Content-type: image/jpeg'); //SET THE FORMATE OF THE IMAGE
$jpg_image = imagecreatefromjpeg('images.jpg'); //GIVE LINK FOR SPECIFIED IMAGE
$color = imagecolorallocate($jpg_image, 500, 500, 500); //GIVE COLOR TO WATERMARK TEXT
$font_location = 'BROADW.TTF'; //WATERMARK FONT
$text = "http://www.sanwebtutorials.blogspot.in/"; //WATERMARK TEXT
$x=200; //X CO-ORDINATE
$y=800; //Y CO-ORDINATE
$size=21; //SIZE OF TEXT
imagettftext($jpg_image,$size, 0, $x, $y, $color, $font_location, $text); //PRINT TEXT ON IMAGE
imagejpeg($jpg_image); //SEND IMAGE TO BROWSER
imagedestroy($jpg_image); //DESTROY THE MEMORY
?>