I use the imagic extension. I have an $image_obj object. I can just echo it but it makes it look like random number of characters unless I put:
$Obj_img->setImageFormat("png");
header("Content-Type: image/png");
echo $image_obj;
Is there way to actually put it like this:
echo "<html>
<body>
this is my image: {$image_obj}
</body>
</html>";
I know you can do
echo "this is my image <img src="raw:{$image_obj}" />
but is there any other way without saving document to a file?
Make an image.php script that generates the image, and point to it inside your html:
<img src="image.php?param1=value1¶m2=value2&..."/>
Related
I'm generating a png image using php that works here:
http://www.sidesay.co/BTC_spark.php
However I am attempting to then insert the generated png file into a table on a webpage (I'm unsure if that's terribly inefficient). I use the php line:
echo "<td><img src=\"https:\/\/www.sidesay.co\/BTC_spark.php\" ></td>";
I get a broken image, but if I right click and select "load image" it works.
Should I instead first run the php file and save the image separately before destroying it? If so, how?
Thanks in advance.
Your BTC_spark.php script is outputing HTML itself:
<html>
<body>
<img src='/libraries/Sparkline/sparkline.php?size=80x20&data=2,4,5,6,10,7,8,5,7,7,11,8,6,9,11,9,13,14,12,16&back=fff&line=5bb763&fill=d5f7d8' />
</body>
</html>
Change it to just output the img src:
/libraries/Sparkline/sparkline.php?size=80x20&data=2,4,5,6,10,7,8,5,7,7,11,8,6,9,11,9,13,14,12,16&back=fff&line=5bb763&fill=d5f7d8
... and then in your main script, stick this output in a variable:
$mypic = file_get_contents('./BTC_spark.php');
echo '<td><img src="'.$mypic.'"></td>';
thank you so much for your hard work,
my issue hopefully is not that complicated.
I'm trying to display few images in my website, and it won't display,
the php page and the image are on the same directory, and when i try the same directory on html file it works fine, but when I try to use the same directory on php file the pic look broken icon,and won't appear..
I also tried png and jpg image, still nothing coming out..
please guys..
this is the simple code, i tried to test,
<html>
<head><title> hello </title></head>
<body>
<?php $image = 'zz.png'; ?>
<img src= "<?php $image ?>" style="width:304px;height:228px;">
hello world..
</body>
</html>
Print the var $image with echo (and close the image tag with a frontslash at the end of the tag for correct html):
<img src= "<?php echo $image; ?>" style="width:304px;height:228px;" />
Other possible reasons the image is not displayed (after you print the image name out with echo):
the value of your var $image mismatched with the imagename.
the image is not stored in the same folder as the php document.
your image is saved in CMYK mode, and not in RGB.
the file permission of the image
configuration of the webserver (apache for ex.)
You have to print out the $image variable by using the echo function. Your code should be looking like this:
<?php
$image = 'zz.png';
?>
<html>
<head>
<title> hello </title>
</head>
<body>
<img src= "<?php echo $image; ?>" style="width:304px;height:228px;">
hello world..
</body>
</html>
Furthermore, it looks better when you put all your php stuff before the HTML code, that does not necessarily have to be inside HTML.
If it is still not working, you are probably viewing your html file locally. You have to view it remotely by using a http web server with a php interpreter included. You can easily set up one with XAMPP.
Remember that it does not work by just opening your file in a browser.
I think you should replace this line:
<img src= "<?php $image ?>" style="width:304px;height:228px;">
With this:
<img src= "<?php echo($image) ?>" style="width:304px;height:228px;">
Since echo() is the function that generates the output.
render.php
$form = '<b>Hi</b><br /><img src="test.jpg" /><br />New File';
echo $form;
header("Content-Type: image/png");
imagejpeg($form);
But this code not work
I want this page render to img file :
<img src="render.php" />
you are trying to output both HTML and image-data - that doesnt make any sense.
Instead you should put an HTML-img-tag into your website, the src-attribute being render.php which sets the header via header() and directly outputs image data - without any additional HTML.
https://stackoverflow.com/a/1851856/351861
You should use extra php file for images, something like:
<img src="render.php?id=15" />
Because if your php script returns only one picture it is used wrong. so you pass it what image to show.
And render.php might be something like
$pic_id = (int)$_GET['id'];// cast to int because only numbers are allowed here
$pic_path = 'assets/img/';
$f = $pic_path.$pic_id.".jpg";// it's just an example so it's simplyfied
header("Content-Type: image/png");
imagejpeg($form);
The point here is that you can check user's permissions to see the picture very flexible and log some data about downloader.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to use imagecreatefromjpeg, imagecreatetruecolor, imagecopyresized and imagejpeg while making use of the echo "<html><body>"; etc...
For some reason I get "The image could not be displayed because there were errors on the page" until I comment out header('Content-type: image/png'); and then I just get a picture of a broken picture, like a torn page.
All I've seen is that I can't have header('Content-type: image/png'); and html in the same .php file. If that's the case can anybody tell me how to resize an image for a thumbnail gallery while still having html in a .php file?
Thanks in advance.
You are mixing up two different things.
A webpage with an image on it, does not contain that image in general. Instead, it often refers to an external source. I said in general, because, yes, an image can be embedded in an HTML page, see below.
You have two options:
You can create an apart PHP file where you create the image and output its bytes. In your HTML code, you refer to that image:
page.html:
<html>
<body>
<img src="myimage.php" alt="" />
</body>
</html>
myimage.php:
<?php
header("Content-Type: image/png");
createimageandso_on();
// Do the drawing.
?>
Or you can embed the image in your HTML file, using base64 encoding:
<?php
$contents = all_bytes_from_created_image();
// Get the bytes from the created image.
$base64 = base64_encode($contents);
?>
<html>
<body>
<img src="data:image/png;base64,<?php echo $base64; ?>" alt="" />
</body>
</html>
The second option is suitable for smaller images, since the base64 encoded string will produce large portions of text.
Edit
If I understand it correctly, you want to read images from a directory and resize them to the same size, using them as thumbnails?
What you might just want to do is create a PHP file where you read a source image and give them the same size.
Just like 'normal' PHP files, PHP can do something with the request parameters you give. Perhaps you've ever seen this:
http://example.com/somepage.php?key=value&anotherkey=anothervalue
That string behind the question mark (key=value&anotherkey=anothervalue) is the query string. PHP can do something with the values:
<?php
echo $_GET['key']; // returns "value"
echo $_GET['anotherkey']; // returns "anothervalue"
?>
Now we can just do the same when creating an image. You don't have to make twenty PHP files with almost the same code, but just a single file which reads a file (you name it) and resizes it to the specified width (you name it) and height (you name it).
thumbnail.php
<?php
// Get some request parameters we're going to use.
// We're expecting the parameters below to exist.
$file = $_GET['filepath'];
$width = $_GET['width'];
$height = $_GET['height'];
// Now we're gonna create the image from the given file.
$src = imagecreatefromjpeg($file);
imagecreatetruecolor($width, $height);
// And the rest of the file reading and image creation.
header("Content-Type: image/jpeg");
imagejpeg($image);
?>
webpage.html
<html>
<body>
<?php
$width = 100;
$height = 100;
$files = read_some_directory_and_return_a_list_of_filenames();
foreach ($files as $file) {
// Echo an image tag in the HTML document;
// use as image our thumbnail.php file and give it a query string:
echo "<img src=\"thumbnail.php?width=".$width."&height=".$height."&filepath=".$file."\" alt=\"\" />";
}
?>
</body>
</html>
What you've seen is correct - you can't have a file with Content-type: image/png that contains html contents. The browser will interpret the html code as encoded html data, which is wrong.
What you should do is leave the content type as text/html and send the image in an html document, as in my example below. I left out the <head> for simplicity, but you should add one.
<!DOCTYPE html>
<html>
<body>
<img src="myimage.png" width="100" height="100" />
</body>
</html>
You cannot have markup inside your image you want to push down to the browser. You're telling the browser to expect an image and then sending down some markup, that's a no-no.
What we can do is include a .php file which uses your imagecreatetruecolor, etc... functions inside some img tags:
<html>
<head><title>Image Test</title></head>
<body>
<img src="image.php?file=A" />
<img src="image.php?file=B" />
</body>
</html>
Then image.php would use our image/jpeg headers and contain your imagecreatefromjpeg code.
<?
header('Content-Type: image/jpeg');
if ($_GET['file'] == 'A') {
$img = imagecreatefromjpeg('image1.jpg');
} elseif ($_GET['file'] == 'B') {
$img = imagecreatefromjpeg('image2.jpg');
}
// do your modification etc... here
imagejpeg($img);
imagedestroy($img);
You can only output "image code" in your image.php file because the browser is going to treat it as if it's a regular jpeg.
Can someone help me with this code? The problem is it doesn't show the image but this weird stuff: OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOÿÀx !ÿÄ¢ }!1AQa"q2‘¡#B±ÁRÑð$3br‚%&'()*456789:CDEFGHIJSTUVW
Here is my code:
<?php
//mysql connect database
mysql_connect('localhost','root','password') or die(mysql_error());
mysql_select_db('database') or die(mysql_error());
$res=mysql_query('select * from img');
while($row=mysql_fetch_array($res))
{
echo "<div id='image'>";
echo "<p/>";?> <img src="<?php echo $row['image'];?>" height='300px' width='468px'>";<?php
echo "<p/>"; echo $row['name'];
echo "</div>";
}
?>
<img> expects a URL pointing at where the image file is. You're trying to dump the raw binary garbage of the image into that image tag. That garbage will naturally contain " and > chars in it, and "close" your img tag, letting the rest of the garbage be treated as plain text.
You either have to serve up your image via separate script, e.g.
html:
<img src="pic.php?id=foo">
php:
header('Content-type: image/jpeg');
echo get_blob_from_database($_GET['id']);
Or embed the image inside the html properly:
<img src="data:image/jpg;base64,<?php echo base64_encode($row['image']); ?>">
And neither of these is particularly a good solution. Storing images directly in the database is almost never a good idea.
it's better to save your images in folder rather than save in blob format
you can save the file name in database table and save the images in folder
when you call the filename, you can combine it with src from and get the content