- To: <php-windows@xxxxxxxxxxxxx>
- Subject: Yet another question - this one related to dynamically generating/passing images through to output/browser
- From: "Jacob Kruger" <jacob@xxxxxxxxxxxxx>
- Date: Thu, 14 Jun 2012 17:06:40 +0200
Based on this page:
http://webcheatsheet.com/php/dynamic_image_generation.php
The following seems to operate/run, but, although can't 'see' if the image looks right, the windows explorer image preview status bar seems to reckon there's an issue with it, and paint refuses to open the image file..?
<?php
//Send a generated image to the browser
create_image();
exit();
function create_image()
{
//Let's generate a totally random string using md5
$md5 = md5(rand(0,999));
//We don't need a 32 character long string so we trim it down to 5
$pass = substr($md5, 10, 5);
//Set the image width and height
$width = 100;
$height = 20;
//Create the image resource
$image = ImageCreate($width, $height);
//We are making three colors, white, black and gray
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$grey = ImageColorAllocate($image, 204, 204, 204);
//Make the background black
ImageFill($image, 0, 0, $black);
//Add randomly generated string in white to the image
ImageString($image, 3, 30, 3, $pass, $white);
//Throw in some lines to make it a little bit harder for any bots to break
ImageRectangle($image,0,0,$width-1,$height-1,$grey);
imageline($image, 0, $height/2, $width, $height/2, $grey);
imageline($image, $width/2, 0, $width/2, $height, $grey);
//send it as an attachment
header('Content-Disposition: attachment; filename="dynamic.jpg"');
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image);
//Free up resources
ImageDestroy($image);
}
?>
Also tried rather using ImageCreateFromPNG() and ImageCreateFromJPEG functions to just load an image from an existing file, and then output it to the browser, but same issue:
<?php
//Send a generated image to the browser
create_image();
exit();
function create_image()
{
//Create the image resource
$image = ImageCreateFromJPEG("./images/it490.jpg");
//send it as an attachment
header('Content-Disposition: attachment; filename="dynamic.jpg"');
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image);
//Free up resources
ImageDestroy($image);
}
?>
Same thing in terms of image file seeming corrupted, while if rename it to .txt, and then open it in like notepad, it does seem to have binary data/character jumbled up in it..?
Effectivelfy, I actually want to take some template images, add text to them in various fonts/layouts, and then effectively place them in PDF files, and have got some similar code seeming to work already, but wanted a bit more control over image files, retrieval of dimensions, etc., but here's a sample that seems to be working already:
http://blindza.co.za/postcards/pdfVersion.php
Just comes down to that if I want to make a bit more of a dynamic use of different image templates, I might want to do a bit more than just render them, etc.
Thoughts..?
Jacob Kruger
Blind Biker
Skype: BlindZA
'...fate had broken his body, but not his spirit...'
[PHP Home]
[PHP Users]
[PHP Install]
[PHP on Windows]
[Programming PHP]
[Kernel Newbies]
[Rice Cooker]
[Home]
[Yosemite News]
[Yosemite Photos]
[PHP Books]