Create image, then pass it back - php

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">

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);
?>

Downloading an image to client that is stored as a variable

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');

php get parameter reset with headers

I have the following scenario. I have about around 600 pictures. Most of theme have in bottom a logo which should be cut it out because branding issue, but not all of theme.(with ftp and photoshop would be a challange)The approach what I was thinking about I list all of my images form folders and I add to theme a hyperlink which should fire on a method class which cuts out 57px from bottom, because the image listing limited to height and width, after cutting should not be present on the page
the url looks like
cut.php?target=http://example.com/hideit/2012/03/myimage.jpg
I want to reset the get parameter after execution to avoid problems on page refresh while would cut again from that image the defined pixels. I was trying the following
function cutAndsave($jpg){
$folder = explode('/', $jpg);
$path = 'I:\\xampp\\htdocs\\hideit\\'. $folder[4]. '\\'. $folder[5] .'\\'.$folder[6] ;
list($width, $height) = getimagesize($jpg);
$offset_x = 0;
$offset_y = 0;
$new_height = $height - 57;
$new_width = $width;
$image = imagecreatefromjpeg($jpg);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
header('Content-Type: image/jpeg');
imagejpeg($new_image,$path, 90);
header("Location: /cat.php/");
die();
}
but the last header call won't work in my case
Your concrete problem at hand is that you're trying to send an HTTP header after you output an image. HTTP headers can only be sent before any content; they're headers after all.
The bigger problem is that the idea is nonsense.
The client requests a URL, example.com/image.php?id=42.jpg. You may think of this URL as a filename. The image is identified by the filename/URL. Different URL, different image. It also doesn't matter that this is a PHP script and not a physical file on a hard disk somewhere, that detail is irrelevant to the client. The client requests the URL and receives an image in return, that's all that matters. Whether the image is just read as is from a data storage, is resized by a script on the fly, is live drawn by unicorns behind the scenes, that all doesn't matter; it's an implementation detail.
URL request, response. That's the important concept you need to grok for working with web servers.
As such, "resetting URL parameters" is pointless. You can't do that. You can redirect the client to a different URL, but that's a different file/URL then. If you want the client to get the image, you respond with an image whenever the client asks for the URL. If the user refreshes the page so the client requests the URL again, so be it.
If you do not want to do the image cutting again and again, cache the resized file. In code, check:
Oh, somebody's requesting image #42.
Do I have a resized version of image #42 already?
If no, create a resized version and save it somewhere.
Serve the resized image #42.
On top of that you can set HTTP headers that influence the client's caching behavior, so the client will keep the image in its cache and not request it again every time.
In case it's also as simple as not overwriting the original. Always keep the original intact, the cut version should be a copy of it. You can either generate that copy as described above, or when the original is uploaded. But don't change the behavior of a URL depending on whether you have already processed the image or not.
You have an error in line header("Location: /cat.php/");
It should be
header("Location: /cut.php");
if you just want to go back to last page.
If you want to additionally show your image you can do
header("Location: /cut.php?show=$path")
and then do a script in your file which prints an <img> tag.

How to set a specific image to not be saved in cache

I'm building a website which uses PHP GD to create images based on user inputted data.
So for instance I'd have something like this:
$image = imagecreate(125, 125);
$blue = imagecolorallocate($image, 0, 0, 255);
imagepng($image, "1.png");
imagedestroy($image);
header("Location: ../index.php");
To generate the image 1.png and go back to the index of the site.
On the index I'm simply displaying the code if the file exists with some more simple php
<?php if(!file_exists("php/1.png"))
echo ("<center><h2> No panels added!</h2></center>");
else
echo('<img class="p_image" src="php/1.png">');
?>
The problem I'm running into is that the browser will cache the last 1.png generated, even if the user deleted it and made a new one, and display that. Is there any way I can stop this from happening without having to rename the file every time?
Thanks in advance!
Just append a timestamp to the filename. That will essentially make it unique and prevent caching:
echo('<img class="p_image" src="php/1.png?'.time().'">');

Need help with PHP GD imagesetpixel()

$imageurl = "kaptka.gif";
$im = imagecreatefromgif($imageurl);
$b = imagecolorallocate($im, 0, 0, 0);
imagesetpixel($im, 5, 5, $b);
header('Content-Type: image/gif');
imagegif($im, "asd.gif");
kaptka.gif is normal gif image. I want to draw some pixels anywhere on the image. Asd.gif looks normal, but when i open the file it should show me both like asd.gif, but it shows just "IMAGE" text.
You are calling the function imagegif() with a filename. This will write the image to disc. To display the contents, simply call it without the filename: this will pass the contents to the output stream, which means it will be sent to the client's browser. One way to do both is to call it twice, once to save the file, and once to display it.
When saving the file, you can assign the result to a variable $foo=imagegif($im, 'bar.gif') and then check the result to see if the save was successful. A FALSE means it failed.
You say the image saved to the server is OK, so the reason you are getting an "IMAGE text" in your browser is probably because you are sending a PNG header, but no data (because of the way you called imagefig()).

Categories