在php中使用GD进行图像处理和输出 [英] Image processing and output using GD in php

查看:78
本文介绍了在php中使用GD进行图像处理和输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用PHP GD库控制图像的显示-当浏览器请求图像时,我将在运行时向其底部添加一些文本,而不是将文本保存到图像中.

I want to control the display of images with the PHP GD library -- I am going to add some text to the bottom corner of it on the fly when a browser requests the image, rather than saving the text into the image.

我知道我可以这样做:在标头中设置MIME类型,然后使用文件名调用imagepng(...)以仅将图像显示在浏览器中,但是如何将其嵌入文档中?喜欢,

I know that I can do this: set the MIME type in the header and then call imagepng(...) with the filename to just display the image to the browser, but how would I embed it in a document? Like,

<img src='somefile.php?i=1' ... />

我只用文件名调用imagepng而不设置标题吗?

do I just call imagepng with the filename but without setting the headers?

此外,如果有人从源代码中复制图像源并在浏览器中导航到它……如果不设置标头,将会发生什么?图像会显示为好像请求了实际图像吗?

Also, if someone copies the image source out of the source code and navigates to it in the browser... what will happen if the headers aren't set? Will the image display as if the actual image was requested?

推荐答案

如果在用户尝试直接访问该URL时在somefile.php上生成图像,则浏览器输出将是图像,除非他们未指定变量其中包含图像本身的ID/名称.

If you generate the image on somefile.php when a user tries to access that url directly the browsers output will be the image unless they don't specify the variable which contains the id/name of the image itself.

要在html上使用图像,只需执行<img src='somefile.php?f=FILENAME' />使其更具可读性(只要您具有相关的图像名称即可).

To use the image on html I just do <img src='somefile.php?f=FILENAME' /> to make it more readable (as long as you have relevant image names).

请确保通过重定向或显示默认图像来处理对somefile.php的非指定访问.

Make sure to handle a non specified access to somefile.php by either redirecting or showing a default image.

关于标题,这是告诉浏览器它将处理的文件类型的信息,因此请确保始终指定它们.例如:

About the headers, that's what tells the browser what type of file it will be handling, so make sure you specify them always. For example:

#somefile.php

header('content-type: image/jpeg');  

$watermark = imagecreatefrompng('watermark.png');  
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);

$image = imagecreatetruecolor($watermark_width, $watermark_height);  
$image = imagecreatefromjpeg($_GET['src']);  //Path to the image file

$size = getimagesize($_GET['src']);          
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);  
imagejpeg($image);  
imagedestroy($image);  
imagedestroy($watermark);  

因此,要使用此代码在html页面上输出图像,请执行以下操作:

So to output an image with this code on your html page, you would do the following:

<img src='somefile.php?src=filePath' />

注意: 如果您不希望jpg,只需将其更改为png.

Note: If you don't want jpg just change it to png.

可在此处获得更多有关GD + PHP的文档.

More documentation about GD + PHP available here.

这篇关于在php中使用GD进行图像处理和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆