Is there any php function that returns an image handle so that i can use it in img src html tag?

余生长醉 提交于 2019-12-12 03:48:40

问题


I tried this imagejpeg() php method, but it returns an image to a browser on running the code.

However I need a method that returns an image URL so that i can use it in my img tag.

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 src="image.jpg" />

My myphpfile.php is the file that contains the code.

Searched many functions at php manuals but no luck, I am also using this method imagettftext() for text addition over image.

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

回答1:


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:

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

    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.

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).




回答2:


Your PHP page can return an image:

<?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. 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. 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.

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


来源:https://stackoverflow.com/questions/14101197/is-there-any-php-function-that-returns-an-image-handle-so-that-i-can-use-it-in-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!