Imagick is displaying blank images with php - php

I installed imagick and checked to make sure the class exists etc. I can get it to count images in tiffs and other things. However, I cant get it to display an image. I am using the following code :
header('Content-type: image/JPG');
$image = new Imagick(' ur to my jpg here ');
$image->thumbnailImage(100, 100);
echo $image;
But it displalys a blank image. When I look at the HTML code produced I get this:
<html>
<body style="margin: 0px;">
<img style="-webkit-user-select: none" src=" url to this page imagechange.php">
</body>
</html>
Can anyone explain why the image is not being displayed? Above the HTML appears the image src as the PHP page that I am using and not the image.

Related

header('Content-Type: image/jpeg') turns page blank

I'm writing my first PHP code and I'm building a form in which the user uploads an image, the image is save in the directory and the name is saved in the database.
Than I have a page that displays that that data from the database i want it to show to image.
$imgdir = 'images/' . $result['fileName'];
$imgsrc = ImageCreateFromJpeg($imgdir);
header('Content-Type: image/jpeg');
imagejpeg($imgsrc);
the output i get is this(notice the block on top left)
but if remove header('Content-Type: image/jpeg'); i got this output(whats in blue is the dir to the image i echoed to check if correct, and it is).
thanks!
You'd better use a simple html img tag, i guess, no need to overdo things :-)
<img src="<?php echo 'images/' . $result['fileName'] ?>" alt="" />

ImageMagick displays garbled text instead of image

I am trying to use ImageMagick class functions in PHP.
To get familiar with ImageMagick, I wrote a few lines of code in PHP to display a red square in my browser.
Instead of displaying a red square, I get garbled text.
I know ImageMagick is installed because I can use ImageMagick functions to save the red square to a file.
Thanks in advance for any help for this newbie to ImageMagick and StackOverflow!
Here's my PHP code:
$image = new Imagick();
$image->newImage(100, 100, new ImagickPixel('red'));
$image->setImageFormat('png');
$image->writeImage("MyOutput.png");
header('Content-type: image/png');
echo $image; //This causes just raw text to be displayed. :(
echo '<img src=MyOutput.png>'; //Displays a 100x100 red image!
//..So, ImageMagick IS installed.
Here's my full PHP file.
<!DOCTYPE html>
<head>
<title>ImageMagick Test</title>
</head>
<body>
<?php
$image = new Imagick();
$image->newImage(100, 100, new ImagickPixel('red'));
$image->setImageFormat('png');
header('Content-type: image/png');
echo $image;
?>
</body>
</html>
The main problem you have is that you're sending Content-Type: image/png after putting some output. Once you put any output, PHP immediately sends all response headers and proceeds to the body part of response. The default Content-Type is text/html on most servers, so your browser is interpreting image content as HTML, and thats why you're seeing some garbage. It's a bit like opening PNG file in notepad.
See this question for more information about sending response headers
Also you can't return HTML and image content upon one request, so do not put any HTML in your image-generating file. The image is independent file and it has to be returned upon it's own, separate request, containing only headers, image bytes and nothing more.
This should work:
<?php
$image = new Imagick();
$image->newImage(100, 100, new ImagickPixel('red'));
$image->setImageFormat('png');
header('Content-type: image/png');
echo $image;
Note, that this is whole PHP file and you can't send any other output neither from outside of <?php ?> nor by echoing.
If you want to include your image into HTML you need a second file (it may be plain HTML), which should look as follows:
<!DOCTYPE html>
<head>
<title>ImageMagick Test</title>
</head>
<body>
<!-- here we're including our image-generating PHP script -->
<img src="imagickTest.php">
</body>
</html>
Then you can refer either to PHP file if you want to see only image or to HTML file if you want to see image put into HTML context.

Display images like logo in .php file with html img tag

I am very new to PHP where the same issue have been facing. Coding a simple welcome php page with a logo in png or jpg format does not show in both development and production server.
Tried using changing the permissions to the folders and files but nothing results.
Any idea?
to do this, you could simply include a PHP echo code segment in your HTML file. for instance,
<html>
<div id="banner">
<?php echo '<image src="logo.jpg" />'; ?>
</div>
</html>
I've used this code to display my logo:
echo "<img src="https://yangtzesolutions.com/wp-content/uploads/2021/07/logo-150x150.png" <?php imageResize(100,100, 150); ?>>";
It will display image from the source URL: https://yangtzesolutions.com/wp-content/uploads/2021/07/logo-150x150.png

Using header('Content-type: image/png') and echo"<html>" in one .php [closed]

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.

Change PHP GD image output file url name/link

I have this problem, i have this script in php that creates a image on the fly, the problem is that the outputted image on the browser is allright, but i need to change it's name.
Ex: Index.php
<?php $url = "http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33" ?>
<img src="<?php echo $url ?>" />
The image_scrc.php is the file that creates the image, and as you can see i have several data that is passed by the get method.
In the image_scrc.php i have tryed
header('Content-type: image/jpg');
header('Content-Disposition:inline; filename="'.$random_name_jpeg.'"');
but the html link is is always appearing like this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
if i select the image on browser and then select copy image link it copies just like this also.
however, when I save the image it assumes the random_name.jpg, but only on save!
i've tried everything, even htaccess rules but nothing seems to work !!
it's this possible to acomplish? transform this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
to this
http://www.somesite.com/cls/random_name.jpg
i cant have the image on the server side! and must be displayed on the fly
Thanks in advance.

Categories