Create a word cloud on selected pixels












9















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



enter image description here



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



enter image description here










share|improve this question


















  • 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
















9















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



enter image description here



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



enter image description here










share|improve this question


















  • 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














9












9








9


1






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



enter image description here



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



enter image description here










share|improve this question














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



enter image description here



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



enter image description here







php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












2 Answers
2






active

oldest

votes


















1





+50









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));





share|improve this answer































    0














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





    share|improve this answer























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


      }
      });














      draft saved

      draft discarded


















      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









      1





      +50









      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));





      share|improve this answer




























        1





        +50









        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));





        share|improve this answer


























          1





          +50







          1





          +50



          1




          +50





          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));





          share|improve this answer













          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));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 29 '18 at 21:23









          iazaraniazaran

          173211




          173211

























              0














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





              share|improve this answer




























                0














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





                share|improve this answer


























                  0












                  0








                  0







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





                  share|improve this answer













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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 29 '18 at 21:45









                  ivanivanivanivan

                  1,618268




                  1,618268






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Mossoró

                      Error while reading .h5 file using the rhdf5 package in R

                      Pushsharp Apns notification error: 'InvalidToken'