Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/Image.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan;
 12: 
 13: /**
 14:  * คลาสสำหรับจัดการรูปภาพ.
 15:  *
 16:  * @author Goragod Wiriya <admin@goragod.com>
 17:  *
 18:  * @since 1.0
 19:  */
 20: class Image
 21: {
 22:     /**
 23:      * คุณภาพของรูปภาพ.
 24:      *
 25:      * @var int
 26:      */
 27:     private static $quality = 75;
 28: 
 29:     /**
 30:      * ฟังก์ชั่น ตัดรูปภาพ ตามขนาดที่กำหนด
 31:      * รูปภาพปลายทางจะมีขนาดเท่าที่กำหนด หากรูปภาพต้นฉบับมีขนาดหรืออัตราส่วนไม่พอดีกับขนาดของภาพปลายทาง
 32:      * รูปภาพจะถูกตัดขอบออกหรือจะถูกขยาย เพื่อให้พอดีกับรูปภาพปลายทางที่ต้องการ
 33:      * ผลลัพท์จะได้ไฟล์รูปภาพ jpg เท่านั้น
 34:      * สำเร็จคืนค่า true.
 35:      *
 36:      * @param string $source      path และชื่อไฟล์ของไฟล์รูปภาพต้นฉบับ
 37:      * @param string $target      path และชื่อไฟล์ของไฟล์รูปภาพปลายทาง
 38:      * @param int    $thumbwidth  ความกว้างของรูปภาพที่ต้องการ
 39:      * @param int    $thumbheight ความสูงของรูปภาพที่ต้องการ
 40:      * @param string $watermark   (optional) ข้อความลายน้ำ
 41:      *
 42:      * @return bool
 43:      */
 44:     public static function crop($source, $target, $thumbwidth, $thumbheight, $watermark = '')
 45:     {
 46:         $info = getimagesize($source);
 47:         switch ($info['mime']) {
 48:             case 'image/gif':
 49:                 $o_im = imageCreateFromGIF($source);
 50:                 break;
 51:             case 'image/jpg':
 52:             case 'image/jpeg':
 53:             case 'image/pjpeg':
 54:                 $o_im = self::orient($source);
 55:                 break;
 56:             case 'image/png':
 57:             case 'image/x-png':
 58:                 $o_im = imageCreateFromPNG($source);
 59:                 break;
 60:             default:
 61:                 return false;
 62:         }
 63:         $o_wd = @imagesx($o_im);
 64:         $o_ht = @imagesy($o_im);
 65:         $wm = $o_wd / $thumbwidth;
 66:         $hm = $o_ht / $thumbheight;
 67:         $h_height = $thumbheight / 2;
 68:         $w_height = $thumbwidth / 2;
 69:         $t_im = ImageCreateTrueColor($thumbwidth, $thumbheight);
 70:         $int_width = 0;
 71:         $int_height = 0;
 72:         $adjusted_width = $thumbwidth;
 73:         $adjusted_height = $thumbheight;
 74:         if ($o_wd > $o_ht) {
 75:             $adjusted_width = ceil($o_wd / $hm);
 76:             $half_width = $adjusted_width / 2;
 77:             $int_width = $half_width - $w_height;
 78:             if ($adjusted_width < $thumbwidth) {
 79:                 $adjusted_height = ceil($o_ht / $wm);
 80:                 $half_height = $adjusted_height / 2;
 81:                 $int_height = $half_height - $h_height;
 82:                 $adjusted_width = $thumbwidth;
 83:                 $int_width = 0;
 84:             }
 85:         } elseif (($o_wd < $o_ht) || ($o_wd == $o_ht)) {
 86:             $adjusted_height = ceil($o_ht / $wm);
 87:             $half_height = $adjusted_height / 2;
 88:             $int_height = $half_height - $h_height;
 89:             if ($adjusted_height < $thumbheight) {
 90:                 $adjusted_width = ceil($o_wd / $hm);
 91:                 $half_width = $adjusted_width / 2;
 92:                 $int_width = $half_width - $w_height;
 93:                 $adjusted_height = $thumbheight;
 94:                 $int_height = 0;
 95:             }
 96:         }
 97:         ImageCopyResampled($t_im, $o_im, -$int_width, -$int_height, 0, 0, $adjusted_width, $adjusted_height, $o_wd, $o_ht);
 98:         if ($watermark != '') {
 99:             $t_im = self::watermarkText($t_im, $watermark);
100:         }
101:         $ret = @ImageJPEG($t_im, $target, self::$quality);
102:         imageDestroy($o_im);
103:         imageDestroy($t_im);
104: 
105:         return $ret;
106:     }
107: 
108:     /**
109:      * ฟังก์ชั่น พลิกรูปภาพ (ซ้าย-ขวา คล้ายกระจกเงา)
110:      * คืนค่า resource ของรูปภาพหลังจากพลิกรูปภาพแล้ว ไม่สำเร็จคืนค่า resource ของรูปภาพต้นฉบับ.
111:      *
112:      * @param resource $imgsrc resource ของรูปภาพต้นฉบับ
113:      *
114:      * @return resource
115:      */
116:     public static function flip($imgsrc)
117:     {
118:         $width = imagesx($imgsrc);
119:         $height = imagesy($imgsrc);
120:         $src_x = $width - 1;
121:         $src_y = 0;
122:         $src_width = -$width;
123:         $src_height = $height;
124:         $imgdest = imagecreatetruecolor($width, $height);
125:         if (imagecopyresampled($imgdest, $imgsrc, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height)) {
126:             return $imgdest;
127:         }
128: 
129:         return $imgsrc;
130:     }
131: 
132:     /**
133:      * อ่านข้อมูล Exif ของรูปภาพ
134:      * คืนค่า array(width, height, mime) ของรูปภาพ, false ถ้าไม่สามารถอ่านได้.
135:      *
136:      * @param string $src
137:      *
138:      * @return array|bool
139:      */
140:     public static function info($src)
141:     {
142:         // Exif
143:         $info = getimagesize($src);
144:         if ($info && $info[0] > 0 && $info[1] > 0) {
145:             return array(
146:                 'width' => $info[0],
147:                 'height' => $info[1],
148:                 'mime' => $info['mime'],
149:             );
150:         }
151: 
152:         return false;
153:     }
154: 
155:     /**
156:      * ฟังก์ชั่น โหลดภาพ jpg และหมุนภาพอัตโนมัติจากข้อมูลของ Exif
157:      * คืนค่า resource ของรูปภาพหลังจากหมุนแล้ว ถ้าไม่สนับสนุนคืนค่า resource เดิม
158:      *
159:      * @param resource $source resource ของรูปภาพต้นฉบับ
160:      *
161:      * @return resource
162:      */
163:     public static function orient($source)
164:     {
165:         $imgsrc = imageCreateFromJPEG($source);
166:         if (function_exists('exif_read_data')) {
167:             // read image exif and rotate
168:             $exif = @exif_read_data($source);
169:             if (!isset($exif['Orientation'])) {
170:                 return $imgsrc;
171:             } elseif ($exif['Orientation'] == 2) {
172:                 // horizontal flip
173:                 $imgsrc = self::flip($imgsrc);
174:             } elseif ($exif['Orientation'] == 3) {
175:                 // 180 rotate left
176:                 $imgsrc = imagerotate($imgsrc, 180, 0);
177:             } elseif ($exif['Orientation'] == 4) {
178:                 // vertical flip
179:                 $imgsrc = self::flip($imgsrc);
180:             } elseif ($exif['Orientation'] == 5) {
181:                 // vertical flip + 90 rotate right
182:                 $imgsrc = imagerotate($imgsrc, 270, 0);
183:                 $imgsrc = self::flip($imgsrc);
184:             } elseif ($exif['Orientation'] == 6) {
185:                 // 90 rotate right
186:                 $imgsrc = imagerotate($imgsrc, 270, 0);
187:             } elseif ($exif['Orientation'] == 7) {
188:                 // horizontal flip + 90 rotate right
189:                 $imgsrc = imagerotate($imgsrc, 90, 0);
190:                 $imgsrc = self::flip($imgsrc);
191:             } elseif ($exif['Orientation'] == 8) {
192:                 // 90 rotate left
193:                 $imgsrc = imagerotate($imgsrc, 90, 0);
194:             }
195:         }
196: 
197:         return $imgsrc;
198:     }
199: 
200:     /**
201:      * ฟังก์ชั่นปรับขนาดของภาพ โดยรักษาอัตราส่วนของภาพตามความกว้างที่ต้องการ
202:      * หากรูปภาพมีขนาดเล็กกว่าที่กำหนด จะเป็นการ copy file
203:      * หากรูปภาพมาความสูง หรือความกว้างมากกว่า $width
204:      * จะถูกปรับขนาดให้มีขนาดไม่เกิน $width (ทั้งความสูงและความกว้าง)
205:      * และเปลี่ยนชนิดของภาพเป็น jpg
206:      * คืนค่าแอเรย์ [name, width, height, mime] ของรูปภาพปลายทาง หรือ false ถ้าไม่สามารถดำเนินการได้.
207:      *
208:      * @param string $source    path และชื่อไฟล์ของไฟล์รูปภาพต้นฉบับ
209:      * @param string $target    path ของไฟล์รูปภาพปลายทาง
210:      * @param string $name      ชื่อไฟล์ของรูปภาพปลายทาง
211:      * @param int    $width     ขนาดสูงสุดของรูปภาพที่ต้องการ
212:      * @param string $watermark (optional) ข้อความลายน้ำ
213:      *
214:      * @return array|bool
215:      */
216:     public static function resize($source, $target, $name, $width, $watermark = '')
217:     {
218:         $info = @getimagesize($source);
219:         if ($info[0] > $width || $info[1] > $width) {
220:             switch ($info['mime']) {
221:                 case 'image/gif':
222:                     $o_im = imageCreateFromGIF($source);
223:                     break;
224:                 case 'image/jpg':
225:                 case 'image/jpeg':
226:                 case 'image/pjpeg':
227:                     $o_im = self::orient($source);
228:                     break;
229:                 case 'image/png':
230:                 case 'image/x-png':
231:                     $o_im = imageCreateFromPNG($source);
232:                     break;
233:             }
234:             $o_wd = @imagesx($o_im);
235:             $o_ht = @imagesy($o_im);
236:             if ($o_wd <= $o_ht) {
237:                 $h = $width;
238:                 $w = round($h * $o_wd / $o_ht);
239:             } else {
240:                 $w = $width;
241:                 $h = round($w * $o_ht / $o_wd);
242:             }
243:             $t_im = @ImageCreateTrueColor($w, $h);
244:             @ImageCopyResampled($t_im, $o_im, 0, 0, 0, 0, $w + 1, $h + 1, $o_wd, $o_ht);
245:             if ($watermark != '') {
246:                 $t_im = self::watermarkText($t_im, $watermark);
247:             }
248:             $newname = substr($name, 0, strrpos($name, '.')).'.jpg';
249:             if (!@ImageJPEG($t_im, $target.$newname, self::$quality)) {
250:                 $ret = false;
251:             } else {
252:                 $ret['name'] = $newname;
253:                 $ret['width'] = $w;
254:                 $ret['height'] = $h;
255:                 $ret['mime'] = 'image/jpeg';
256:             }
257:             @imageDestroy($o_im);
258:             @imageDestroy($t_im);
259: 
260:             return $ret;
261:         } elseif (@copy($source, $target.$name)) {
262:             $ret['name'] = $name;
263:             $ret['width'] = $info[0];
264:             $ret['height'] = $info[1];
265:             $ret['mime'] = $info['mime'];
266: 
267:             return $ret;
268:         }
269: 
270:         return false;
271:     }
272: 
273:     /**
274:      * ฟังก์ชั่น วาดลายน้ำที่เป็นตัวอักษรลงบนรูปภาพ
275:      * คืนค่า resource ของรูปภาพต้นฉบับ.
276:      *
277:      * @param resource $imgsrc    resource ของรูปภาพต้นฉบับ
278:      * @param string   $text      ข้อความที่จะใช้เป็นลายน้ำ
279:      * @param string   $pos       (optional) ตำแหน่งของลายน้ำเช่น center top bottom right left (default 'top left')
280:      * @param string   $color     (optional) สีของตัวอักษร เป็น hex เท่านั้น ไม่ต้องมี # (default CCCCCC)
281:      * @param int      $font_size (optional) ขนาดตัวอักษรของลายน้ำเป็นพิกเซล (default 20px)
282:      * @param int      $opacity   (optional) กำหนดค่าตัวอักษรโปร่งใส 0-50 (default 50)
283:      *
284:      * @return resource
285:      */
286:     public static function watermarkText($imgsrc, $text, $pos = '', $color = 'CCCCCC', $font_size = 20, $opacity = 50)
287:     {
288:         $font = ROOT_PATH.'skin/fonts/leelawad.ttf';
289:         $offset = 5;
290:         $alpha_color = imagecolorallocatealpha($imgsrc, hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2)), 127 * (100 - $opacity) / 100);
291:         $box = imagettfbbox($font_size, 0, $font, $text);
292:         if (preg_match('/center/i', $pos)) {
293:             $y = $box[1] + (imagesy($imgsrc) / 2) - ($box[5] / 2);
294:         } elseif (preg_match('/bottom/i', $pos)) {
295:             $y = imagesy($imgsrc) - $offset;
296:         } else {
297:             $y = $box[1] - $box[5] + $offset;
298:         }
299:         if (preg_match('/center/i', $pos)) {
300:             $x = $box[0] + (imagesx($imgsrc) / 2) - ($box[4] / 2);
301:         } elseif (preg_match('/right/i', $pos)) {
302:             $x = $box[0] - $box[4] + imagesx($imgsrc) - $offset;
303:         } else {
304:             $x = $offset;
305:         }
306:         imagettftext($imgsrc, $font_size, 0, $x, $y, $alpha_color, $font, $text);
307: 
308:         return $imgsrc;
309:     }
310: }
311: 
Kotchasan API documentation generated by ApiGen