ImageMagick素晴らしいけど、入ってなかったりするので。それにImageMagickは以前やった。
cf. ImageMagicで影付き文字を入れるRuby scriptを増強しました http://c4se.hatenablog.com/entry/2012/12/02/005226
GD module (libgd) を使って画像を生成する。GDが使へるかは、phpinfo()
とgd_info()
を見れば判る。
cf. PHP: GD および Image 関数 - Manual http://www.php.net/manual/ja/ref.image.php
randomな四角を十個描く。
<?php // license: Public Domain $type = empty($_GET['type']) ? 'png' : $_GET['type']; $image = new ArtImage(600, 600); foreach(range(1, 10) as $i) $image->fillRandomRect(200); $image->echo($type); class ArtImage { /** @type integer */ public $width; /** @type integer */ public $height; /** @type integer */ private $image; /** * @param integer $width * @param integer $height */ function __construct($width, $height) { $this->width = $width; $this->height = $height; $this->image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($this->image, 255, 255, 255); imagefilledrectangle($this->image, 0, 0, $width, $height, $white); imagecolordeallocate($this->image, $white); } /** * @param integer $max_size */ public function fillRandomRect($max_size) { $color = imagecolorallocate($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); $x = mt_rand(50, $this->width - 50); $y = mt_rand(50, $this->height - 50); imagefilledrectangle($this->image, $x, $y, $x + mt_rand(1, $max_size), $y + mt_rand(1, $max_size), $color); imagecolordeallocate($this->image, $color); } /** * @param string $type */ public function echo($type) { switch ($type) { case 'jpeg': $this->echoJpeg(); break; case 'png': default: $this->echoPng(); } } private function echoJpeg() { header('Content-Type: image/jpeg'); imagejpeg($this->image, null, 95); imagedestroy($this->image); } private function echoPng() { header('Content-Type: image/png'); imagepng($this->image, null, 9); imagedestroy($this->image); } } // vim:set et sw=4 sts=4 ff=unix:
その後
<?php // license: Public Domain /** * http://www.php.net/manual/ja/function.mb-split.php#80046 * * @param string $string * @return array */ function mbStringToArray($string) { $strlen = mb_strlen($string); while ($strlen) { $array[] = mb_substr($string, 0, 1, 'UTF-8'); $string = mb_substr($string, 1, $strlen, 'UTF-8'); $strlen = mb_strlen($string); } return $array; } class ArtImage { /** * @param string $path * @return ArtImage */ public static function fromJpeg($path) { $size = getimagesize($path); $image = new self($size[0], $size[1]); $image->image = imagecreatefromjpeg($path); return $image; } /** * @param string $path * @return ArtImage */ public static function fromPng($path) { $size = getimagesize($path); $image = new self($size[0], $size[1]); $image->image = imagecreatefrompng($path); return $image; } /** @type integer */ public $width; /** @type integer */ public $height; /** @type integer */ public $image; /** * @param integer $width * @param integer $height */ function __construct($width, $height) { $this->width = $width; $this->height = $height; $this->image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($this->image, 255, 255, 255); imagefilledrectangle($this->image, 0, 0, $width, $height, $white); imagecolordeallocate($this->image, $white); } /** * @param integer $x * @param integer $y * @param integer $width * @param integer $height * @param integer $red * @param integer $green * @param integer $blue */ public function fillRect($x, $y, $width, $height, $red, $green, $blue) { $color = imagecolorallocate($this->image, $red, $green, $blue); imagefilledrectangle($this->image, $x, $y, $x + $width, $y + $height, $color); imagecolordeallocate($this->image, $color); } /** * @param integer $max_size */ public function fillRandomRect($max_size) { $x = mt_rand(1, $this->width - 1); $y = mt_rand(1, $this->height - 1); $width = mt_rand(1, $max_size); $height = mt_rand(1, $max_size); $red = mt_rand(0, 255); $green = mt_rand(0, 255); $blue = mt_rand(0, 255); $this->fillRect($x, $y, $width, $height, $red, $green, $blue); } /** * @param string $text * @param integer $size * @param integer $x * @param integer $y * @param integer $red * @param integer $green * @param integer $blue * @param string $font_path */ public function fillTextUp($text, $size, $x, $y, $red, $green, $blue, $font_path) { $color = imagecolorallocate($this->image, $red, $green, $blue); $y -= 6; foreach(mbStringToArray($text) as $char) { $box = imagettfbbox($size, 0, $font_path, $char); $y += $box[2] + 6; imagettftext($this->image, $size, 0, $x, $y, $color, $font_path, $char); } imagecolordeallocate($this->image, $color); } /** * @param ArtImage $image * @param integer $x * @param integer $y */ public function drawImage($image, $x, $y) { imagecopy($this->image, $image->image, $x, $y, 0, 0, $image->width, $image->height); } /** * @param float $deg */ public function rotate($deg) { $transparent = imagecolorallocatealpha($this->image, 0, 0, 0, 127); $image = imagerotate($this->image, $deg, $transparent); imagecolordeallocate($this->image, $transparent); $this->image = $image; $this->width = imagesx($this->image); $this->height = imagesy($this->image); } /** * @param float $ratio_x * @param float $ratio_y */ public function scale($ratio_x, $ratio_y) { $image = new ArtImage($this->width * $ratio_x, $this->height * $ratio_y); imagecopyresampled($image->image, $this->image, 0, 0, 0, 0, $image->width, $image->height, $this->width, $this->height); $this->image = $image->image; $this->width = $image->width; $this->height = $image->height; } public function echoJpeg() { header('Content-Type: image/jpeg'); imagejpeg($this->image, null, 95); imagedestroy($this->image); } public function echoPng() { header('Content-Type: image/png'); imagepng($this->image, null, 9); imagedestroy($this->image); } public function echoWebp() { header('Content-Type: image/webp'); imagewebp($this->image, null); imagedestroy($this->image); } } // vim:set et sw=4 sts=4 ff=unix:
<?php require_once 'lib/ArtImage.php'; $image = ArtImage::fromPng(__DIR__ . '/img/756078151.png'); $fukidashi = ArtImage::fromPng(__DIR__ . '/img/fukidashi.png'); $image->scale(0.43, 0.43); $image->drawImage($fukidashi, 80, 30); $image->fillTextUp('ももんがに', 10, 145, 50, 0, 0, 0, __DIR__ . '/font/TakaoExGothic.ttf'); $image->fillTextUp('救いは無い', 10, 125, 50, 0, 0, 0, __DIR__ . '/font/TakaoExGothic.ttf'); $image->echoPng(); // vim:set et sw=4 sts=4 ff=unix:
ももんがに救いは無い(〃l _ l)