This question already has answers here:
Displaying an image created with imagecreatefromstring
(4 answers)
Closed 2 years ago.
I have a PHP page containing a form(method post).
On the submit of the button, I create an image using imagecreatetruecolor and imagesetpixel.
Then, I want to show this picture after the form.
I tried saving the image to a file using imagepng($img, 'test.png'); and echo '<img src="test.png">';, but that doesn't get the new image. How can I achieve that?
You need to determine where your image is being created when you call imagepng(), then ensure your image tag has the correct path to it.
For example:
$root = $_SERVER['DOCUMENT_ROOT'];
imagepng($img, $root . '/images/test.png')
Your image will then be available at:
https://example.com/images/test.png
And you can set your image source accordingly:
<img src="/images/test.png">
Related
This question already has answers here:
Creating a thumbnail from an uploaded image
(9 answers)
Closed 3 years ago.
I want to display images in my search page as a part of echoed content as a result of search query. These images have different resolutions and what's also very important for the next part they are not squarish. For displaying these photos I've initially used something like this:
if ($queryResult > 0){
while ($row = mysqli_fetch_assoc($result)){
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['photo']).'" ">';
echo "<a href='account.php?username=".$row['username']."&id=".$row['id']."' style='text-decoration:none; color:rgb(0,0,0)'>
<div class='article-box'>
<h3>".$row['username']."</h3>
</div>
</a>";
}
}
But sadly the photos displayed were very big so the result wasn't satisfying me. That's why later I set image displaying part like this:
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['photo']).'" width="150" height="150">';
Again it didn't give me good results. Although the images were then finally small, the non-squarish images were turned into squares what caused them to be deformed. How to display images nicely, I mean to keep them small or of exact size but not to deform them.
Just remove one of the arguments and the browser will scale the image and keep aspect ratio.
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['photo']).'" width="150"';
Or set the height. But not both.
You need to make image thumbs when they are uploaded, not when they are displayed, it ll load faster.
Take a look at this script :
https://pqina.nl/blog/creating-thumbnails-with-php/
Using this script you can now for example generate square 160 by 160 pixel thumbnails with the following command :
createThumbnail('profile.jpg', 'profile_thumb.jpg', 160);
then call profile_thumb.jpg instead of original image
I have this line of code to echo an image (thumb)
printf('<img src="./uploads/designments/%s/thumbs/80x80">',$item['designment_id']);
On the page it will show the broken image icon. and if I look in the elements I see the following path: /uploads/designments/71/thumbs/80x80"
If I look in the ftp to the corresponding file name I see here the path is:
/uploads/designments/71/thumbs/80x80/calculator_wheat_0.jpg
So what do I have to add to my code to get the corresponding filename in the URL?
You closed off the image tag a little too early. Try:
printf('< img src="./uploads/designments/%s/thumbs/80x80/',$item['designment_id'],'">');
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 7 years ago.
I changed my *.html file into *.php. It seemed okay, but after I click the button for image it doesn't show the picture. IN *.html file it was working.
My code:
<img src="C:\xampp\htdocs\Bella Cristina/diamond.jpg" alt="Diamond Room" style="width:200px;height:200px;">
Firstly, you have to make sure that the image exist in your image folder. Insert the name of the folder and the image into a field in your table in the database.
To display it write thees lines of PHP code:
// this will display the image with the size reduced or increased by 180px by 180px
echo '<img src="'.$row['field'].'" '.'width="180px"'.' '.'height="180px"'.'>';
?>
Note: the $connection much be existing in your connection string that is connecting the database
This question already has answers here:
Php : Convert a blob into an image file
(4 answers)
Closed 8 years ago.
I have queried a BLOB my mysql and using the following code I can display the image:
<?php
header("Content-type: image/jpeg");
echo $row['image'];
?>
However, my HTML code below will not display. When i take away this php code, it displays. How can I display the image without preventing my HTML from displaying?
Create a new file, image.php then place the above code in that file.
Then where you want to show your image/html do:
<img src="image.php" />
... rest of your html
For future reference, the same thing holds true when you are creating Javascript/CSS files via PHP.
If you place your php code in a file called image.php, then you can try this
<img src="<?php include 'image.php'; ?>" />
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Want to render an image without saving it to disk using PHP GD libs
The image cannot be displayed because it contains errors.
I'd like to call a function that make a image trought the img src tag. Is it possible?
I mean : instead of call <img src="path/file.php" /> I'd like to do somethings like <img src="function()" />
PHP is server side; It can generate either a base64_encoded image result which can be placed as an image, or you can point to a php script that will generate an image. But, regarding client side, it won't work.
So, you could do the following:
// the browser will make a call to your generator to render an image back
echo '<img src="myimagegenerator.php" />';
// src will be something like "data:image/png;base64,..."
echo '<img src="'.generateImage().'" />';
In HTML5 you can put the base64 encoded image source in an image tag. You will just need a function to return that.
function getImage($file){
return 'data:image/gif;base64,' . base64_encode(file_get_contents($file));
}
Your img tag:
<img src="<? echo getImage('path-to-image.gif'); ?>" />
No.
However, you can use the URL "thispage.php?makeimage=1" and call the function and output an image if $_GET['makeimage'] contains 1.