Zend Framework and GD
Moderator: General Moderators
Zend Framework and GD
Hi all,
Had a small question?, Can we draw diagrams using Zend Framework.. Especially setting headers as PNG or JPEG before they are rendered. If so can some body put some small snippet,
Thanks
dude
Had a small question?, Can we draw diagrams using Zend Framework.. Especially setting headers as PNG or JPEG before they are rendered. If so can some body put some small snippet,
Thanks
dude
Hi I don't use any framework but the following draws an image using GD:
pic.php:
Change header to image/png and imagejpeg to imagepng for a png image.
Call it like this:
pic.php:
Code: Select all
<?php
header("Content-type: image/jpeg");
$im = @imagecreatetruecolor(240, 80)
or die("Cannot Initialize new GD image stream");
$black=imagecolorallocate($im, 1, 1, 1);
$bg=imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $bg);
imagestring($im,3,1,1,"Black text on whit background jpg.",$black);
imagejpeg($im);
imagedestroy($im);
exit;
?>Call it like this:
Code: Select all
echo "<img src='pic.php?sid=".rand()."' />";1. Using the @ symbol to suppress is very bad practise.robshanks wrote:Code: Select all
<?php header("Content-type: image/jpeg"); $im = @imagecreatetruecolor(240, 80) or die("Cannot Initialize new GD image stream"); $black=imagecolorallocate($im, 1, 1, 1); $bg=imagecolorallocate($im, 255, 255, 255); imagefill($im, 0, 0, $bg); imagestring($im,3,1,1,"Black text on whit background jpg.",$black); imagejpeg($im); imagedestroy($im); exit; ?>
2. Putting the header() before the image stuff means you're never going to see any errors anyway, browsers will just display a broken image.
3. Why did you give $black a proper variable name but use contracted names for the rest? That's not very consistent.
4. Why did you put an exit at the end?
lol onion2k
In answer to your questions i grabbed a quick and nasty code snippet I had in my junk folder that i used to test image drawing when i had an unrelated problem.
Do i have to check best practice and verify the consistency of my code before posting a quick response to a request for assistance?
In answer to your questions i grabbed a quick and nasty code snippet I had in my junk folder that i used to test image drawing when i had an unrelated problem.
Do i have to check best practice and verify the consistency of my code before posting a quick response to a request for assistance?
Yes. We get all levels of users here, so posting bad code isn't a good thing to do because people take it as verbatim code that they should learn from.robshanks wrote:Do i have to check best practice and verify the consistency of my code before posting a quick response to a request for assistance?
Besides, even quick'n'dirty code still shouldn't have mistakes like error suppression in it.
Thanks you for the help robshanks, but I'm familiar with the method that you proposed.
I'm particular about using Zend framework as header in the Zend framework would do a multiple redirects.
I'm just wondering if someone could provide me with Zend's setting Custom headers and generate a png or jpeg. I've attempted, but there is no output
Thanks
dude81
I'm particular about using Zend framework as header in the Zend framework would do a multiple redirects.
I'm just wondering if someone could provide me with Zend's setting Custom headers and generate a png or jpeg. I've attempted, but there is no output
Thanks
dude81
-
cowsonfire
- Forum Newbie
- Posts: 1
- Joined: Wed Aug 08, 2007 12:52 pm
This method works for me, though I'm not sure if this is the preferred method while using the Zend Framework.
Code: Select all
<?php
class ImageController extends Zend_Controller_Action
{
public function preDispatch()
{
// Disable view script autorendering
$this->_helper->viewRenderer->setNoRender();
}
public function indexAction()
{
// Create the image handle
$image_handle = imagecreatetruecolor(230, 100);
if (!$image_handle)
{
// TODO: Handle the error
return;
}
// Allocate the colors
$color_black = imagecolorallocate($image_handle, 0x00, 0x00, 0x00);
$color_background = imagecolorallocate($image_handle, 0xAA, 0xAA, 0xAA);
// Fill the image with the background color
imagefill($image_handle, 0, 0, $color_background);
// Draw some text on the image
imagestring($image_handle, 3, 50, 45, "Generated Image", $color_black);
// Output the image
imagejpeg($image_handle, null, 100);
// Destroy the image handle
imagedestroy($image_handle);
// Set the headers
$this->getResponse()->setHeader('Content-Type', 'image/jpeg');
}
}