简体   繁体   English

有没有返回图像句柄的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?

I tried this imagejpeg() php method, but it returns an image to a browser on running the code. 我尝试了这个imagejpeg() php方法,但是在运行代码时它将图像返回到浏览器。

However I need a method that returns an image URL so that i can use it in my img tag. 但是,我需要一种返回图像URL的方法,以便可以在img标签中使用它。

Currently i have to use - 目前我必须使用-

<img src="siteurl/myphpfile.php" />

but i think its better if the method returns an image handler so that i can use it in my img tag, like - 但是我认为如果该方法返回图像处理程序更好,这样我就可以在img标签中使用它,例如-

<img src="image.jpg" />

My myphpfile.php is the file that contains the code. 我的myphpfile.php是包含代码的文件。

Searched many functions at php manuals but no luck, I am also using this method imagettftext() for text addition over image. 在php手册中搜索了许多功能,但是没有运气,我也使用imagettftext()这个方法在图像上添加了文本。

Total code in the File -- 文件中的总代码-

<?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. 有两种更明显的方法可以解决此问题。

  1. Write the rendered image to a file 将渲染的图像写入文件

    This is possible using imagejpeg , by passing in a second parameter of the filename, for example: 使用imagejpeg可以实现这一点,方法是传入文件名的第二个参数,例如:

      $im = imagecreatetruecolor(..); // .. imagejpeg($im, 'image.jpg'); 
  2. Use URL Rewriting 使用URL重写

    You can always pass through all .jpgs in a special folder (for example /dynamic/image.jpg through to your script which will render it). 您始终可以在特殊文件夹中浏览所有.jpgs文件(例如,将/dynamic/image.jpg传递到脚本中进行渲染)。 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. 如果需要,您甚至可以将文件名( image.jpg )作为脚本的参数,告诉脚本如果图像是动态的,则呈现什么内容。

Obviously, if your image needs to be different per request, #2 is the better option. 显然,如果每个请求的图像需要不同,则#2是更好的选择。 However, #1 is faster and recommended if your image is static (ie. your imagettftext always writes the same text over the same image). 但是,#1速度更快,如果图像是静态的,则建议使用#1(即, imagettftext始终在同一图像上写入相同的文本)。

Your PHP page can return an image: 您的PHP页面可以返回图像:

<?php
$filename = "my_image.png";
$image = fopen($filename, 'rb');

header("Content-Type: image/png");
header("Content-Length: " . filesize($filename));
fpassthru($image);

You can then make it read GET argument and return appropriate image. 然后,您可以使其读取GET参数并返回适当的图像。 Then you could use it in another pages like this: 然后,您可以在其他页面中使用它,如下所示:

<img src="my_image_return_page.php?image=<?=$image_id?>">

This would ofcourse work for PNG image type, for other types you will have to change Content-Type header to appropriate format. 这当然适用于PNG图像类型,对于其他类型,则必须将Content-Type标头更改为适当的格式。 You can find available formats here . 您可以在此处找到可用的格式。

Extended answer: 扩展答案:

Say you want a script that needs to read image from database according to image id passed. 假设您需要一个脚本,该脚本需要根据传递的图像ID从数据库读取图像。

<?php
//Get image id
$imageId = $_GET['id'];

//You search for your image in database here, and return the location
//Additionally if image with that id wasn't found, you can return location of some image that says "Ooops, we couldn't find image you were looking for".
//Say you save image's location in $imageLocation variable.

//Get pathinfo of your image file
$pathInfo = pathinfo($imageLocation);

$extension = $pathInfo['extension'];
if ($extension == "jpg") {
    $contentType = "jpeg";
} elseif ($extension == "png") {
    $contentType = "png";
} elseif ($extension == "gif") {
    $contentType = "gif";
} else {
    //Handle error here if format isn't recognized
}

//Set content type
header("Content-Type: image/".$contentType);
//Set content length
header("Content-Length: ". filesize($imageLocation));

//This is shorter way then using fopen, but has the same result
echo file_get_contents($imageLocation);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM