Create a word cloud on selected pixels
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

php
add a comment |
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

php
1
You are sampling every single pixel in the image so ofcourse it's going to take a lot of time, where do you want to write exactly? or what are you trying to accomplish? Personally when I had to work with images I had an easier time using imagine library than using these functions directly & JFYI Imagick would be faster so i'd go with that too.
– ahmad
Dec 27 '18 at 14:50
add a comment |
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

php
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

php
php
asked Dec 20 '18 at 7:23
Akash KumarAkash Kumar
9741342
9741342
1
You are sampling every single pixel in the image so ofcourse it's going to take a lot of time, where do you want to write exactly? or what are you trying to accomplish? Personally when I had to work with images I had an easier time using imagine library than using these functions directly & JFYI Imagick would be faster so i'd go with that too.
– ahmad
Dec 27 '18 at 14:50
add a comment |
1
You are sampling every single pixel in the image so ofcourse it's going to take a lot of time, where do you want to write exactly? or what are you trying to accomplish? Personally when I had to work with images I had an easier time using imagine library than using these functions directly & JFYI Imagick would be faster so i'd go with that too.
– ahmad
Dec 27 '18 at 14:50
1
1
You are sampling every single pixel in the image so ofcourse it's going to take a lot of time, where do you want to write exactly? or what are you trying to accomplish? Personally when I had to work with images I had an easier time using imagine library than using these functions directly & JFYI Imagick would be faster so i'd go with that too.
– ahmad
Dec 27 '18 at 14:50
You are sampling every single pixel in the image so ofcourse it's going to take a lot of time, where do you want to write exactly? or what are you trying to accomplish? Personally when I had to work with images I had an easier time using imagine library than using these functions directly & JFYI Imagick would be faster so i'd go with that too.
– ahmad
Dec 27 '18 at 14:50
add a comment |
2 Answers
2
active
oldest
votes
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));
add a comment |
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;
?>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53864086%2fcreate-a-word-cloud-on-selected-pixels%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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));
add a comment |
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));
add a comment |
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));
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));
answered Dec 29 '18 at 21:23
iazaraniazaran
173211
173211
add a comment |
add a comment |
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;
?>
add a comment |
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;
?>
add a comment |
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;
?>
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;
?>
answered Dec 29 '18 at 21:45
ivanivanivanivan
1,618268
1,618268
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53864086%2fcreate-a-word-cloud-on-selected-pixels%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You are sampling every single pixel in the image so ofcourse it's going to take a lot of time, where do you want to write exactly? or what are you trying to accomplish? Personally when I had to work with images I had an easier time using imagine library than using these functions directly & JFYI Imagick would be faster so i'd go with that too.
– ahmad
Dec 27 '18 at 14:50