I am trying to create a word cloud of a person's face. Similar to this
To achieve this I got a black & white image of a person and turned the darkest pixel to black and lightest pixel to white. And here is my result
Now I have got the area where I would like to place word clouds. Now I can't figure out how do I place words inside the face keeping margin/angle between words.
Here's the code what i have done so far
<?php
set_time_limit(0);
$src = 'person.jpeg';
$im = imagecreatefromjpeg($src);
$size = getimagesize($src);
$width = $size[0];
$height = $size[1];
$image_p = imagecreatetruecolor($width, $height);
imagecopyresampled($image_p, $im, 0, 0, 0, 0, $width, $height, $width, $height);
$white_color = imagecolorallocate($im, 255, 255, 255);
$black_color = imagecolorallocate($im, 0, 0, 0);
$font = __DIR__ . "/testfont.ttf";
$font_size = 16;
$text = "Test text";
$skip = true;
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
$rgb = imagecolorat($im, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if ($r >= 126) {
imagesetpixel($image_p, $x, $y, $white_color);
} else {
imagesetpixel($image_p, $x, $y, $black_color);
if ($x % 20 == 1) {
imagestring($image_p, 5, $x, $y, 'T', $black_color);
//imagettftext($image_p, 16, 0, $x, $y, $black_color, $font, $text);
}
}
//var_dump($r, $g, $b);
//echo "<br/>";
}
}
imagestring($image_p, 5, 0, 0, 'Hello world!', $black_color);
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
I tried using imagestring & imagettftext
if ($x % 20 == 1) {
imagestring($image_p, 5, $x, $y, 'T', $black_color);
//imagettftext($image_p, 16, 0, $x, $y, $black_color, $font, $text);
}
And got weird output. With imagettftext it takes too long to render and with imagestring this is what I got
Using one of these functions not both of them: imagesetpixel or imagestring
And if you have B/W photo, forget $black_color & $white_color or add them to this codes to customize more. And also add your custom header in the end.
list($w, $h, $type) = getimagesize('person.jpeg');
$resource = imagecreatefromstring(file_get_contents('person.jpeg'));
$img = imagecreatetruecolor($w, $h);
for($y=0; $y<$h; $y+=20)
for($x=0; $x<$w; $x+=20)
imagestring($img, 5, $x, $y, 'Hello world!', imagecolorat($resource, $x, $y));
Here's what works for me. I create a png image template in the Gimp, save it. Trickiest part for me was figuring out the font path aspect, since the font file just had to be in the one directory.
<?php
// Create Image From Existing File
$image = imagecreatefrompng('baseimg.png');
// Allocate A Color For The Text
$black = imagecolorallocate($image, 125, 125, 255);
// Set Path to Font File
$font_path = './FreeSans.ttf';
$text = "Hello World!";
// Print Text On Image
imagettftext($image, 14, 0, 15, 110, $black, $font_path, $text);
//Set the Content Type
header('Content-type: image/png');
// Send Image to Browser
imagepng($image);
// Clear Memory
imagedestroy($image);
exit;
?>
Related
Im drawing a dynamically text on a image centersized with GD library.
Whats the best way to align all lines center?
<?php
function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $fontfile, $text, $px) {
for($c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++)
for($c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++)
$bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);
return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text);
}
$image = "12.png";
$font = "./impact.ttf";
$font_size = "50";
$image_2 = imagecreatefrompng($image);
$black = imagecolorallocate($image_2, 255,255,255);
$black2 = imagecolorallocate($image_2, 0,0,0);
$image_width = imagesx($image_2);
$image_height = imagesy($image_2);
$margin = 35;
$text = "This is the Text. It is dynamically long. This text will be split using the function under this text.";
//explode text by words
$text_a = explode(' ', $text);
$text_new = '';
foreach($text_a as $word){
//Create a new text, add the word, and calculate the parameters of the text
$box = imagettfbbox($font_size, 0, $font, $text_new.' '.$word);
//if the line fits to the specified width, then add the word with a space, if not then add word with new line
if($box[2] > $image_width - $margin*2){
$text_new .= "\n".$word;
} else {
$text_new .= " ".$word;
}
}
$text_box = imagettfbbox($font_size,0,$font,$text_new);
$text_width = $text_box[2]-$text_box[0]; // lower right corner - lower left corner
$text_height = $text_box[3]-$text_box[1];
$x = ($image_width/2) - ($text_width/2);
$y = ($image_height/2) - ($text_height/2);
$font_color = imagecolorallocate($image_2, 255, 255, 255);
$font_color2 = imagecolorallocate($image_2, 0, 0, 0);
$stroke_color = imagecolorallocate($image_2, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$grey = imagecolorallocate($im, 175, 175, 175);
imagettfstroketext($image_2, $font_size ,0,$x,$y,$font_color, $stroke_color, $font, $text_new, 0);
header ("Content-type: image/png");
imagejpeg($image_2, "../img/output.png");
imagedestroy($image_2);
?>
This is what it looks right now:
This is what it should look like:
Using three times "imagettfstroketext" with adding "$y+50" would do the trick but the text is dynamic.
Any suggestions?
best regards
I would create a temporary image on one line, storing the accumulated length in an array. Then determine exactly which words fit on a line, and the offset to center each line. Then create the image.
Alternatively you can create separate images for each line, then combine them with the right offset.
I use the following code to create an EAN13 barcode. The barcode is created without any issues, but when I scan it it is not recognized.
What is the problem?
include('php-barcode.php');
$font = 'fonts/GSMT.TTF';
$fontSize = 16; // GD1 in px ; GD2 in point
$marge = 10; // between barcode and hri in pixel
$x = 80; // barcode center
$y = 80; // barcode center
$height = 50; // barcode height in 1D ; module size in 2
$width = 2; // barcode height in 1D ; not use in 2D
$angle = 0;
$code = "111110001001"; // barcode, of course ;)
$type = "ean13"; //
$im = imagecreatetruecolor(180, 180);
$black = ImageColorAllocate($im,0x00,0x00,0x00);
$white = ImageColorAllocate($im,0xff,0xff,0xff);
imagefilledrectangle($im, 0, 0, 180, 180, $white);
$data = Barcode::gd($im, $black, $x, $y, $angle, $type, array('code'=>$code), $width, $height);
if ( isset($font) ){
$box = imagettfbbox($fontSize, 0, $font, $data['hri']);
$len = $box[2] - $box[0];
Barcode::rotate(-$len / 2, ($data['height'] / 2) + $fontSize + $marge, $angle, $xt, $yt);
imagettftext($im, $fontSize, $angle, $x + $xt, $y + $yt, $black, $font, $data['hri']);
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
Here's the problem the text overlayed over the image and gradient has some weird pixilation around the text, but I can't work out why.
-->
Here's the code:
<?php
function hex2rgb($hex) {
$rgb[0] = hexdec(substr($hex, 0, 2));
$rgb[1] = hexdec(substr($hex, 2, 2));
$rgb[2] = hexdec(substr($hex, 4, 2));
return $rgb;
}
$sourceImage = imagecreatefromstring(file_get_contents("source.jpg"));
$imagePadding = 10;
$height = imagesy($sourceImage);
$width = imagesx($sourceImage);
$baseImage = imagecreatetruecolor($width, $height);
$backgroundColor = imagecolorallocate($baseImage, 0, 0, 0);
imagefill($baseImage, 0, 0, $backgroundColor);
imagecopyresampled($baseImage, $sourceImage, 0, 0, 0, 0, $width, $height, $width, $height);
//==== GRADIENT ====//
// Modified from: http://stackoverflow.com/questions/14684622/blend-transparent-gradient-with-an-image-using-php-gd-library
$gradientCanvas = imagecreatetruecolor($width, $height);
$transColor = imagecolorallocatealpha($gradientCanvas, 0, 0, 0, 127);
imagefill($gradientCanvas, 0, 0, $transColor);
$color = hex2rgb("000000");
$start = -40;
$stop = 120;
$range = $stop - $start;
for ($y = 0; $y < $height; ++$y) {
$alpha = $y <= $start ? 0 : round(min(($y - $start) / $range, 1) * 127);
$newColor = imagecolorallocatealpha($gradientCanvas, $color[0], $color[1], $color[2], $alpha);
imageline($gradientCanvas, 0, $y, $width, $y, $newColor);
}
imagecopyresampled($baseImage, $gradientCanvas, 0, 0, 0, 0, $width, $height, $width, $height);
//==== TEXT ====//
putenv('GDFONTPATH=.');
$font = "HelveticaNeueBold.ttf";
$white = imagecolorallocate($baseImage, 255, 255, 255);
imagettftext($baseImage, 14, 0, $imagePadding, $imagePadding + 16, $white, $font, "Foobar");
header('Content-Type: image/jpeg');
imagejpeg($baseImage);
imagedestroy($baseImage);
imagedestroy($sourceImage);
?>
The default quality of imagejpeg http://php.net/manual/en/function.imagejpeg.php is ~75, you will want to set this to 100
imagejpeg($baseImage, null, 100);
The main reason - you are using JPG as output and input image. Editing JPG images often causes the image distortions. Try to use a PNG image as a source.
Or try set the "quality" parameter of imagejpeg() to 100
I tried to change the $font_size but it seems it is not working. It stays the same no matter what. Even if I altered the $font_size up to 130. It stays the same:
<?php
header('Content-type: image/png');
$rand_num = rand(1000, 9999);
$font_size = 130;
$image_width = 110;
$image_height = 20;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$black_color = imagecolorallocate($image, 0, 0, 0);
imagestring($image, $font_size, 0, 0, $rand_num, $black_color);
for($x=0; $x <= 30; $x++){
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $black_color);
}
imagepng($image);
?>
According to PHP Manual, The font parameter
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().
So you have to try 1 2 3 4 5 and not 130.
For built in fonts, font size can be from 1,2,3,4 and 5. Please refer to http://php.net/manual/en/function.imagestring.php
You can change the font size using imagettftext function.
imagettftext($image, $font_size, $angle, $x, $y, $text_color, '$font-family', $text);
$image is the $imagecreate function
$font_size is the size of font you want.
$angle is the angle of the fonts tilted
$x and $y are coordinates.
$text_color is the imagecolorallocate function
$font-family is the family of font you want to use
$text is the text or random text to be displayed
When i merge two images as one background and the other as target image. I m using pngs. When I rotate the target image and then merge, yes everything is fine except that the edges of rotated image becomes zigzag i means not smooth. How to make the edges smooth using php GD???
The code I am using:
<?php
// Create image instances
$dest = imagecreatefrompng('bg.png');
$src = imagecreatefrompng('text.png');
$width = imagesx($src);
$height = imagesy($src);
imageantialias($src, true);
$color = imagecolorallocatealpha($src, 0, 0, 0, 127);
$rotated = imagerotate($src, 40, $color);
imagesavealpha($rotated, true);
// $trans_colour = imagecolorallocatealpha($rotated, 0, 0, 0, 127);
// imagefill($rotated, 0, 0, $trans_colour);
imagepng($rotated, 'shahid.png');
$new_img = imagecreatefrompng('shahid.png?');
$width = imagesx($new_img);
$height = imagesy($new_img);
// imagecopymerge($dest, $new_img, 50, 50, 0, 0, $width+60, $height+60, 100);
imagecopymerge_alpha($dest, $new_img, 0, 20, 0, 0, $width, $height, 100);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
{
if (!isset($pct)) {
return false;
}
$pct/= 100;
// Get image width and height
$w = imagesx($src_im);
$h = imagesy($src_im);
// Turn alpha blending off
imagealphablending($src_im, false);
// Find the most opaque pixel in the image (the one with the smallest alpha value)
$minalpha = 127;
for ($x = 0; $x < $w; $x++)
for ($y = 0; $y < $h; $y++) {
$alpha = (imagecolorat($src_im, $x, $y) >> 24) & 0xFF;
if ($alpha < $minalpha) {
$minalpha = $alpha;
}
}
// loop through image pixels and modify alpha for each
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
// get current alpha value (represents the TANSPARENCY!)
$colorxy = imagecolorat($src_im, $x, $y);
$alpha = ($colorxy >> 24) & 0xFF;
// calculate new alpha
if ($minalpha !== 127) {
$alpha = 127 + 127 * $pct * ($alpha - 127) / (127 - $minalpha);
}
else {
$alpha+= 127 * $pct;
}
// get the color index with new alpha
$alphacolorxy = imagecolorallocatealpha($src_im, ($colorxy >> 16) & 0xFF, ($colorxy >> 8) & 0xFF, $colorxy & 0xFF, $alpha);
// set pixel with the new color + opacity
if (!imagesetpixel($src_im, $x, $y, $alphacolorxy)) {
return false;
}
}
}
// The image copy
imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
}
?>
I had this in an old project and worked for me. Anyway, I don't remember if my pngs had really alpha transparency or "gif" transparency...
$image = imagecreatefrompng($href);
$newImage = imagecreatefrompng($href2);
imagealphablending($image, true);
imagealphablending($newImage, true);
$newImage = imagerotate($newImage, $r, -1);
$blue = imagecolorallocate($newImage, 0, 0,255);
imagecolortransparent($newImage, $blue);
imagecopy($image,$newImage,$x,$y,0,0,imagesx($newImage) , imagesy($newImage));
From the manual, in that case, imagealphablending should be replaced by imagesavealpha