PHP Classes

Use addFrame to pass the image directly?

Recommend this page to a friend!

      Move Me GIF  >  All threads  >  Use addFrame to pass the image directly?  >  (Un) Subscribe thread alerts  
Subject:Use addFrame to pass the image directly?
Summary:A small change that simplifies the usage (IMO)
Messages:3
Author:André Dias
Date:2016-10-22 02:21:42
 

  1. Use addFrame to pass the image directly?   Reply   Report abuse  
Picture of André Dias André Dias - 2016-10-22 02:21:42
I have done a small change to the code to simplify its usage by passing the frame data direct into addFrame, I understand that it may be mixing different levels of classes (FileImageCanvas is now required inside GifBuilder) and your code is well structured but as I'm not an OO programmer I don't see any bad in it ;-)

Now I can either:

$builder->addFrame(__DIR__ . '/horse/' . $i . '.png')

Or:

$builder->addFrame(new FileImageCanvas(__DIR__ . '/horse/' . $i . '.png'))

This is just an idea and since I made the change in my downloaded version I'm sharing the change.

I don't know how to format code here so I also added the change here to make it easier to see:
pastebin.com/AtDpEVfG

Unformatted version here:
public function addFrame($data = null)
{
$frame = new Frame();

if (gettype($data) == 'object' && get_class($data) == 'movemegif\domain\FileImageCanvas')
$frame->setCanvas($data);
else if (is_string($data) && file_exists($data))
$frame->setCanvas(new FileImageCanvas($data));
else if ($data != null)
throw new MovemegifException('Unknown datatype. Use either a path or a Canvas object.');

$this->extensions[] = $frame;
return $frame;
}

  2. Re: Use addFrame to pass the image directly?   Reply   Report abuse  
Picture of Patrick Van Bergen Patrick Van Bergen - 2016-10-22 06:59:41 - In reply to message 1 from André Dias
Why not create separate functions for these variants (i.e. addFileFrame($path))? That way you don't have to mix types in your function arguments.

  3. Re: Use addFrame to pass the image directly?   Reply   Report abuse  
Picture of André Dias André Dias - 2016-10-22 13:43:59 - In reply to message 2 from Patrick Van Bergen
You are right, since the code is static there shouldn't be a need for the function to guess the file type and it would make it more organized. Thanks.