将标头转换为
我想使用 标记而不是标头 ( 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该脚本生成一个图像,它的输出是一个图像,因此您可以在标签中使用它,但在其他页面中:
This script generates an image, its output is an image, so you can use it in a tag, but in an other page:
或者让您的脚本将图像输出到某个文件中,然后使用该文件。
or have your script output the image in some file and then use this file.
不要这样做。理论上可以使用
data:
URI's,但它有太多缺点,通常是一个坏主意。使用引用 PHP 脚本的标签的经典方法是可行的方法。
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.使用
ob_start()
和ob_get_contents()
您可以在图像标签的 src 中执行此操作最后
回显'?image=1'
you work with the
ob_start()
andob_get_contents()
at the end you can in the src of image tage do this
echo '?image=1'