是否有任何PHP函数返回图像句柄,以便我可以在img src html标签中使用它?

时间:2012-12-31 11:48:55

标签: php image php-gd

我尝试了这个imagejpeg() php方法,但它在运行代码时将图像返回给浏览器。

但是我需要一个返回图像URL的方法,以便我可以在我的img标签中使用它。

目前我必须使用 -

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

但我认为如果方法返回一个图像处理程序以便我可以在我的img标记中使用它更好,比如 -

<img src="image.jpg" />

我的myphpfile.php是包含代码的文件。

在php手册中搜索了很多功能,但没有运气,我也使用这种方法imagettftext()在图像上添加文字。

文件中的总代码 -

<?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);    
?>

2 个答案:

答案 0 :(得分:3)

有两种更明显的方法可以解决这个问题。

  1. 将渲染图像写入文件

    通过传入文件名的第二个参数,可以使用imagejpeg,例如:

     $im = imagecreatetruecolor(..);
     // ..
     imagejpeg($im, 'image.jpg');
    
  2. 使用网址重写

    您始终可以将特殊文件夹中的所有.jpgs(例如/dynamic/image.jpg)传递到将呈现它的脚本。如果需要,您甚至可以将文件名(image.jpg)作为参数添加到脚本中,告诉它在图像是动态的情况下要渲染的内容。

  3. 显然,如果每个请求的图像需要不同,那么#2是更好的选择。但是,如果您的图像是静态的(即您的imagettftext始终在同一图像上写入相同的文本),#1会更快并且建议使用。

答案 1 :(得分:1)

您的PHP页面可以返回图像:

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

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

然后,您可以使其读取GET参数并返回适当的图像。然后你可以在另外这样的页面中使用它:

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

这对PNG图像类型有效,对于其他类型,您必须将Content-Type标题更改为适当的格式。您可以找到可用的格式here

扩展答案:

假设您需要一个需要根据传递的图像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);