CONADERT HEADER INTO <img/>

时间:2011-01-25 20:20:56

标签: php header gd image

我想使用<img>标记而不是标题(标题(“content-type:image / jpeg”); )在此页面中显示图片。任何想法?

<?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);
?>

4 个答案:

答案 0 :(得分:4)

此脚本生成图像,其输出是图像,因此您可以在标记中使用它,但在其他页面中使用它:

<img src="image.php" /> <!-- (if image.php is the name of the script you posted) -->

答案 1 :(得分:3)

<img src="yourscript.php"/> 

或让您的脚本在某个文件中输出图像,然后使用此文件。

答案 2 :(得分:3)

  

任何想法?

不要这样做。理论上可以使用data: URI's在HTML中嵌入图像,但它有太多的缺点,通常是个坏主意。使用引用PHP脚本的<img>标记的经典方法是可行的方法。

答案 3 :(得分:1)

您使用ob_start()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'?>"]

最后你可以在图像trc的src中做到这一点 echo '?image=1'