Downloading an image to client that is stored as a variable - php

I have a fairly basic PHP script used to generate an image by combining several files, using variables stored in the URL, into one. For ease of explanation, here it is:
<?php
$images = array( $_GET['item1'], $_GET['item2'], $_GET['item3'] );
// Allocate new image
$img = imagecreatetruecolor(480, 480);
// Make alpha channels work
imagealphablending($img, true);
imagesavealpha($img, true);
foreach($images as $fn) {
// Load image
$cur = imagecreatefrompng($fn);
imagealphablending($cur, true);
imagesavealpha($cur, true);
// Copy over image
imagecopy($img, $cur, 0, 0, 0, 0, 480, 480);
// Free memory
imagedestroy($cur);
}
header('Content-Type: image/png'); // Comment out this line to see PHP errors
imagepng($img);
?>
No issue with that.
I don't really understand what to do with this now though. I would like to download this to the client's downloads folder but I am unaware of and cannot find any methods for this.
I do not particularly need someone to do this for me, but just a push in the right direction for resources on doing this.
I know how to do this by temporary storing that file as a .png on my server and directing the user to a download link for that but that seems like the long way around this.
Any help please?
-Tim

You should save your code within a php file i.e. image.php. If a user calls this file passing valid arguments like this
www.example.com/image.php?item1=bild1.png&item2=bild2.png&item3=bild3.png
the file will be displayed within the browser (you don't have to save it on server side). To force a download of the image add an additional header:
header('Content-Disposition: Attachment;filename=image.png');

Related

Don't show processed GD PHP image in browser

this is my first question and I'm planning to hang around in this forum. I'm very new to programming since I'm studying but I'm making great progress. So, with this in mind I'll try to be as detailed as possible.
The project I am working with is about creating a png-image using GD PHP. The script recieves data, calculates image WIDTH/HEIGHT according to these. From this data I then print out pixels on the image in different spots. Everything works great, it displays the image and saves it on the server, there's nothing wrong with that. But when I run the script, it outputs the image to the browser. I don't want that. I just want the script to process the image and save it to the server. I have searched plenty but haven't found anything about it.
Code for creating the image: I have to remove some code though, but it's not needed to answer my question. I suspect it's something with the header and imagecreatetruecolor. This is all the data I can give.
<?php
//Some calculations before
// --- START: CREATE IMAGE ---
$png = imagecreatetruecolor($WIDTH, $HEIGHT);
imagesavealpha($png, true);
$trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
imagefill($png, 0, 0, $trans_colour);
//Here's just a loop to print pixels
header("Content-type: image/png");
imagepng($png);
save($png); //Function used for saving
//Erase from memory
imagedestroy($png);
?>

Create image, then pass it back

I'm fairly new to php but I'm trying to send an image to a buffer or some sort of temporary place where I can access later. The script I'm calling merges a bunch of images into one, then displays that image (see it in action here - change query parameters to change image). Here's a piece of my code so you have an idea of how that's happening:
$dest = imagecreatefrompng($img0);
$src12 = imagecreatefrompng($img12);
imagecolortransparent($src12, imagecolorat($src12, 0, 0));
//copy and merge
$src12_x = imagesx($src12);
$src12_y = imagesy($src12);
imagecopymerge($dest, $src12, 0, 0, 0, 0, $src12_x, $src12_y, 100);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
However, this is an external script so I'd like to be able to pull that image from another page. I'm not sure what the best way to do it is... temporarily store it or pass the image back through. The one constraint is that the image has to exist before the content of the parent page is loaded. How can I make this happen?
If I understand correctly what you mean;
1) You want to pull the image from another website and save it to disk, you can do this;
file_put_contents("img.png", file_get_contents("URL-TO-IMAGE"));
2) You want to just display it? As the URL aboves headers are to display an image, you can put it right into an IMG tag.
<img src="URL-TO-IMAGE">

How to get and save THIS transparent image with PHP

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.

PHP issue with imagepng showing broken image

I'm trying to create a dynamic image generator that also caches the images.
Everything works perfectly, except that for some reason when I save an images and try to display it at the same time, it shows a broken image icon (like: http://cl.ly/image/3K1f0R0n1R0U).
Code sample:
// some string parsing junk happens up here, but removing that didn't change
// what I've been having a problem with
header("Content-type: image/png;");
if(file_exists($fullFileName)){ // serve the file if it already exists
ob_clean();
flush();
readfile($fullFileName);
exit;
} else { // create the file if it doesn't exist
$my_img = imagecreate(200, 80);
$background = imagecolorallocatealpha($my_img, 0, 0, 0, 127);
$line_colour = imagecolorallocatealpha($my_img, $color["r"], $color["g"], $color["b"], $color["a"]);
imagesetthickness($my_img, 1);
imageline($my_img, 30, 45, 165, 45, $line_colour);
//////////////////////////////////////////
// the line that's causing my troubles: //
//////////////////////////////////////////
imagepng($my_img, $fullFileName);
imagecolordeallocate($line_color);
imagecolordeallocate($background);
imagedestroy($my_img);
}
So, the images are created just fine, save just fine, and display exactly like I want when I refresh the page (because it grabs the saved file).
If I switch that darn line above to:
imagepng($my_img);
Then the image shows up fine, but it doesn't save (since I'm not telling it to).
I thought that this topic:
PHP echoing jpeg image fails, but writing same image to file works great. Why?
Sounded similar, but removing the trailing white space still didn't change what's happening. Other threads mentioned it could be a problem with other content being sent before the headers, but there is nothing like that in this file.
Any ideas on what could be going wrong? No errors are being logged, and I'm out of ideas.
Thanks!
-Zach
EDIT:
It was explained that I was destroying my image, which I missed. I ended up using this:
header("Content-type: image/png");
if(!file_exists($fullFileName)) {
$my_img = imagecreate($dashWidth + $dashSpace, $height);
$background = imagecolorallocatealpha($my_img, 0, 0, 0, 127);
$line_colour = imagecolorallocatealpha($my_img, $color["r"], $color["g"], $color["b"], $color["a"]);
imagesetthickness($my_img, 1);
imageline($my_img, 0, $height-1, $dashWidth-1, $height-1, $line_colour);
imagepng($my_img, $fullFileName);
imagecolordeallocate($my_img, $line_color);
imagecolordeallocate($my_img, $background);
}
ob_clean();
flush();
readfile($fullFileName);
exit;
you mean
header("Content-type: image/png"); // without ;
and also, you are destroying your image, read again the file
readfile($fullFileName);
because imagepng($my_img, $fullFileName); only saves it.
From the manual, imagepng
Outputs or saves a PNG image from the given image
Note the "or". Try making two calls to imagepng. One call to save it to a file and one call without the filename parameter to output the image to the browser.
Try to open image with you text editor.
Turn off error_reporting.
Show you broken image.

PNG manipulation removes transparency

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

Categories