0

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);    
?>
swapnesh
  • 26,318
  • 22
  • 94
  • 126

2 Answers2

3

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

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
  • I embeded the code in a method so do i need to return the 'image.jpg'; from the method and then use it in under my html?? – swapnesh Dec 31 '12 at 12:11
  • Correct, obviously you'd only need to do that if your `image.jpg` wasn't always `image.jpg`. – Rudi Visser Dec 31 '12 at 12:15
  • just for the info I am adding headers for content type html in mail..so its not the same as I just edit the original code – swapnesh Dec 31 '12 at 12:35
  • That's obviously not going to work, if you're sending this out as an email you'll need to have a full path in your email (including `http://www.domain.com/`). – Rudi Visser Dec 31 '12 at 13:52
  • .yeah very true..just resolved it just.now..plz add this line in your answer and i will then accept your answer :) +1 already – swapnesh Dec 31 '12 at 13:57
1

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);
  • I tried this, I am sending that image in an email so what it does its sending email twice – swapnesh Dec 31 '12 at 12:12
  • It is a problem in other part of your code then probably. You can check if it is by accessing directly to that URL in browser. If just image opens up as if you accessed image directly, then its confirmed. –  Dec 31 '12 at 12:15
  • Your function isn't good, you are editing image, saving it and then returning `mynameimg.jpg`. Since you are sending it in an email you need to return full __web path__ of image! For example: `http://www.mywebsite.com/image/path/myname.jpg`. –  Dec 31 '12 at 12:40
  • @igoran can u plz suggest more what n whr to change..m totally out this time :( – swapnesh Dec 31 '12 at 12:48