Content-Type header does not let HTML / CSS

0

I have this code to superimpose two images, one on top of the other

<?php
$img = $_POST['img'];
$user = imagecreatefromjpeg($img);
$mask = imagecreatefromgif('imgs/logo.gif');
$width = imagesx($user);
$height = imagesy($user);
$metade = $width/50;
$altura =  $height/3.4;
imagealphablending($user, false);
imagesavealpha($user, true);
imagecopymerge($user, $mask, $metade, $altura, 0, 0, 620, 360, 60); 
header('Content-Type: image/png');
imagepng($user);
imagedestroy($user);
imagedestroy($mask);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div id="listra"></div>
    <?php


    ?>
</body>
</html>

Only the

header('Content-Type: image/png');

not letting you use html, css would have some solution?

    
asked by anonymous 16.11.2015 / 03:28

2 answers

0

When you submit a Content-type header, you can not use more than one file type (image and HTML, in your example). One solution is to encode the base-64 generated image and display it with a data URI >, for this you need to control the buffer output of imagepng() with the functions ob_* .

In your code remove header('Content-Type: image/png'); and replace imagepng($user); with:

ob_start(); // liga o buffer de saída
imagepng($user);
$imagem = ob_get_clean(); // armazena o conteúdo e desliga  o buffer
$tagImagem = '<img src="data:image/png;base64,' . base64_encode($imagem) . '"/>';

And within <body>

echo $tagImagem;
    
17.11.2015 / 13:09
1

The solution would be to save the php script as a different file, and add it to the script as any image.

<?php
// imagem_script.php

$img = $_POST['img'];
$user = imagecreatefromjpeg($img);
$mask = imagecreatefromgif('imgs/logo.gif');
$width = imagesx($user);
$height = imagesy($user);
$metade = $width/50;
$altura =  $height/3.4;
imagealphablending($user, false);
imagesavealpha($user, true);
imagecopymerge($user, $mask, $metade, $altura, 0, 0, 620, 360, 60); 
header('Content-Type: image/png');
imagepng($user);
imagedestroy($user);
imagedestroy($mask);
?>

Then just call this image in the img tag, so that it is displayed in the body of this document, because due to content-type defined in the script header, this file is a script-generated image, and now has the MIME type image / png, which is the same used in normal images.

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div id="listra"></div>
    <img src="imagem_script.php"/>
</body>
</html>
    
16.11.2015 / 04:20