Related
I am trying to create a photo frame where there will be a light red shade and a text aligned center. I am taking the actual size of the images and then trying to create the new image programmatically with the frame. Below is a sample that I did so far:
In the image, there is a white border that I've used for demonstration purpose. I want the light red shade should not go over the white border and the text should be exactly in the middle for every image. As I am working with the actual image size, the shade and text does not remain in the specific position. In some images, the light red shade and text does not show up. I did the following code to make it work but seem like missing something:
$size = getimagesize($file_tmp); //Gets the image file size
$width = $size[0];
$height = $size[1];
$images_orig = imagecreatefromjpeg($file_tmp); //Creates jpeg image from tmp file location
$photoX = imagesx($images_orig);
$photoY = imagesy($images_orig);
$images_fin = imageCreateTrueColor($width, $height);
$color = imagecolorallocate($images_fin, 255, 255, 255);
$lightRed = imagecolorallocatealpha($images_fin, 255, 0, 0, 100);
$black = imagecolorallocate($images_fin, 0, 0, 0);
$text = 'I am from China';
$font = 'MyriadPro-Light.ttf';
$positions_redline = ($height / 4)*3;
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width, $height, $photoX, $photoY);
Imagefilledrectangle($images_fin, 0, $positions_redline, $width, $height, $lightRed);
$font_size = 10;
/*Starts - This is what I tried to fit the text into the image specifically in the center*/
$bbox = imagettfbbox($font_size, 0, $font, $text);
$tbwidth = $bbox[2];
$factor = round(($width / $tbwidth), 0, PHP_ROUND_HALF_DOWN);
$newFontSize = $font_size * $factor;
/*Finishes - This is what I tried to fit the text into the image specifically in the center*/
print_r($bbox[2]);
print_r("<br/>".$factor);
// Get your Text Width and Height
$text_width = $bbox[2] - $bbox[6];
$text_height = $bbox[3] - $bbox[7];
// Calculate coordinates of the text
$x = ($photoX / 2) - ($text_width / 2);
$y = (($height - $positions_redline) / 2) - ($text_height / 2);
Imagettftext($images_fin, $newFontSize, 0, $x, $y + $positions_redline, $color, $font , $text); //Trying to write text and align it center here
$new_images = 'testImageResult.jpg';
ImageJPEG($images_fin,"Images/".$new_images);
Try this source
$file_tmp = '/var/www/html/testGuzzle/testImage.jpg';
$size = getimagesize($file_tmp); //Gets the image file size
$width = $size[0];
$height = $size[1];
$images_orig = imagecreatefromjpeg($file_tmp); //Creates jpeg image from tmp file location
$photoX = imagesx($images_orig);
$photoY = imagesy($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
$color = imagecolorallocate($images_fin, 255, 255, 255);
$lightRed = imagecolorallocatealpha($images_fin, 255, 0, 0, 100);
$black = imagecolorallocate($images_fin, 0, 0, 0);
$text = 'Im from VietNam';
$font = '/var/www/html/testGuzzle/OpenSans-SemiboldItalic.ttf';
$positions_redline = ($height/4)*3;
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width, $height, $photoX, $photoY);
Imagefilledrectangle($images_fin, 0, $positions_redline, $width, $height, $lightRed);
$font_size = 20;
$bbox = imagettfbbox($font_size, 0, $font, $text);
print_r($bbox);
// Get your Text Width and Height
$text_width = $bbox[2]-$bbox[0];
$text_height = $bbox[7]-$bbox[1];
// Calculate coordinates of the text
$x = ($photoX/2) - ($text_width/2);
$y = ($height-$positions_redline)/2) - ($text_height/2);
Imagettftext($images_fin, 40, 0, $x, $y+$positions_redline, $color, $font , $text); //Trying to write text and align it center here
$new_images = 'testImageResult.jpg';
ImageJPEG($images_fin,"/var/www/html/testGuzzle/".$new_images);
I have one problem with PHP create image. What is wrong with my following code?
<?php
$filename = 'play';
$img = "http://www.slcentral.com/ipod-mp3-player/5.JPG";
$image = imagecreatefromjpeg($img);
$cleft = 0;
$ctop = 45;
$canvas = imagecreatetruecolor(480, 270);
imagecopy($canvas, $image, 0, 0, $cleft, $ctop, 480, 360);
$image = $canvas;
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
// ADD THE PLAY ICON
$play_icon = "f-play.png";
$logoImage = imagecreatefrompng($play_icon);
imagealphablending($logoImage, TRUE);
$logoWidth = imagesx($logoImage);
$logoHeight = imagesy($logoImage);
// CENTER PLAY ICON
$left = round($imageWidth / 2) - round($logoWidth / 2);
$top = round($imageHeight / 2) - round($logoHeight / 2);
// CONVERT TO PNG SO WE CAN GET THAT PLAY BUTTON ON THERE
imagecopy( $image, $logoImage, $left, $top, 0, 0, $logoWidth, $logoHeight);
imagepng( $image, $filename .".png", 9);
// MASHUP FINAL IMAGE AS A JPEG
$input = imagecreatefrompng($filename .".png");
$output = imagecreatetruecolor($imageWidth, $imageHeight);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $imageWidth, $imageHeight, $white);
imagecopy($output, $input, 0, 0, 0, 0, $imageWidth, $imageHeight);
// OUTPUT TO 'i' FOLDER
header("Content-type: image/jpeg");
imagejpeg($output, $filename . ".jpg", 95);
// UNLINK PNG VERSION
#unlink($filename .".png");
die();
?>
Above code is create one image from giving url and add play icon to it. And I want to access to http://coolrss.com/create-img.php it will show image with play icon.
Please help!
Make sure the path has the right permission or you will get:
PHP Warning: imagepng(): Unable to open 'play.png' for writing: Permission denied in /var/www/coolrss/public_html/create-img.php on line 32
Make sure you have installed GD.
Start your code with this header (after the php tag (<?php))
header("Content-Type: image/jpeg");
At the end (last line) add
imagejpeg($output, NULL, 100);
Your code looks like this now:
<?php
header("Content-Type: image/jpeg");
$filename = 'play';
$img = "http://www.slcentral.com/ipod-mp3-player/5.JPG";
$image = imagecreatefromjpeg($img);
$cleft = 0;
$ctop = 45;
$canvas = imagecreatetruecolor(480, 270);
imagecopy($canvas, $image, 0, 0, $cleft, $ctop, 480, 360);
$image = $canvas;
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
// ADD THE PLAY ICON
$play_icon = "f-play.png";
$logoImage = imagecreatefrompng($play_icon);
imagealphablending($logoImage, TRUE);
$logoWidth = imagesx($logoImage);
$logoHeight = imagesy($logoImage);
// CENTER PLAY ICON
$left = round($imageWidth / 2) - round($logoWidth / 2);
$top = round($imageHeight / 2) - round($logoHeight / 2);
// CONVERT TO PNG SO WE CAN GET THAT PLAY BUTTON ON THERE
imagecopy( $image, $logoImage, $left, $top, 0, 0, $logoWidth, $logoHeight);
imagepng( $image, $filename .".png", 9);
// MASHUP FINAL IMAGE AS A JPEG
$input = imagecreatefrompng($filename .".png");
$output = imagecreatetruecolor($imageWidth, $imageHeight);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $imageWidth, $imageHeight, $white);
imagecopy($output, $input, 0, 0, 0, 0, $imageWidth, $imageHeight);
imagejpeg($output, NULL, 100);
Screenshot - Works for me
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
?>
i want to align the text generated on the image to the center of the image. for the moment, i dont know if it is possible to align it. below is the code.
$im = #imagecreatefromjpeg('poloroid.jpg');
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
//imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
//$text = 'John...';
$fbid = $_POST["id"];
$text = $_POST["want"];
$fb_email =$_POST["email"];
$fb_name=$_POST["name"];
$uploads_dir = 'uploaded_files/';
// Replace path by your own font path
$font = 'verdana.ttf';
//image file name
//$name ="$fbid.png";
$name = $uploads_dir.$fbid.".png"; //this saves the image inside uploaded_files folder
// Add some shadow to the text
imagettftext($im, 20, 0, 25, 126, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 25, 125, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
//imagepng($im);
imagepng($im,$name,9);
imagedestroy($im);
thanks for the help guys.
$im = #imagecreatefromjpeg('poloroid.jpg');
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
//imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
//$text = 'John...';
$fbid = $_POST["id"];
$text = $_POST["want"];
$fb_email =$_POST["email"];
$fb_name=$_POST["name"];
$uploads_dir = 'uploaded_files/';
// Replace path by your own font path
$font = 'verdana.ttf';
$font_size = 20;
$angle = 45;
//image file name
//$name ="$fbid.png";
$name = $uploads_dir.$fbid.".png"; //this saves the image inside uploaded_files folder
// Get image Width and Height
$image_width = imagesx($im);
$image_height = imagesy($im);
// Get Bounding Box Size
$text_box = imagettfbbox($font_size,$angle,$font,$text);
// Get your Text Width and Height
$text_width = $text_box[2]-$text_box[0];
$text_height = $text_box[7]-$text_box[1];
// Calculate coordinates of the text
$x = ($image_width/2) - ($text_width/2);
$y = ($image_height/2) - ($text_height/2);
// Add some shadow to the text
imagettftext($im, $font_size, 0, $x, $y+1, $grey, $font, $text);
// Add the text
imagettftext($im, $font_size, 0, $x, $y, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
//imagepng($im);
imagepng($im,$name,9);
imagedestroy($im);
You can use stil/gd-text class. Disclaimer: I am the author.
<?php
use GDText\Box;
use GDText\Color;
$im = #imagecreatefromjpeg('poloroid.jpg');
$textbox = new Box($im);
$textbox->setFontSize(20);
$textbox->setFontFace('verdana.ttf');
$textbox->setFontColor(new Color(0, 0, 0)); // black
$textbox->setTextShadow(
new Color(0, 0, 0, 80), // black color, but 60% transparent
0,
-1 // shadow shifted 1px to top
);
$textbox->setBox(
0, // distance from left edge
0, // distance from top edge
imagesx($im), // textbox width, equal to image width
imagesy($im) // textbox height, equal to image height
);
// now we have to align the text horizontally and vertically inside the textbox
// the texbox covers whole image, so text will be centered relatively to it
$textbox->setTextAlign('center', 'center');
// it accepts multiline text
$textbox->draw($text);
$uploads_dir = 'uploaded_files/';
//image file name
//$name ="$fbid.png";
$name = $uploads_dir.$fbid.".png"; //this saves the image inside uploaded_files folder
imagepng($im, $name, 9);
imagedestroy($im);
Demonstration:
I've updated your code a little:
function ImageTTFCenter($image, $text, $font, $size, $angle = 45)
{
$xi = imagesx($image);
$yi = imagesy($image);
$box = imagettfbbox($size, $angle, $font, $text);
$xr = abs(max($box[2], $box[4]));
$yr = abs(max($box[5], $box[7]));
$x = intval(($xi - $xr) / 2);
$y = intval(($yi + $yr) / 2);
return array($x, $y);
}
$im = #imagecreatefromjpeg('poloroid.jpg');
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
//imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
//$text = 'John...';
$fbid = $_POST["id"];
$text = $_POST["want"];
$fb_email =$_POST["email"];
$fb_name=$_POST["name"];
$uploads_dir = 'uploaded_files/';
// Replace path by your own font path
$font = 'verdana.ttf';
//image file name
//$name ="$fbid.png";
$name = $uploads_dir.$fbid.".png"; //this saves the image inside uploaded_files folder
list($x, $y) = ImageTTFCenter($im, $text, $font, 20)
// Add some shadow to the4 text
imagettftext($im, 20, 0, $x, $y+1, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, $x, $y, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
//imagepng($im);
imagepng($im,$name,9);
imagedestroy($im);
The ImageTTFCenter function will find the center coordinates of you image which you will tell imagettftext
foreach ($user as $key=>$value){
$bb = imagettfbbox($value['font-size'],0,$fontname,$value['name']);
$WW = abs($bb[2]-$bb[0]);
$XX = ($value['XPos']+$WW);
$HH = abs($bb[5]-$bb[3]);
$HH +=1;
$HHH += $HH;
imagettftext($im, $value['font-size'], 0, $value['XPos'], $value['YPos'], $color[$value['color']], $fontname, $value['name']);
$HHH += 1;
$WIDE = abs($bb[2]-$bb[0]);
$endpoint=$value['XPos']+$WIDE;
$bb2 = imagettfbbox($value['font-size'],0,$fontname,$value['name']);
$WW2 = abs($bb2[2]-$bb2[0]);
$x2pos= $endpoint-$WW2;
imagettftext($im, $value['font-size'], 0, $x2pos, $value['YPos'], $color[$value['color']], $fontname, $value['name']);
}