0

このimagejpeg() phpメソッドを試しましたが、コードを実行するとブラウザに画像が返されます。

ただし、画像のURLを返すメソッドが必要です。これにより、画像のURLをimgタグで使用できるようになります。

現在私は使用する必要があります-

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

しかし、メソッドが画像ハンドラーを返し、imgタグで使用できるようにした方が良いと思います。

<img src="image.jpg" />

私のmyphpfile.phpは、コードを含むファイルです。

imagettftext()phpマニュアルで多くの関数を検索しましたが、運が悪かったので、画像にテキストを追加するためにもこの方法を使用しています。

ファイル内の合計コード-

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

2 に答える 2

3

この問題にアプローチするには、さらに 2 つの明白な方法があります。

  1. レンダリングされたイメージをファイルに書き込む

    これはimagejpeg、ファイル名の 2 番目のパラメーターを渡すことで可能です。たとえば、次のようになります。

     $im = imagecreatetruecolor(..);
     // ..
     imagejpeg($im, 'image.jpg');
    
  2. URL 書き換えを使用する

    特別なフォルダー内のすべてをいつでも通過でき.jpgsます (たとえば/dynamic/image.jpg、それをレンダリングするスクリプトに通過します)。必要に応じて、ファイル名 ( image.jpg) をスクリプトのパラメーターとして使用して、画像が動的な場合に何をレンダリングするかを指示することもできます。

明らかに、リクエストごとに画像を変える必要がある場合は、#2 の方が適しています。ただし、画像が静的な場合 (つまり、imagettftext常に同じ画像に同じテキストを書き込む場合) は、#1 の方が高速であり、推奨されます。

于 2012-12-31T11:52:52.043 に答える
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ヘッダーを適切な形式に変更する必要があります。利用可能なフォーマットはここにあります。

拡張回答:

渡された画像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);
于 2012-12-31T11:57:38.210 に答える