You can use php header, only if nothing was outputed before... In that case, You have entered <div class="image" >
, and the standard text header was send. In that case sending another header is WRONG.
As mentioned in comments, <?php
- needs space after.
The solution is simple. Create another php file, and call it image.php. In that file, enter this code (inside <?php
?>
tags):
header('Content-Type: image/jpeg');
$image=$link;
$image_size=getimagesize($image);
$image_width=$image_size[0];
$image_height=$image_size[1];
$new_width=200;
$new_height=200;
$new_image = imagecreatetruecolor($new_width,$new_height);
$old_image = imagecreatefromjpeg($image);
imagecopyresized($new_image,$old_image,0,0,0,0,$new_width,$new_height,$image_width,$image_height);
imagejpeg($new_image);
In Your original code, you can use this:
<div class="image" >
<img src="image.php">
</div>
In that case, php script is visible as an image. With the header that suggest it is an image. In the real code, You only give a source of this image, which is image.php. The extension, is not important (normally we write image.jpg for exxample), but the headers of that file (header('Content-Type: image/jpeg');
) tells browser that this is really an image.
Additional note:
If in Your main script you've got some logic, that you are using to create that image, you must pass all this logic to that image using GET method, or rewrite it inside image.php...
For example, if the size is a variable (in that case it is not, but I'm using an example), you can do:
<img src="image.php?width=<?php echo $width; ?>">
And in image.php
$width = isset($_GET['width']) ? $_GET['width'] : 100; // 100 = default
Remember to not pass any IMPORTANT informations using that method. If for example you are producing captcha image, you should not pass tha captcha code in get ;).
I hope that Your image producing code is good. If not, You can remove the first line, and visit image.php in your browser. Without any headers it will treat it as a normal text page, and will display php errors. If there are no errors, just bit-coded-something, then add the header, and it should work.
Hope it helps, and give you a little clue on how the headers work in php. There is no "on the fly" header, each file lives in its own world of headers.