MK DEV
MK DEV

Reputation: 64

Image header issue facing

I have issue that when i am inserting the dynamic text into the existing image i mean creating dynamic text into the image here is the code:

<?
include("header.php");
include("menu.php");

     $rImg = imagecreatefrompng("../images/test.php");
     $cor = imagecolorallocate($rImg, 0, 0, 0);
     imagestring($rImg,5,126,22,"test",$cor);
     imagejpeg($rImg,NULL,100);

include("btSlider.php");
include("footer.php");
?>

but i have issue that i don't want to use header output header('Content-type: image/png'); the above code show me the garbage values. If i want to show the image for the following code what should i do.

If i run this its work

<?
     $rImg = imagecreatefrompng("../images/test.php");
     $cor = imagecolorallocate($rImg, 0, 0, 0);
     imagestring($rImg,5,126,22,"test",$cor);
     header('Content-type: image/png');
     imagejpeg($rImg,NULL,100);
?>

i want to add the header and footer how could i do this and display the image on header and footer included file and also save the rImg varaiable.

I am beginner in the php that(s) why i was facing difficulties.

Updated: I also want to know that how could i enter multiple text on the image ?

Thanks in advance.

Upvotes: 1

Views: 226

Answers (2)

Orangepill
Orangepill

Reputation: 24645

If you want the image embedded onto your page you can use a dataurl as your image src.

<?php
     $rImg = imagecreatefrompng("../images/test.php");
     $cor = imagecolorallocate($rImg, 0, 0, 0);
     imagestring($rImg,5,126,22,"test",$cor);
     ob_start();
     imagejpeg($rImg,NULL,100);
     $imgData = ob_get_clean();
?> 
<img src="data:image/jpeg;base64,<?= base64_encode($imgData); ?>"/>

Upvotes: 2

The image generation code should be in it's own file. You can then reference the php/image src using HTML..

image.php

<?php
   $rImg = imagecreatefrompng("../images/test.php");
   $cor = imagecolorallocate($rImg, 0, 0, 0);
   imagestring($rImg,5,126,22,"test",$cor);
   //header('Content-type: image/png');
   header('Content-type: image/jpeg');
   //imagepng($rImg,NULL,100);
   imagejpeg($rImg,NULL,100);

the_page.php

<?php

    include("header.php");
    include("menu.php");
    include("btSlider.php");
    include("footer.php");

header.php/footer.php

<img src="image.php" alt="This is the php generated image" >

NOTE in your code sample, you are sending a png header header('Content-type: image/png'); and rendering a jpeg with imagejpeg($rImg,NULL,100);

Upvotes: 2

Related Questions