$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()).
Related
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">
I think this will be an easy answer but I just can't figure it out.
I have a script that generates a pdf using fpdf and fpdi.
If I use a normal image then the pdf generates perfectly but I am trying to use an image generated dynamically by php.
$bc = new PrintBarcode('DARP CODE');
$bc->drawVoucher();
$src = $bc->getVoucher();
// image
$src = $bc->getVoucher();
$pdf->Image($src, 22, 94, 100, 15);
This gives the error:
FPDF error: Image file has no extension and no type was specified: Resource id #16
If I do
$barcode = imagejpeg($src);
$pdf->Image($barcode, 22, 94, 100, 15);
presumably because the imagejpeg is actually outputting the image but the image header is set because if the jpeg header is set the image will display fine so I'm just trying to figure out the correct way of doing this.
If it's easier, I have a separate script which can generate the image, e.g. `printbarcode.php' but I don't know how to get the contents of that script into this function:
$pdf->Image($barcode, 22, 94, 100, 15);
From the error message I would guess that the $pdf->Image() function is expecting $src to be a path to a file. What you are passing seems like a object.
Try saving the image to disk and loading from there. You are almost there with imagejpeg() function - see docs on php.net. Supply a path to save the file
imagejpeg($img, 'myfile.jpg', 90); // 90 denotes high quality
$pdf->Image('myfile.jpg', 22, 94, 100, 15);
...
unlink('myfile.jpg'); // delete after
or alternatively look for a function on the fpdf library that takes image resource as a parameter.
You don't need the image handler PHP uses to manipulate the image. What you need is the actual result of the image manipulation, namely the resulting file. Use imagejpeg($img, '/path/to/some/tmp/dir/file.jpg'); and hand the path over to your $pdf->Image() call. Then after creating the PDF you can safely remove the temporarily stored image, I think.
you have to do like this with the help of php's GD library..
//Create a new image from file or URL
$img_src = imagecreatefromjpeg($src);
// Output the image
imagejpeg($img_src);
// Free up memory
imagedestroy($img_src);
I am trying to generate a PDF with an image that is also generated by php.
Sounds simple enough and I am sure I'm just screwing up the header but I can't seem to find a solution here.
first I generate a PDF:
define('FPDF_FONTPATH','fonts/');
require('scpt/fpdf.php');
class PDF extends FPDF {}
$pdf = new FPDF('P','in',array(8.5,11));
$pdf->SetAutoPageBreak(false,0);
$pdf->SetTextColor(0,0,0);
$pdf->SetDrawColor(0,0,0);
$pdf->SetFont('Helvetica','',12);
$pdf->Image('label.php?imgid=17',0,0,0,0,'PNG');
$pdf->Output('label.pdf','D');
then I generate the PNG in label.php:
if(isset($_GET["imgid"])) {
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
}
This will output: FPDF error: Not a PNG file:....
calling label.php?imgid=17 in the browser however will show me the image just fine...
What am I missing?
EDIT
In document:
Example
// Insert a logo in the top-left corner at 300 dpi
$pdf->Image('logo.png',10,10,-300);
// Insert a dynamic image from a URL
$pdf->Image('http://chart.googleapis.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World',60,30,90,0,'PNG');
so it SHOULD be possible to include a dynamically generated image without saving it first?!
EDIT #2
I made it work with the library mem_image but the problem remains that this should not throw an error IMO?! So I leave this question open to see if there is indeed something wrong with my script of this turning out to be a bug.
You almost had it, you're missing only a couple things. First you need the full server address in the URL for your Image() call, so instead of
$pdf->Image('label.php?imgid=17',0,0,0,0,'PNG');
You need:
$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');
That will eliminate the FPDF error you were encountering. Then to get FPDF to render your output correctly, you need to add a call to AddPage(), so your PDF generation script would become:
require('scpt/fpdf.php');
$pdf = new FPDF('P','in',array(8.5,11));
$pdf->AddPage();
$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');
$pdf->Output('label.pdf','D');
Of course you don't need the SetTextColor(), SetFont(), etc if you're only including the single image. You didn't need the custom class definition either (I removed the unneeded lines).
Don't forget to substitute www.yourserver.com for the appropriate domain and path for your script.
The argument to the Image function needs to be a file, not a URL or another script. Since you already have your label script, the easiest work around would be to download the image to a temp location and then call $pdf->Image() with that temporary file.
This problem occurred With your file name,Make sure your passing file name is valid
function _parsepngstream($f, $file)
{
// Check signature
if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
$this->Error('Not a PNG file: '.$file);
...
}
View allow_url_fopen directive
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
You need to do only changes that
require('fpdf.php');
and do the
$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');
and run your code...
First, try to check your url with this code
$handle = fopen("http://www.yourserver.com/label.php?imgid=17", "rb");
print_r(fread($handle, 8192));
you with see what the contain in your url
**) make sure your url is not redirect to the login page
much more easy, only put .png in the final of dinamic chain and its all
example:
$pdf->Image('http://www.yourserver.com/label.php? imgid=17.png',0,0,0,0,'PNG');
or
$pdf->Image('../label.php?imgid=17.png',0,0,0,0,'PNG');
the mime type is not recognized in the url why does not appear png, if it's a stupid mistake but it is proven, google does the same by adding the extension with the url & PNG
I am doing an assignment for school where we are to make two php files. First file invokes session, generates a random 5 char string, and saves the string to the session array. The second script generates an image, and takes the string from the first file and incorporates it over the image to make a captcha.
I am having an issue passing the value to the second script. The session variable 'captcha_string' is fully visible in the first script, but is not passed to the second page. I am brand new at this, and frustrated. My understanding is, that as long as I start session, the entire $_SESSION array should be available. When I run the first script, I get a broken image tag, not the captcha i am hoping for. Hope this clears up my problem.
This is what I have done for the first file:
<?php
session_start();
$possible_chars = array_merge(range('A','Z'),range('0','9'));
shuffle($possible_chars);
$string = substr(implode($possible_chars),0,5);
$_SESSION['captcha_string']=$string;
?>
<img src="captcha_generator.php" alt="Weinerdog!" />
and this is the bit from the second file where I try to grab the $string (captcha_string), which is named "captcha_generator.php:
<?php
session_start();
putenv('GDFONTPATH=' . realpath('.'));
header("Content-type: image/png");
//import string for the captcha from $_SESSION
$string = $_SESSION['captcha_string'];
// Build an image resource using an existing image as a starting point.
$backgroundimage = "captcha_wiener.jpg";
$im=imagecreatefromjpeg($backgroundimage);
$colour = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
// Output the string of characters using a true type font.
// Above we set the font path to the current directory, this
// means that arial.ttf font file must be in this directory.
$font = 'arial.ttf';
$angle = rand(-5,5);
imagettftext($im, 120, $angle, 50, 250, $colour, $font, $string);
// Draw some annoying lines across the image.
imagesetthickness($im, 10);
for ($i = 0; $i <3; $i++) {
imageline($im, rand(100,50), rand(150,200), rand(450,550), rand(200,250), $colour);
}
// Output the image as a PNG and the free the used memory.
imagejpeg($im);
imagedestroy($im);
?>
This is, of course, strictly an exercise to make sure we can pass values using session. There is no problem with the rest of the code making the captcha, it has been tested and works.
You're echo-ing some values with a content-type set to image/png, hence either you'll have the error of headers already sent, or, if the text wasn't sent yet (because cached by PHP), you'll have a broken image and you won't be able to see the text.
Don't worry, it has happened to everyone including me :-)
I've installed the GD Library on my Apache just now, and it seems that my script below doesn't work.
I'm trying to add a layer "play.png" to a youtube video thumbnail (http://img.youtube.com/vi/VIDEOID/default.jpg)
I've tried it with many different videoID's but the image doesn't load. There is a message that the graphic couldn't be opened because it contains errors.
I'm opening the file with postimage.php?v=7yV_JtFnIwo
http://img.youtube.com/vi/7yV_JtFnIwo/default.jpg opens correctly too...
Does anyone know where the issue could be?
Thanks in advance!
<?php
// The header line informs the server of what to send the output
// as. In this case, the server will see the output as a .png
// image and send it as such
header ("Content-type: image/png");
// Defining the background image. Optionally, a .jpg image could
// could be used using imagecreatefromjpeg, but I personally
// prefer working with png
$background = imagecreatefromjpeg("http://img.youtube.com/vi/".$_GET['v']."/default.jpg");
// Defining the overlay image to be added or combined.
$insert = imagecreatefrompng("play.png");
// Select the first pixel of the overlay image (at 0,0) and use
// it's color to define the transparent color
imagecolortransparent($insert,imagecolorat($insert,0,0));
// Get overlay image width and hight for later use
$insert_x = imagesx($insert);
$insert_y = imagesy($insert);
// Combine the images into a single output image. Some people
// prefer to use the imagecopy() function, but more often than
// not, it sometimes does not work. (could be a bug)
imagecopymerge($background,$insert,0,0,0,0,$insert_x,$insert_y,100);
// Output the results as a png image, to be sent to viewer's
// browser. The results can be displayed within an HTML document
// as an image tag or background image for the document, tables,
// or anywhere an image URL may be acceptable.
imagepng($background,"",100);
?>
Do not close (avoid whitespaces or newslines) your script with ?> and use NULL instead "".
imagepng($background, NULL);
Then, in imagepng the quality parameter is between 0 and 9, as in http://it.php.net/manual/en/function.imagepng.php.