• I have this code that uses gd libraries, and it can add text on a image. If i run this code alone or on a static page, it works. But when i run it in WordPress, it appears a broken image.

    This is the original code.

    <?php
          //Set the Content Type
          header('Content-type: image/jpeg');
    
          // Create Image From Existing File
          $jpg_image = imagecreatefromjpeg('sunset.jpg');
    
          // Allocate A Color For The Text
          $white = imagecolorallocate($jpg_image, 255, 255, 255);
    
          // Set Path to Font File
          $font_path = 'font.TTF';
    
          // Set Text to Be Printed On Image
          $text = "This is a sunset!";
    
          // Print Text On Image
          imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
    
          // Send Image to Browser
          imagejpeg($jpg_image);
    
          // Clear Memory
          imagedestroy($jpg_image);
        ?>

    And this is the same code implemented on WordPress, which I have problems.

    <?php
    /*
    Template Name: Tarjetas
    */
    ?>
    <?php
      //Set the Content Type
      header('Content-type: image/jpeg');
    ?>
    <?php get_header(); ?>
        <div class="containing">
        <?php
    		$user = wp_get_current_user();
    		$id = $user->id
    	?>
        <?php
      // Create Image From Existing File
      $jpg_image = imagecreatefromjpeg('https://www.alvarols.com/califica/wp-content/themes/mexicotecalifica/images/tarjeta.jpg');
    
      // Allocate A Color For The Text
      $white = imagecolorallocate($jpg_image, 255, 255, 255);
    
      // Set Path to Font File
      $font_path = 'https://www.alvarols.com/califica/wp-content/themes/mexicotecalifica/font.TTF';
    
      // Set Text to Be Printed On Image
      $text = $id;
    
      // Print Text On Image
      imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
    
      // Send Image to Browser
      imagejpeg($jpg_image);
    
      // Clear Memory
      imagedestroy($jpg_image);
    ?>
    
        </div>
     <?php get_footer(); ?>

    I hope you can help me.

    Thanks.

Viewing 1 replies (of 1 total)
  • you set output as jpeg as the header, so the browser is expecting a image,

    instead, you are sending HTML as-well,

    instead (this may not work, untested and just a guess)

    <?php
    /*
    Template Name: Tarjetas
    */
    ob_start();
      //Set the Content Type
      header('Content-type: image/jpeg');
    
    		$user = wp_get_current_user();
    		$id = $user->id
    
      // Create Image From Existing File
      $jpg_image = imagecreatefromjpeg('https://www.alvarols.com/califica/wp-content/themes/mexicotecalifica/images/tarjeta.jpg');
    
      // Allocate A Color For The Text
      $white = imagecolorallocate($jpg_image, 255, 255, 255);
    
      // Set Path to Font File
      $font_path = 'https://www.alvarols.com/califica/wp-content/themes/mexicotecalifica/font.TTF';
    
      // Set Text to Be Printed On Image
      $text = $id;
    
      // Print Text On Image
      imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
    
      // Send Image to Browser
      imagejpeg($jpg_image, NULL, 100 );
    
      // Clear Memory
      imagedestroy($jpg_image);
    
      $i = ob_get_clean();
    ?>
    <?php get_header(); ?>
        <div class="containing">
          <?php echo "<img src='data:image/jpeg;base64," . base64_encode( $i )."'>"; ?>
        </div>
     <?php get_footer(); ?>
Viewing 1 replies (of 1 total)
  • The topic ‘This code works alone, but it doesn't work implemented in WordPress’ is closed to new replies.