Zend Framework and GD

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Zend Framework and GD

Post 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
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Post 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()."' />";
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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?
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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
cowsonfire
Forum Newbie
Posts: 1
Joined: Wed Aug 08, 2007 12:52 pm

Post 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');
	}
}
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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
Post Reply