Class
- Tip&Tech
| [ÇÔ¼ö] ¼¶³×ÀÏ Å¬·¡½º | |||||
| ±Û¾´ÀÌ | ³¯ Â¥ | 11-06-22 00:53 | Á¶ ȸ | 3190 | |
|---|---|---|---|---|---|
| °£ÆíURL |
http://www.phpschool.com/link/tipntech/73861
|
||||
| ´Ù¿î·Îµå1 | Thumbnail-class.zip (195.8K), Down : 121, 2011-06-22 00:53:16 | ||||
| Link1 | http://phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=4¡¦ (246) | ||||
![]()
±«ºÎ±â´ÔÀÇ Äڵ带 ¿øº»À¸·Î »ó´çºÎºÐ ¼öÁ¤ ¹× Ãß°¡ÇÏ¿´½À´Ï´Ù.
°¡±ÞÀû PHP 5.3 ÀÌ»ó ¹öÀü¿¡¼ °¡´ÉÇÒ°É·Î ¿¹»óÇϰí, ÀÌÇÏ ¹öÀü¿¡¼´Â Å×½ºÆ®´Â ¸øÇغýÀ´Ï´Ù. ÁÖ¿ä±â´É - ¼¶³×ÀÏ width, height ÁöÁ¤ °¡´É (µÑ Áß Çϳª¸¸ ÁöÁ¤ÇÒ °æ¿ì ÇØ´ç À̹ÌÁö ºñÀ²¿¡ ¸ÂÃç ¼¶³×ÀÏ Å©±â ¼³Á¤) - Ãà¼Ò ¹æ¹ý ¼³Á¤ (¼¶³×ÀÏ ¾È¿¡ À̹ÌÁö ¸ðµÎ º¸À̰Ô, ¼¶³×ÀÏ Å©±â¿¡ ²ËÂ÷°Ô ¼³Á¤ °¡´É) - Ãâ·Â ¼¶³×ÀÏ ÆÄÀÏÇü½Ä ¼³Á¤ °¡´É (GIF, JPG, PNG) - ¼¶³×ÀÏ Ã³¸® Àü/ÈÄ Äݹé ÇÔ¼ö µî·Ï °¡´É (ÄݹéÇÔ¼ö·Î Å׵θ® ¹× ¿öÅ͸¶Å© µî »ç¿ëÀÚ°¡ ½±°Ô Ãß°¡ °¡´É) - ¼¶³×ÀÏ ÀúÀå °æ·Î¸¦ ½±°Ô ¼³Á¤ °¡´É (Ű¿öµå »ç¿ëÀ¸·Î ¼¶³×ÀÏ °æ·Î¿¡ À̹ÌÁö Å©±â³ª °æ·Î¸¦ ½±°Ô »ç¿ë) »ç¿ë¿¹ sample.php
<?php
require_once 'Thumbnail.class.php';
$watermark = new ThumbnailWatermark;
$sourcefiles = Array(
'sample_1.jpg',
'sample_2.jpg'
);
Thumbnail::setOption('debug', true);
for ($L1 = 0; $L1 < count($sourcefiles); $L1++)
{
// case 1
// Å©±â¸¦ 120x120 À¸·Î ¼³Á¤Çϰí Å©±â ¾È¿¡ À̹ÌÁö°¡ ¸ðµÎ º¸À̵µ·Ï ¼³Á¤
// ¿öÅ͸¶Å©´Â »ç¿ë¾ÈÇÔ
Thumbnail::create($sourcefiles[$L1],
120, 120,
SCALE_SHOW_ALL,
Array(
'savepath' => '%PATH%%FILENAME%_thumb-case-1.%EXT%'
));
// case 2
// Å©±â¸¦ 120x120 À¸·Î ¼³Á¤Çϰí Å©±â ¾È¿¡ À̹ÌÁö°¡ ¸ðµÎ º¸À̵µ·Ï ¼³Á¤
Thumbnail::create($sourcefiles[$L1],
120, 120,
SCALE_SHOW_ALL,
Array(
'preprocess' => Array(&$watermark, 'preprocess'),
'postprocess' => Array(&$watermark, 'postprocess'),
'savepath' => '%PATH%%FILENAME%_thumb-case-2.%EXT%'
));
// case 3
// Å©±â¸¦ 120x120 À¸·Î ¼³Á¤Çϰí À̹ÌÁö°¡ Å©±â¿¡ ²Ë Â÷µµ·Ï ¼³Á¤
Thumbnail::create($sourcefiles[$L1],
120, 120,
SCALE_EXACT_FIT,
Array(
'preprocess' => Array(&$watermark, 'preprocess'),
'postprocess' => Array(&$watermark, 'postprocess'),
'savepath' => '%PATH%%FILENAME%_thumb-case-3.%EXT%'
));
// case 4
// ³ÐÀ̸¸ 120 Çȼ¿·Î °íÁ¤Çϰí, ±âº» ¿É¼ÇÀ» PNG ÆÄÀÏ·Î Ãâ·ÂÇϵµ·Ï ¼³Á¤
Thumbnail::setOption('export', EXPORT_PNG);
Thumbnail::create($sourcefiles[$L1],
120, null,
null,
Array(
'preprocess' => Array(&$watermark, 'preprocess'),
'postprocess' => Array(&$watermark, 'postprocess'),
'savepath' => '%PATH%%FILENAME%_thumb-case-4.%EXT%'
));
Thumbnail::setOption('export', EXPORT_JPG);
}
Thumbnail.class.php
<?php
/**
* ¼¶³×ÀÏ »ý¼º Ŭ·¡½º
* PHPSchool ±«ºÎ±â´ÔÀÇ Äڵ带 ¿øº»À¸·Î »ó´çºÎºÐ ¼öÁ¤ÇÏ¿´½À´Ï´Ù.
*
* @class Thumbnail
* @author ±«ºÎ±â, KAi
* @version 0.2 (2011-06-21)
* @see http://phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=46878
* @modified KAi (2011-06-21)
*
*
* - ÁÖ¿ä±â´É
* - ¼¶³×ÀÏ width, height ÁöÁ¤ °¡´É (µÑ Áß Çϳª¸¸ ÁöÁ¤ÇÒ °æ¿ì ÇØ´ç À̹ÌÁö ºñÀ²¿¡ ¸ÂÃç ¼¶³×ÀÏ Å©±â ¼³Á¤)
* - Ãà¼Ò ¹æ¹ý ¼³Á¤ (¼¶³×ÀÏ ¾È¿¡ À̹ÌÁö ¸ðµÎ º¸À̰Ô, ¼¶³×ÀÏ Å©±â¿¡ ²ËÂ÷°Ô ¼³Á¤ °¡´É)
* - Ãâ·Â ¼¶³×ÀÏ ÆÄÀÏÇü½Ä ¼³Á¤ °¡´É (GIF, JPG, PNG)
* - ¼¶³×ÀÏ Ã³¸® Àü/ÈÄ Äݹé ÇÔ¼ö µî·Ï °¡´É (ÄݹéÇÔ¼ö·Î Å׵θ® ¹× ¿öÅ͸¶Å© µî »ç¿ëÀÚ°¡ ½±°Ô Ãß°¡ °¡´É)
* - ¼¶³×ÀÏ ÀúÀå °æ·Î¸¦ ½±°Ô ¼³Á¤ °¡´É (Ű¿öµå »ç¿ëÀ¸·Î ¼¶³×ÀÏ °æ·Î¿¡ À̹ÌÁö Å©±â³ª °æ·Î¸¦ ½±°Ô »ç¿ë)
*/
define('SCALE_EXACT_FIT', 'exactfit');
define('SCALE_SHOW_ALL', 'showall');
define('EXPORT_JPG', 'jpg');
define('EXPORT_GIF', 'gif');
define('EXPORT_PNG', 'png');
class Thumbnail
{
// {{{ Variables
// ±âº» ¿É¼Ç Á¤º¸
private static $_TdefaultOptions = Array(
'debug' => false,
'export' => 'jpg',
'preprocess' => null,
'postprocess' => null,
'savepath' => '%PATH%%FILENAME%_thumb.%EXT%'
);
// }}}
// {{{ Functions
/**
* ¼¶³×ÀÏ À̹ÌÁö »ý¼º
*
* @param String $filepath ¿øº» ÆÄÀÏÀÌ ÀÖ´Â °æ·Î¸¦ ÁöÁ¤ÇÑ´Ù.
* @param Number $width (optional) ¼¶³×ÀÏ ³ÐÀÌ. width¿Í height µÑ Áß Çϳª´Â ¹Ýµå½Ã ÁöÁ¤µÇ¾î¾ß
* Çϸç,Çϳª°¡ null ÀÎ °æ¿ì ´Ù¸¥ °ª(width ¶Ç´Â height)À» ±âÁØÀ¸·Î À̹ÌÁöÀÇ ºñÀ²¿¡ ¸Â°Ô Å©±â°¡ ¼³Á¤µÈ´Ù.
* @param Number $height (optional) ¼¶³×ÀÏ ³ôÀÌ.
* @param String $scale (optional) Ãà¼Ò ¹æ½Ä. ¸¸¾à, width ¶Ç´Â height °ª Áß Çϳª¶óµµ ÁöÁ¤ÀÌ ¾ÈµÈ
* °æ¿ì scale Àº ÀÚµ¿ÀûÀ¸·Î SCALE_SHOW_ALLÀÌ µÇ¸ç À̹ÌÁö Àüü°¡ Ç¥½Ã µÈ´Ù.
* SCALE_EXACT_FIT, SCALE_SHOW_ALL µÑ Áß Çϳª¸¦ ÁöÁ¤°¡´É Çϰí, width ¹× height °ªÀÌ ÁöÁ¤µÈ °æ¿ì ÇØ´ç ¹æ½Ä¿¡
* ¸Â°Ô Ãà¼Ò°¡ µÇ´Âµ¥, SCALE_EXACT_FIT ÀÇ °æ¿ì ÁöÁ¤µÈ ¼¶³×ÀÏ Å©±â¿¡ À̹ÌÁö°¡ ²ËÂ÷°Ô Ãà¼ÒµÈ´Ù.
* SCALE_SHOW_ALL ÀÇ °æ¿ì ¼¶³×ÀÏ Å©±â ¾È¿¡ ¿øº» À̹ÌÁö°¡ ¸ðµÎ º¸À̵µ·Ï Ãà¼ÒµÈ´Ù. ÀÌ °æ¿ì ¼¶³×ÀÏ¿¡ ¿©¹éÀÌ
* »ý±æ ¼ö ÀÖ´Ù. ±âº»ÀûÀ¸·Î ¿©¹éÀÇ »öÀº °ËÀº»öÀÌÁö¸¸, preprocess ÇÔ¼ö¸¦ ÁöÁ¤ÇÏ¿© ´Ù¸¥»öÀ¸·Î º¯°æ °¡´ÉÇÏ´Ù.
* @param Array $options (optional) ±âŸ ¿É¼Ç. ±âŸ ¿É¼Ç¿¡¼´Â Ãâ·ÂµÉ ¼¶³×ÀÏ ÆÄÀÏ Çü½Ä°ú, ¼¶³×ÀÏ
* Àü/ÈÄ Ã³¸® Äݹé ÇÔ¼ö, ¼¶³×ÀÏ ÀúÀå °æ·Î¸¦ ¼³Á¤ °¡´ÉÇÏ´Ù.
*
* - ¿É¼Ç Ç׸ñ
* $options = Array(
* 'export' => 'jpg',
* 'preprocess' => Array(&$class, 'preprocess'),
* 'postprocess' => 'postprocess',
* 'savepath' => '%PATH%%FILENAME%_thumb.%EXT%'
* );
*
* export ¿¡¼´Â gif, jpg, png ¸¦ ¼±ÅÃÇÒ ¼ö ÀÖ´Ù. (±âº»°ªÀº jpg)
* preprocess, postprocess ¿¡¼´Â ¼¶³×ÀÏ »ý¼º½Ã À̹ÌÁö Ãà¼Ò Àü/ÈÄ¿¡ ÄݹéÇÔ¼ö¸¦ È£ÃâÇÏ¿© ¹è°æ ¹× ¿öÅ͸¶Å©¸¦
* ³Öµµ·Ï ÇÒ ¼ö ÀÖ´Ù. ¼³Á¤¹æ¹ýÀº ¿©Å¸ PHP ÇÔ¼ö È£Ãâ°ú ¸¶Âù°¡Áö·Î, Ŭ·¡½º ¸Þ¼Òµå¸¦ È£ÃâÇÒ °æ¿ì preprocess
* ³»¿ë ó·³ ¹è¿·Î Ŭ·¡½º °´Ã¼¿Í ¸Þ¼Òµå¸¦ ³Ñ°ÜÁÖ¸éµÇ°í, ÀÏ¹Ý ÇÔ¼ö¸¦ È£ÃâÇÒ °æ¿ì postprocess ó·³ ÇÔ¼ö¸íÀ»
* ÀûÀ¸¸é µÈ´Ù.
* savepath ´Â ¼¶³×ÀÏÀÌ ÀúÀåµÈ °æ·Î¸¦ ¼³Á¤ÇÏ´Â °ÍÀ¸·Î ¹Ì¸® ¼³Á¤µÈ Ű¿öµå¸¦ ÅëÇØ ¿øº»À̹ÌÁö °æ·Î Á¤º¸¸¦ ÀÌ¿ë
* ÇÒ ¼ö ÀÖ´Ù. ±âº»°ªÀº ¿øº»À̹ÌÁö °æ·Î µÚ¿¡ _thumb ÀÌ ºÙ´Â°ÍÀ¸·Î ¿øº»À̹ÌÁö¿Í °°Àº Æú´õ¿¡ ÀúÀåÀÌ µÈ´Ù.
* ´Ù¸¥ Æú´õ¿¡ ÀúÀåÀ» ÇÏ·Á¸é %PATH% Ű¿öµå ¾ÕÀ̳ª µÚ¿¡ ´Ù¸¥ °æ·Î¸¦ Àû°Å³ª %PATH% ´ë½Å ´Ù¸¥ °æ·Î¸¦ ÀûÀ¸¸é µÇ¸ç
* ¸¸¾à ÇØ´ç °æ·Î¿¡ Æú´õ°¡ ¾ø´Ù¸é ÀÚµ¿À¸·Î »ý¼º µÈ´Ù.
* ¡Ø Áß¿ä: savepath ¼³Á¤ÇÒ¶§ %EXT% ¿Í °°Àº È®ÀåÀÚ°¡ ºüÁö¸é È®ÀåÀÚ ¾øÀÌ À̹ÌÁö °æ·Î°¡ »ý¼ºµÇ±â ¶§¹®¿¡ ¹®Á¦°¡
* µÉ ¼ö ÀÖ½À´Ï´Ù.
*
* - ¼³Á¤µÈ Ű¿öµå
* %PATH% ¿øº» À̹ÌÁö °æ·Î
* %FILENAME% ¿øº» À̹ÌÁö ÆÄÀϸí (È®ÀåÀÚ Á¦¿Ü)
* %EXT% ¼¶³×ÀÏ Ãâ·Â Çü½Ä È®ÀåÀÚ (¿É¼Ç¿¡¼ export ·Î ¼³Á¤µÈ Çü½ÄÀÇ È®ÀåÀÚ »ç¿ë)
* %THUMB_WIDTH ¼¶³×ÀÏ ³ÐÀÌ
* %THUMB_HEIGHT ¼¶³×ÀÏ ³ôÀÌ
* %IMAGE_WIDTH ¿øº» À̹ÌÁö ³ÐÀÌ
* %IMAGE_HEIGHT ¿øº» À̹ÌÁö ³ôÀÌ
*
* @return String ¼¶³×ÀÏ °æ·Î
*/
public static function create($filepath, $width = null, $height = null, $scale = 'exactfit', $options = null)
{
// ¿øº» À̹ÌÁö°¡ ¾ø´Â °æ¿ì
if ( ! file_exists($filepath))
Thumbnail::raiseError('#Error: Thumbnail::create() : File not found or permission error.'.' at '. __LINE__);
// ¼¶³×ÀÏ Å©±â°¡ À߸ø ÁöÁ¤µÈ °æ¿ì
if ($width <= 1 && $height <= 1)
Thumbnail::raiseError('#Error: Thumbnail::create() : Invalid thumbnail size.'.' at '. __LINE__);
// ½ºÄÉÀÏ ÁöÁ¤ÀÌ ¾ÈµÇ¾î Àְųª Ʋ¸± °æ¿ì ±âº» SCALE_SHOW_ALL À¸·Î ÁöÁ¤
if ( ! $scale || ($scale != SCALE_EXACT_FIT && $scale != SCALE_SHOW_ALL))
$scale = SCALE_SHOW_ALL;
// ±âŸ ¿É¼Ç
if ($options)
$options = array_merge(Thumbnail::$_TdefaultOptions, $options);
// ¿É¼Ç Áß Ãâ·Â À̹ÌÁö Çü½ÄÀÌ À߸ø ÁöÁ¤µÈ °æ¿ì
if ( ! in_array($options['export'], Array('jpg', 'gif', 'png')))
Thumbnail::raiseError('#Error: Thumbnail::create() : Invalid export format.'.' at '. __LINE__);
// À̹ÌÁö ŸÀÔÀÌ Áö¿øµÇÁö ¾Ê´Â °æ¿ì
// 1 = GIF, 2 = JPEG
$type = getimagesize($filepath);
if (($type[2] < 1 || $type[2] > 2) ||
($type[2] == 1 && ! function_exists('imagegif')) ||
($type[2] == 2 && ! function_exists('imagejpeg')))
{
Thumbnail::raiseError('#Error: Thumbnail::create() : Filetype not supported. Thumbnail not created.'.' at '. __LINE__);
}
// ¿øº» À̹ÌÁö·ÎºÎÅÍ Image °´Ã¼ »ý¼º
switch ($type[2])
{
case 1: $image = imagecreatefromgif($filepath); break;
case 2: $image = imagecreatefromjpeg($filepath); break;
}
// AntiAlias
if (function_exists('imageantialias'))
imageantialias($image, TRUE);
// À̹ÌÁö Å©±â ¼³Á¤
$image_attr = getimagesize($filepath);
$image_width = $image_attr[0];
$image_height = $image_attr[1];
if ($width > 0 && $height > 0)
{
// ¼¶³×ÀÏ Å©±â ¾È¿¡ ¸ðµÎ Ç¥½Ã
// À̹ÌÁöÀÇ °¡Àå Å« ¸éÀ» ±âÁØÀ¸·Î ÁöÁ¤
switch ($scale)
{
case SCALE_SHOW_ALL: $side = ($image_width >= $image_height) ? 'width' : 'height'; break;
case SCALE_EXACT_FIT:
default: $side = ($image_width / $width <= $image_height / $height) ? 'width' : 'height'; break;
}
$thumb_x = $thumb_y = 0;
if ($side == 'width')
{
$ratio = $image_width / $width;
$thumb_width = $width;
$thumb_height = floor($image_height / $ratio);
$thumb_y = round(($height - $thumb_height) / 2);
}
else
{
$ratio = $image_height / $height;
$thumb_width = floor($image_width / $ratio);
$thumb_height = $height;
$thumb_x = round(($width - $thumb_width) / 2);
}
}
else
{
// width ¶Ç´Â height Å©±â°¡ ÁöÁ¤µÇÁö ¾Ê¾ÒÀ» °æ¿ì,
// ÁöÁ¤µÈ ¼¶³×ÀÏ Å©±â ºñÀ²¿¡ ¸Â°Ô ´Ù¸¥ ¸éÀÇ Å©±â¸¦ ¸ÂÃã
$thumb_x = $thumb_y = 0;
if ( ! $width)
{
$thumb_width = $width = intval($image_width / ($image_height / $height));
$thumb_height = $height;
}
elseif ( ! $height)
{
$thumb_width = $width;
$thumb_height = $height = intval($image_height / ($image_width / $width));
}
}
// ¼¶³×ÀÏ °´Ã¼ »ý¼º
$thumbnail = imagecreatetruecolor($width, $height);
if ($options['preprocess'])
call_user_func($options['preprocess'], &$thumbnail, $width, $height, $thumb_width, $thumb_height);
@imagecopyresampled($thumbnail, $image, $thumb_x, $thumb_y, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
if ($options['postprocess'])
call_user_func($options['postprocess'], &$thumbnail, $width, $height, $thumb_width, $thumb_height);
// ÀúÀåÇÒ °æ·Î »ý¼º ¹× µð·ºÅ丮 °Ë»ç
preg_match('@^(.+/)?([^/]+)\.([^.]+)?$@', $filepath, $m);
$savepath = str_replace(Array('%PATH%', '%FILENAME%', '%EXT%', '%THUMB_WIDTH%', '%THUMB_HEIGHT%', '%IMAGE_WIDTH%', '%IMAGE_HEIGHT%'), Array($m[1], $m[2], $options['export'], $width, $height, $image_width, $image_height), $options['savepath']);
Thumbnail::validatePath($savepath);
// ÁöÁ¤µÈ Æ÷¸äÀ¸·Î ¼¶³×ÀÏÀ̹ÌÁö ÀúÀå
$iserror = false;
switch ($options['export'])
{
case EXPORT_GIF: if ( ! imagegif($thumbnail, $savepath)) $iserror = true; break;
case EXPORT_PNG: if ( ! imagepng($thumbnail, $savepath)) $iserror = true; break;
case EXPORT_JPG:
default: if ( ! imagejpeg($thumbnail, $savepath)) $iserror = true; break;
}
if ($iserror)
Thumbnail::raiseError('#Error: Thumbnail::create() : invalid path or permission error.'.' at '. __LINE__);
elseif (Thumbnail::getOption('debug'))
{
echo '@Debug: Thumbnail::create() - source='. $filepath .', image[width='. $image_width .',height='. $image_height .'], '
.'thumb[width='. $width .',height='. $height .'], scale='. $scale .', scaled[x='. $thumb_x .',y='. $thumb_y
.',width='. $thumb_width .',height='. $thumb_height .']ºÎÁ·Çϰųª Ãß°¡ÇÒ ºÎºÐ ÀÖÀ¸¸é Á÷Á¢ ¼öÁ¤Çϼŵµ ¹«¹æÇÕ´Ï´Ù.. =_= |
|||||
|
|||||
Àüü´ñ±Û¼ö 4
12345678910



