将标头转换为

发布于 2024-10-13 19:36:51 字数 1288 浏览 2 评论 0原文

我想使用 标记而不是标头 ( header("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);
?>

I'd like to show the image in this page using an <img> tag instead of an header ( header("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);
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如果没有你 2024-10-20 19:36:51

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

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

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) -->
城歌 2024-10-20 19:36:51
<img src="yourscript.php"/> 

或者让您的脚本将图像输出到某个文件中,然后使用该文件。

<img src="yourscript.php"/> 

or have your script output the image in some file and then use this file.

漆黑的白昼 2024-10-20 19:36:51

有什么想法吗?

不要这样做。理论上可以使用 data: URI's,但它有太多缺点,通常是一个坏主意。使用引用 PHP 脚本的 标签的经典方法是可行的方法。

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.

随波逐流 2024-10-20 19:36:51

使用 ob_start()ob_get_contents() 您可以在图像标签的 src 中执行此操作


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'?>"]

最后
回显'?image=1'

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'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文