Jump to content

image showing as text


brem13

Recommended Posts

hey, i'm trying to add a watermark to pictures that show up, however it is only showing as text

here is the code

$watermark = imagecreatefrompng('watermark1.png');  
$image = imagecreatefromjpeg("/userImages/big/".$pic);  
imagecopymerge($image, $watermark, 5, 5, 0, 0, 135, 35, 100);  
imagejpeg($image);  
imagedestroy($image);  
imagedestroy($watermark); 

also, where i fould how to do this, said u have to put a

header('content-type: image/jpeg');

code at the top of the page or else it would read as text, however, when i do that, the whole page disappears except for a question mark thing indicating a picture should be there? what do i do??

Link to comment
https://forums.phpfreaks.com/topic/209478-image-showing-as-text/
Share on other sites

You have to have the header() function in there for it to work.  You are merging the $watermark into the $image, but if $image isn't returning an image, then it will fail.

 

Are you sure that $pic is set, and is a valid image in that directory?

yes, the pic is there, i tested the same code in a blank page with the header and it worked, but when i add the code to the section of a page i want it in, and add the header to the top, it only shows a 'little box with a question mark(im on a mac) indicating a picture should be there' - the image address for that is just the url to that page and shows nothing else on the page

i tested the same code in a blank page with the header and it worked, but when i add the code to the section of a page i want it in, and add the header to the top,

 

That is why it isn't working.

 

You must have all of your code in a separate file.  If $pic is defined in your main file, then pass it as a get parameter.

 

main.php

<?php
$pic = 'somepic.jpg';
?>
<img src="pic.php?pic=<?php echo $pic; ?>" alt="<?php echo $pic; ?>"/>

 

pic.php

header('content-type: image/jpeg');
$pic = $_GET['pic'];
$watermark = imagecreatefrompng('watermark1.png');  
$image = imagecreatefromjpeg("/userImages/big/".$pic);  
imagecopymerge($image, $watermark, 5, 5, 0, 0, 135, 35, 100);  
imagejpeg($image);  
imagedestroy($image);  
imagedestroy($watermark);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.