Version 1 by Davey Shafik
on Nov 12, 2006 21:55.

compared with
Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (28)

View Page History
{info}

{note:title=Under Construction}
This proposal is under construction and is not ready for review.
{note}

{zone-template-instance:ZFDEV:Zend Proposal Zone Template}

{zone-data:component-name}
Zend_Something_Or_Other Zend_Image
{zone-data}

{zone-data:proposer-list}
[My E-mail Address|mailto:noreply@zend.com]
[Davey Shafik|mailto:davey@php.net]
{zone-data}

{zone-data:revision}
1.1 - 1 August 2006: Updated from community comments.
1.0 - 12 November 2006: Initial Version.
{zone-data}

{zone-data:overview}
Zend_Magic is a simple component that reads my mind and generates code dynamically to do what I want.
Zend_Image provides easier image manipulation using GD.
{zone-data}

{zone-data:references}
* [Harry Houdini Wikipedia Entry|http://en.wikipedia.org/wiki/Harry_Houdini]
* [MagicTricks.com|http://www.magictricks.com/]
* [PHPs GD Extension|http://php.net/image]
{zone-data}

{zone-data:requirements}
Most requirements take the form of "foo will do ...." or "foo will not support ...", although different words and sentence structure might be used. Adding functionality to your proposal is requirements creep (bad), unless listed below. Discuss major changes with your team first, and then open a "feature improvement" issue against this component.

* This component *will* correctly reads a developers mind for intent and generate the right configuration file.
* The generated config file *will not* support XML, but will provide an extension point in the API.
* This component *will* use no more memory than twice the size of all data it contains.
* This component *will* include a factory method.
* This component *will not* allow subclassing. (i.e. when reviewed, we expect to see "final" keyword in code)
* This component *will* only generate data exports strictly complying with RFC 12345.
* This component *will* validate input data against formats supported by ZF component Foo.
* This component *will not* save any data using Zend_Cache or the filesystem. All transient data *will be* saved using Zend_Session.
* This component *will* provide an easy wrapper for common GD operations for Jpeg/Png/Gif images
{zone-data}


{zone-data:operation}
The component is instantiated with a mind-link that ...
Uses a factory method to return an image specific class on which to perform modifications
{zone-data}

{zone-data:milestones}
Describe some intermediate state of this component in terms of design notes, additional material added to this page, and / code. Note any significant dependencies here, such as, "Milestone #3 can not be completed until feature Foo has been added to ZF component XYZ." Milestones will be required for acceptance of future proposals. They are not hard, and many times you will only need to think of the first three below.
* Milestone 1: [design notes will be published here|http://framework.zend.com/wiki/x/sg]
* Milestone 2: Working prototype checked into the incubator supporting use cases #1, #2, ...
* Milestone 3: Working prototype checked into the incubator supporting use cases #3 and #4.
* Milestone 4: Unit tests exist, work, and are checked into SVN.
* Milestone 5: Initial documentation exists.

If a milestone is already done, begin the description with "\[DONE\]", like this:
* Milestone #: \[DONE\] Unit tests ...
* Milestone 1: Working prototype checked into the incubator
* Milestone 2: Unit Tests & Documentation
* Milestone 3: Release in trunk
{zone-data}

{zone-data:class-list}
* Zend_Magic_Exception Zend_Image
* Zend_Magic (factory class)
* Zend_Magic_MindProbe Zend_Image_Png
* Zend_Magic_MindProbe_Intent Zend_Image_Jpeg
* Zend_Magic_Action Zend_Image_Gif
* Zend_Magic_CodeGen Zend_Image_Exception
{zone-data}

{zone-data:use-cases}
||UC-01||

... (see good use cases book)
{zone-data}

{zone-data:skeletons}
{code}
class Zend_Magic_Exception extends Zend_Exception {}
<?php
class Zend_Image {
/**
* JPEG Image Type
*/
const IMAGE_TYPE_JPEG = 1;

/**
* JPEG Image Type for those who don't like the extra E
*/
const IMAGE_TYPE_JPG = 1;

/**
* GIF Image Type
*/
const IMAGE_TYPE_GIF = 2;

/**
* PNG Image Type
*/
const IMAGE_TYPE_PNG = 3;

/**
* @var resource Image
*/
private $_image = null;

/**
* @var int Image Type
*/
private $_type = null;

class Zend_Magic {
... /**
* Do not allow direct construction of class
*/
protected function __construct() { }

/**
* Retrieve an Image Type specific object
*
* @param string $file File name
* @param int $type Image File Type
*/
public function factory($file = null, $type = false)
{
if (!$type && is_null($file)) {
throw new Zend_Image_Exception("You must specify a file or image type.");
} else {
if (!is_null($file)) {
$parts = pathinfo($file);
$type = strtolower($parts['extension']);
}
switch ($type) {
case "png":
case self::IMAGE_TYPE_PNG:
require_once 'Zend/Image/Png.php';
$image = new Zend_Image_Png($file);
break;
case "jpeg":
case self::IMAGE_TYPE_JPEG:
require_once 'Zend/Image/Jpeg.php';
$image = new Zend_Image_Jpeg($file);
break;
case 'gif':
case self::IMAGE_TYPE_GIF:
require_once 'Zend/Image/Gif.php';
$image = new Zend_Image_Gif($file);
break;
default:
throw new Zend_Image_Exception("Unknown Image Type");
}
}
}

/**
* Keep the PNG Alpha Channel
*
* @return bool
*/
public function useAlpha()
{
if ($this->_type != IMAGETYPE_PNG) {
return false;
} else {
imagealphablending($this->_image, false);
imagesavealpha($this->_image, true);
return true;
}
}

/**
* Get Image Width in Pixels
*
* @return int
*/
public function getWidth()
{
return imagesx($this->_image);
}

/**
* Get Image Height in Pixels
*
* @return int
*/
public function getHeight()
{
return imagesy($this->_image);
}

/**
* Get Image Width/Height in Pixels
*
* @return array
*/
public function getSize()
{
return array("width" => $this->getWidth(), "height" => $this->getHeight());
}

/**
* Resize Image
*
* @param int $new_width New Width in % or Pixels (i.e. +10% or 100)
* @param int $new_height New Height in % or Pixels (i.e. -15% or 100)
* @param bool $keep_aspect Keep Aspect Ratio if only one of height/width specified
*/
public function resize($new_width, $new_height, $keep_aspect = true)
{
if ((is_null($new_width) || $new_width === false) && !$keep_aspect) {
$new_width = $this->getWidth();
} elseif ($new_width[0] == '-') {
$new_width = intval($new_width) / 100 * $this->getWidth();
$new_width = $this->getWidth() - $new_width;
} elseif ($new_width[0] == '+') {
$new_width = intval($new_width) / 100 * $this->getWidth();
$new_width = $this->getWidth() + $new_width;
}

if ((is_null($new_height) || $new_height === false) && !$keep_aspect) {
$new_height = $this->getHeight();
} elseif ($new_height[0] == '-') {
$new_height = intval($new_height) / 100 * $this->getHeight();
$new_height = $this->getHeight() - $new_height;
} elseif ($new_height[0] == '+') {
$new_height = intval($new_height) / 100 * $this->getHeight();
$new_height = $this->getHeight() + $new_height;
}

if ((is_null($new_width) || $new_width === false) && $keep_aspect) {
$ratio = $this->getHeight() / $new_height;
$new_width = $this->getWidth() / $ratio;
} elseif ((is_null($new_height) || $new_height === false) && !$keep_aspect) {
$ratio = $this->getWidth() / $new_height;
$new_height = $this->getHeight() / $ratio;
}

$new_width = floor($new_width);
$new_heigt = floor($new_height);

$tmp = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($tmp, $this->_image, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(), $this->getHeight());
$this->_image = $tmp;
}

public function rotate($degrees)
{
if ($degrees[1] == "-") {
$degrees = 360 - intval(substr($degrees, 1));
} else {
$degrees = intval($degrees);
}
}

public function flipHorizontal()
{
$tmp = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($tmp, $this->_image, 0, 0, ($this->getWidth()-1), 0, $this->getWidth(), $this->getHeight(), 0-$this->getWidth(), $this->getHeight());
$this->_image = $tmp;
}

public function flipVertical()
{
$tmp = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($tmp, $this->_image, 0, 0, 0, ($this->getHeight()-1), $this->getWidth(), $this->getHeight(), $this->getWidth(), 0-$this->getHeight());
$this->_image = $tmp;
}

public function negate()
{
imagefilter($this->_image, IMG_FILTER_NEGATE);
}

public function grayscale()
{
imagefilter($this->_image, IMG_FILTER_GRAYSCALE);
}

public function brightness($level)
{
imagefilter($this->_image, IMG_FILTER_BRIGHTNESS, $level);
}

public function contrast($level)
{
imagefilter($this->_image, IMG_FILTER_CONTRAST, $level);
}

public function colorize($red, $green, $blue)
{
imagefilter($this->_image, IMG_FILTER_COLORIZE, $red, $green, $blue);
}

public function edgeDetect()
{
imagefilter($this->_image, IMG_FILTER_EDGEDETECT);
}

public function emboss()
{
imagefilter($this->_image, IMG_FILTER_EMBOSS)
}

public function gaussianBlur()
{
imagefilter($this->_image, IMG_FILTER_GAUSSIAN_BLUR);
}

public function selectiveBlur()
{
imagefilter($this->_image, IMG_FILTER_SELECTIVE_BLUR);
}

public function sketchy()
{
imagefilter($this->_image, IMG_FILTER_MEAN_REMOVAL);
}

public function smooth($level)
{
imagefilter($this->_image, IMG_FILTER_SMOOTH);
}

public function getImageResource()
{
return $this->_image;
}

public function convert($type)
{
switch ($type) {
case "png":
case self::IMAGE_TYPE_PNG:
require_once 'Zend/Image/Png.php';
$image = new Zend_Image_Png($this->_image);
break;
case "jpeg":
case self::IMAGE_TYPE_JPEG:
require_once 'Zend/Image/Jpeg.php';
$image = new Zend_Image_Jpeg($this->_image);
break;
case 'gif':
case self::IMAGE_TYPE_GIF:
require_once 'Zend/Image/Gif.php';
$image = new Zend_Image_Gif($this->_image);
break;
default:
throw new Zend_Image_Exception("Unknown Image Type");
}
}
}

class Zend_Image_(Gif|Png|Jpeg) {
public function __construct($image) { }

public function save($filename = null) { }
}

class Zend_Image_Exception extends Zend_Exception { }
{code}
{zone-data}