CONVERT HEADER INTO <img>
I'd like to show the image in this page using an <img>
tag instead of an header ( hea开发者_StackOverflow社区der("content-type: image/jpeg"); ). any ideas?
<?php
$main_img = "Porsche_911_996_Carrera_4S.jpg"; // main big photo / picture
$watermark_img = "watermark.gif"; // use GIF or PNG, JPEG has no tranparency support
$padding = 3; // distance to border in pixels for watermark image
$opacity = 50; // image opacity for transparent watermark
$watermark = imagecreatefromgif($watermark_img); // create watermark
$image = imagecreatefromjpeg($main_img); // create main graphic
if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");
$watermark_size = getimagesize($watermark_img);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];
$image_size = getimagesize($main_img);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;
// copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
This script generates an image, its output is an image, so you can use it in a tag, but in an other page:
<img src="image.php" /> <!-- (if image.php is the name of the script you posted) -->
<img src="yourscript.php"/>
or have your script output the image in some file and then use this file.
any ideas?
Don't do it. It is theoretically possible to embed images in HTML using data:
URI's, but it has too many downsides and is usually a bad idea. The classical approach using an <img>
tag that references the PHP script is the way to go.
you work with the ob_start()
and ob_get_contents()
if($_GET['image'])
{
ob_start();
$main_img = "a.jpg"; // main big photo / picture
$watermark_img = "b.gif"; // use GIF or PNG, JPEG has no tranparency support
$padding = 3; // distance to border in pixels for watermark image
$opacity = 50; // image opacity for transparent watermark
$watermark = imagecreatefromgif($watermark_img); // create watermark
$image = imagecreatefromjpeg($main_img); // create main graphic
if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");
$watermark_size = getimagesize($watermark_img);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];
$image_size = getimagesize($main_img);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;
// copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
$s = ob_get_contents();
ob_clean();
echo $s;
}
?>
[img src="echo '?image=1'?>"]
at the end you can in the src of image tage do this
echo '?image=1'
精彩评论