View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFDEV:Zend Proposal Zone Template}

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

{zone-data:proposer-list}
[Davey Shafik|mailto:davey@php.net]
{zone-data}

{zone-data:revision}
1.0 - 12 November 2006: Initial Version.
{zone-data}

{zone-data:overview}
Zend_Image provides easier image manipulation using GD.
{zone-data}

{zone-data:references}
* [PHPs GD Extension|http://php.net/image]
{zone-data}

{zone-data:requirements}
* This component *will* provide an easy wrapper for common GD operations for Jpeg/Png/Gif images
{zone-data}

{zone-data:dependencies}
* Zend_Exception
{zone-data}

{zone-data:operation}
Uses a factory method to return an image specific class on which to perform modifications
{zone-data}

{zone-data:milestones}
* 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_Image
* Zend_Image_Png
* Zend_Image_Jpeg
* Zend_Image_Gif
* Zend_Image_Exception
{zone-data}

{zone-data:skeletons}
{code}
<?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;

/**
* 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}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>