Page 1 of 1

Zend Framework and GD

Posted: Tue Aug 07, 2007 1:33 am
by dude81
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

Posted: Tue Aug 07, 2007 6:47 am
by robshanks
Hi I don't use any framework but the following draws an image using GD:
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;
?>
Change header to image/png and imagejpeg to imagepng for a png image.
Call it like this:

Code: Select all

echo "<img src='pic.php?sid=".rand()."' />";

Posted: Tue Aug 07, 2007 6:56 am
by onion2k
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;
?>
1. Using the @ symbol to suppress is very bad practise.
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?

Posted: Tue Aug 07, 2007 7:01 am
by robshanks
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?

Posted: Tue Aug 07, 2007 9:02 am
by onion2k
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?
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.

Besides, even quick'n'dirty code still shouldn't have mistakes like error suppression in it.

Posted: Wed Aug 08, 2007 12:21 am
by dude81
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

Posted: Wed Aug 08, 2007 12:56 pm
by cowsonfire
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');
	}
}

Posted: Thu Aug 09, 2007 12:41 am
by dude81
Yes, this is what I was looking for. Really Great thanks...Ill try it out.

Yes It did work out for me but I didnt needed that predispatch method, I'm using Zend 0.8