Skip to content

Instantly share code, notes, and snippets.

@riaf
Created February 11, 2009 04:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riaf/61826 to your computer and use it in GitHub Desktop.
Save riaf/61826 to your computer and use it in GitHub Desktop.
<?php
/**
* iPhoneのアイコンみたいな画像を作るよ
*
* @author riaf<riafweb@gmail.com>
*/
if(!class_exists('Imagick')){
throw new Exception('Imagick required.');
return -1;
}
class iPhoneIconizer
{
private $image;
public static function auto($filename, $output=null){
$iphonizer = new iPhoneIconizer();
return $iphonizer->set($filename)->crystal()->round()->output($output);
}
public function set($filename, $width=58, $height=58){
$image = new Imagick($filename);
$image->setImageMatte(true);
$image->cropThumbnailImage($width, $height);
$this->image = $image;
return $this;
}
public function round($rounding=10, $edge=5){
return ($edge !== false)? $this->_edgedRound($rounding, $edge): $this->_normalRound($rounding);
}
private function _normalRound($rounding){
return $this;
}
private function _edgedRound($rounding, $edge){
$w = $this->image->getImageWidth();
$h = $this->image->getImageHeight();
// outer
$o = $this->image->clone();
$o = $o->fxImage('p*1.3');
$omask = new Imagick();
$omask->newImage($w, $h, 'none');
$omask_draw = new ImagickDraw();
$omask_draw->setFillColor('#ffffff');
$omask_draw->setStrokeColor('#ffffff');
$omask_draw->setStrokeWidth($edge);
$omask_draw->roundRectangle($edge, $edge, $w-$edge-1, $h-$edge-1, $rounding, $rounding);
$omask->drawImage($omask_draw);
// $omask_draw->destroy();
$omask->compositeImage($o, Imagick::COMPOSITE_IN, 0, 0, Imagick::CHANNEL_ALL);
// inner
$i = $this->image->clone();
$imask = new Imagick();
$imask->newImage($w, $h, 'none');
$imask_draw = new ImagickDraw();
$imask_draw->setFillColor('#ffffff');
$imask_draw->setStrokeWidth(0);
$imask_draw->roundRectangle($edge, $edge, $w-$edge-1, $h-$edge-1, $rounding, $rounding);
$imask->drawImage($imask_draw);
// $imask_draw->destroy();
$imask->compositeImage($i, Imagick::COMPOSITE_IN, 0, 0, Imagick::CHANNEL_ALL);
// merge
$omask->compositeImage($imask, Imagick::COMPOSITE_DEFAULT, 0, 0, Imagick::CHANNEL_ALL);
$new = new Imagick();
$new->newImage($w, $h, 'none');
$new->compositeImage($omask, Imagick::COMPOSITE_DEFAULT, 0, 0, Imagick::CHANNEL_ALL);
// $this->image->destroy();
$this->image = $new;
// $i->destroy();
// $o->destroy();
// $imask->destroy();
// $omask->destroy();
return $this;
}
public function crystal(){
$mask = new Imagick();
$mask->newImage($this->image->getImageWidth(), $this->image->getImageHeight(), 'none');
$draw = new ImagickDraw();
$draw->setFillColor('#ffffff');
$draw->circle(
$this->image->getImageWidth()/2,
$this->image->getImageHeight()*-0.5,
$this->image->getImageWidth(),
$this->image->getImageHeight()*0.3
);
$mask->drawImage($draw);
$mask->setImageOpacity(0.1);
$this->image->compositeImage($mask, Imagick::COMPOSITE_DEFAULT, 0, 0, Imagick::CHANNEL_ALL);
return $this;
}
public function output($filename=null){
if(is_null($filename)){
// display
} else {
$this->image->writeImage($filename);
}
return $this;
}
public function __destruct(){
if(is_object($this->image)){
// $this->image->destroy();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment