I've got this strange situation going on. Imagejpeg is only working when I define a filename to which the image is going to be saved. The problem is that I don't want to save the image, I just want to show it.
This is my code:
header('Content-Type: image/jpeg');
$img = imagecreatefromjpeg("./_test/test.jpg");
imagejpeg($img, null, 100);
That doesn't work, but if I just change the last line to:
imagejpeg($img, "./_test/blah.jpg", 100);
The image is being saved as it should be.
Doesn anyone have experiance with this problem and knows a solution?
Is your image script file stored as UTF8 instead of ANSI? If so, then that might be your problem. See this answer to an earlier similar question:
https://stackoverflow.com/a/3170007/123749
Try this code to display the image, also you need image headers like so header('Content-type: image/png');
imagejpeg($img);
Related
This seems like a SUPER easy question for people who knows PHP but I can't figure out how to make this work (probably because I know VERY little about PHP). Most question related to this asks how to output an image to either browser or disk, but I wanna do both. See example below:
<?php
header('Content-type: image/png');
$filename = "my_written_image.png";
$png_image = imagecreatefrompng('some_fancy_image.png');
imagepng($png_image, $filename);
imagepng($png_image);
?>
This won't work. The image won't show in the browser, but it will work if I remove this line: imagepng($png_image, $filename);
I'm guessing one can only "output" one thing using imagepng but how will I accomplish the above?
You should use the following code:
<?php
header('Content-type: image/png');
$filename = "my_written_image.png";
$png_image = imagecreatefrompng('some_fancy_image.png');
file_put_contents($filename, $image);
imagepng($png_image, $filename);
?>
My server is not turned on so I can't test the code but I think this should work.
Okay it seems my original solution actually did work. The problem was I was using (in my real code): if(readfile("myfile.png")) and this will return false if it's not found, but it will also throw an error if #readfile() is not used (note the #). The docs said:
an error message is printed
I didn't think this would cause the whole thing to fail. Sorry for my stupidness.
I am trying to get a image from a URL, and then save it to the server. It is a simple task, yeah, but this image has transparency... The thing is, when i save that file to my PC or when I try to get and save it via PHP to my server, then I get the same result: the image gets all messed up with its transparency set to black. The URL of the image is:
http://cellufun.com/p/avatarimage/agb.aspx?i=Body|M04001|Shirt|M10020|
Another weird thing is my browser says the image is MIME Type image/png but I can't use imagecreatefrompng(); because I get the "image not a valid PNG" error...
Here is my code (I did try other solutions, this is what I tryed the last time):
<?php
$result=file_get_contents("http://cellufun.com/p/avatarimage/agb.aspx?i=Body|F04001|Shirt|F10020|Pants|MO55|");
$img = imageCreateFromstring($result);
imageAlphaBlending($img, true);
imageSaveAlpha($img, true);
imagecolortransparent($img);
header("Content-type: image/png");
imagepng($img);
?>
Oh, and I just tryed this image against the copy() function, and still get the same result... It looks like the image is smaller in size then the original image...
Just tryed this:
file_put_contents("files/test.png",file_get_contents($result));
and still not working... so it has something to do with the image itself, because whatever i try to get the data, it does not work.
Try this code (changes at lines 2-4):
<?php
$img = imageCreateFromgif("http://cellufun.com/p/avatarimage/agb.aspx?i=Body|M04001|Shirt|M10020|");
$index = imagecolorexact($img, 0, 0, 0);
imagecolortransparent($img, $index);
header("Content-type: image/png");
imagepng($img);
?>
It works for me although it's a little trick ;) ..and I don't know exactly why the quality is worse..
PS: The main problem I see is that the image at the url you provided is not really a png.
I am simply trying to return an PNG image through PHP, but I have a problem with the transparency not showing right. (Basically one PHP file will be capable of returning any of my images.)
I use simple code to return the image:
<?php
$im = imagecreatefrompng("images/fakehairsalon.png");
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
The original image looks like this:
And the one returned via PHP (and that piece of code) looks like this:
Is there anything I can do to prevent this and make the image come through normal?
As explained in a user comment, you must do this:
<?php
$im = imagecreatefrompng("borrame.png");
header('Content-Type: image/png');
imagealphablending($im, true); // setting alpha blending on
imagesavealpha($im, true); // save alphablending setting (important)
imagepng($im);
imagedestroy($im);
?>
Update: This answer was assuming that your code is an except from a bigger script you are using to do on-the-fly image manipulation.
If you don't want to change the original file, this is a plain waste of memory and CPU cycles. You can use file system functions to read it as a regular file, such as readfile().
It's also worth noting that using PHP to deliver a file only makes sense if you want to do something else as well, such as:
Restricting access to the file
Keeping a counter
I have a online.png and a offline.png
My idea is to do serverstatus.php?sid=1
then have it check if the server is online or not, i know how to run the checks for that.
Then have it return the online or offline image with the mime type of png but really a php file and show up like a image. No text or anything added to it. just the original online or offline png. So it can be used for img tags.
From PHP documentation at http://php.net/manual/en/function.imagepng.php
<?php
$im = imagecreatefrompng("test.png");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
You can use the imagecreatefrompng function
I have writen a small script to go in a Facebook App that can filter images for you. I am having trouble with the GRAYSCALE filter It seems to only display what I think is byte code for the image, instead of the image. I think this may have something to do with the headers and content type. I need to display the image filtered by PHP with this code:
header("content-type: image/jpeg");
$image = imagecreatefromjpeg("http://majik.zbrowntechnology.info/upload/zbt_1794056140.jpg");
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagepng($image);
imagedestroy($image, 'test.jpg');
on an HTML page. Any ideas?
You set Content-Type to image/jpeg but send a PNG image.
header("Content-Type: image/jpeg");
imagejpeg($image);
This should work.
BTW: imagedestroy() only has one argument
The script works fine for me, I see the grayscaled image(even with the wrong parts listed above)
If you see there the source I would guess at first, that there is any output before the headers can be sent.
Set error_reporting to E_ALL, so you can see if and where there is some unintended output.