有没有返回图像句柄的php函数,以便我可以在img src html标签中使用它? [英] Is there any php function that returns an image handle so that i can use it in img src html tag?
问题描述
我尝试了 imagejpeg() php方法,但是它返回了图像浏览器运行代码.
I tried this imagejpeg() php method, but it returns an image to a browser on running the code.
但是,我需要一种返回图像URL的方法,以便可以在img标签中使用它.
However I need a method that returns an image URL so that i can use it in my img tag.
当前我必须使用-
<img src="siteurl/myphpfile.php" />
但是我认为如果该方法返回一个图像处理程序更好,这样我就可以在img标签中使用它,例如-
but i think its better if the method returns an image handler so that i can use it in my img tag, like -
<img src="image.jpg" />
我的myphpfile.php是包含代码的文件.
My myphpfile.php is the file that contains the code.
在php手册中搜索了许多功能,但没有运气,我也使用此方法imagettftext()
在图像上添加文本.
Searched many functions at php manuals but no luck, I am also using this method imagettftext()
for text addition over image.
文件中的总代码-
<?php
function myimagecreate( $myname ) {
$headurl = 'template_header01.jpg';
header('Content-type: image/jpeg');
$text = "My Full Text";
$name = $text.".jpg";
$jpg_image = imagecreatefromjpeg($headurl);
$black = imagecolorallocate($jpg_image, 1, 1, 1);
$font_path = 'myfont/arial.ttf';
imagettftext($jpg_image, 24, 0, 175, 85, $black, $font_path, $text);
imagejpeg($jpg_image,'mynameimg.jpg');
imagedestroy($jpg_image);
return 'mynameimg.jpg';
}
$to = 'swapnesh20032003@gmail.com';
$subject = $myname;
$message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Kings</title>
</head>
<body>
<table width="100%"><tr><td valign="bottom" align="center" height="282"><img src="'.myimagecreate( $myname ).'" /></td></tr></table></table></body></html>';
mail($to,$subject,$message);
?>
推荐答案
有两种更明显的方法可以解决此问题.
There are 2 more obvious ways to approach this problem.
-
将渲染的图像写入文件
使用imagejpeg
,可以通过输入文件名的第二个参数来实现,例如:
This is possible using imagejpeg
, by passing in a second parameter of the filename, for example:
$im = imagecreatetruecolor(..);
// ..
imagejpeg($im, 'image.jpg');
使用URL重写
您始终可以遍历特殊文件夹中的所有.jpgs
(例如,将/dynamic/image.jpg
传递到将呈现它的脚本中).如果需要,您甚至可以将文件名(image.jpg
)作为脚本的参数,告诉脚本如果图像是动态的,则呈现什么内容.
You can always pass through all .jpgs
in a special folder (for example /dynamic/image.jpg
through to your script which will render it). If you want, you could even take the filename (image.jpg
) as a parameter to your script telling it what to render if the image is dynamic.
很明显,如果每个请求的图像需要不同,则#2是更好的选择.但是,#1速度更快,如果图像是静态的,则建议使用#1(例如,imagettftext
总是在同一图像上写入相同的文本).
Obviously, if your image needs to be different per request, #2 is the better option. However, #1 is faster and recommended if your image is static (ie. your imagettftext
always writes the same text over the same image).
这篇关于有没有返回图像句柄的php函数,以便我可以在img src html标签中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!