/** * サムネイルを作成 * * @return string|bool|PEAR_Error * サムネイルを生成・保存に成功したとき、サムネイルのパス * テンポラリ・サムネイルの生成に成功したとき、true * 失敗したとき PEAR_Error */ public function convert($size, $md5, $mime, $width, $height, $force = false) { // 画像 if (!empty($this->intermd) && file_exists($this->intermd)) { $src = realpath($this->intermd); $csize = getimagesize($this->intermd); $width = $csize[0]; $height = $csize[1]; } else { $src = $this->srcPath($size, $md5, $mime, true); } $thumbURL = $this->thumbPath($size, $md5, $mime); $thumb = $this->thumbPath($size, $md5, $mime, true); if ($src == false) { $error = PEAR::raiseError("無効なMIMEタイプ。({$mime})"); return $error; } elseif (!file_exists($src)) { $error = PEAR::raiseError("ソ\ース画像がキャッシュされていません。({$src})"); return $error; } if (!$force && !$this->dynamic && file_exists($thumb)) { return $thumbURL; } $thumbdir = dirname($thumb); if (!is_dir($thumbdir) && !@mkdir($thumbdir)) { $error = PEAR::raiseError("ディレクトリを作成できませんでした。({$thumbdir})"); return $error; } // サイズが既定値以下で回転なし、画像形式が同じならばそのままコピー // --- 携帯で表示できないことがあるので封印、ちゃんとサムネイルをつくる /*if ($this->resize == false && $this->rotate == 0 && $this->type == $this->mimemap[$mime]) { if (@copy($src, $thumb)) { return $thumbURL; } else { $error = PEAR::raiseError("画像をコピーできませんでした。({$src} -> {$thumb})"); return $error; } }*/ // Epegでサムネイルを作成 if ($mime == 'image/jpeg' && $this->epeg) { $dst = $this->dynamic ? '' : $thumb; switch ($this->dpr) { case self::DPR_1_5: $max_width = intval($this->max_width * 1.5); $max_height = intval($this->max_height * 1.5); break; case self::DPR_2_0: $max_width = $this->max_width * 2; $max_height = $this->max_height * 2; break; default: $max_width = $this->max_width; $max_height = $this->max_height; } $result = epeg_thumbnail_create($src, $dst, $max_width, $max_height, $this->quality); if ($result == false) { $error = PEAR::raiseError("サムネイルを作成できませんでした。({$src} -> {$dst})"); return $error; } if ($this->dynamic) { $this->buf = $result; } return $thumbURL; } // 出力サイズを計算 $size = array('w' => $width, 'h' => $height); list($size['tw'], $size['th']) = $this->calc($width, $height, true); if (is_array($this->coord)) { $size['sx'] = $this->coord['x'][0]; $size['sy'] = $this->coord['y'][0]; $size['sw'] = $this->coord['x'][1]; $size['sh'] = $this->coord['y'][1]; } else { $size['sx'] = 0; $size['sy'] = 0; $size['sw'] = $width; $size['sh'] = $height; } // イメージドライバにサムネイル作成処理をさせる $convertorClass = 'Thumbnailer_' . ucfirst(strtolower($this->driver)); $convertor = new $convertorClass(); $convertor->setBgColor($this->bgcolor[0], $this->bgcolor[1], $this->bgcolor[2]); $convertor->setHttp(true); if ($this->type == '.png') { $convertor->setPng(true); } else { $convertor->setQuality($this->quality); } $convertor->setResampling($this->resize); $convertor->setRotation($this->rotate); $convertor->setTrimming($this->trim); if ($this->driver == 'imagemagick') { $convertor->setImageMagickConvertPath($this->magick); } if ($this->dynamic) { $result = $convertor->capture($src, $size); if (is_string($result)) { $this->buf = $result; } } else { $result = $convertor->save($src, $thumb, $size); } if (PEAR::isError($result)) { return $result; } return $thumbURL; }
<?php /** * php_epeg/examples * multiple output */ require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'common.inc.php'; if ($argc != 2 || $argv[1] != 'make') { echo 'Usage:', PHP_EOL; echo ' php ', $argv[0], ' make > thumb2.jpg 2> thumb3.jpg', PHP_EOL; echo ' md5 thumb1.jpg thumb2.jpg thumb3.jpg', PHP_EOL; } else { // write to file epeg_thumbnail_create($sampleimg, 'thumb1.jpg', 160, 160, 80); // print echo epeg_thumbnail_create($sampleimg, '', 160, 160, 80); // write to STDERR epeg_thumbnail_create($sampleimg, 'php://stderr', 160, 160, 80); }
<?php /** * php_epeg/examples * create thumbnail by epeg_thumbnail_create() function */ require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'common.inc.php'; /** * Read from pathname */ var_dump(epeg_thumbnail_create($sampleimg, 'thumb.jpg', 160, 160, 80)); /** * Read from www */ //var_dump(epeg_thumbnail_create('http://example.com/example.jpg', 'thumb-from-web.jpg', 160, 160));
<?php /** * php_epeg/examples * do not resize, only remove marker */ require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'common.inc.php'; var_dump(epeg_thumbnail_create($sampleimg, 'no-marker.jpg', 30000, 30000));