/ php-ppcms/includes/classes/lib.image.corner.class.php
PHP | 271 lines | 213 code | 31 blank | 27 comment | 35 complexity | 80d1ef7923db13509bdacf078c06e6ee MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
- <?php
- /***************************************************************
- * Copyright notice
- * (c) 2009, jianyuzhu@gmail.com
- * All rights reserved
- * This script is part of the PPEMI project.
- ***************************************************************/
-
- class LibImageCorner {
- var $src_file = '';
- var $src_w = 0;
- var $src_h = 0;
- var $src_x = 0;
- var $src_y = 0;
-
- var $dest_file = '';
- var $dest_w = 189;
- var $dest_h = 247;
- var $dest_x = 0;
- var $dest_y = 0;
-
- var $cutting = 0;
- var $angle = 0;
-
- var $alpha_top = 80;
- var $alpha_bottom = 60;
- var $alpha_x = 0;
- var $alpha_y = 0;
- var $alpha_w = 0;
- var $alpha_h = 0;
-
- var $corner_file = 'images/corners/rounded_corner_10px.png';
- var $corner_radius = 10;
- var $corner_topleft = true;
- var $corner_topright = true;
- var $corner_bottomleft = true;
- var $corner_bottomright = true;
-
- var $text_str = 'Brake';
- var $font_path = 'images/fonts/';
- var $font_list = array(
- 'Antykwa' => array('spacing' => -3, 'minSize' => 27, 'maxSize' => 30, 'font' => 'AntykwaBold.ttf'),
- 'Candice' => array('spacing' =>-1.5,'minSize' => 28, 'maxSize' => 31, 'font' => 'Candice.ttf'),
- 'DingDong' => array('spacing' => -2, 'minSize' => 24, 'maxSize' => 30, 'font' => 'Ding-DongDaddyO.ttf'),
- 'Duality' => array('spacing' => -2, 'minSize' => 30, 'maxSize' => 38, 'font' => 'Duality.ttf'),
- 'Heineken' => array('spacing' => -2, 'minSize' => 24, 'maxSize' => 34, 'font' => 'Heineken.ttf'),
- 'Jura' => array('spacing' => -2, 'minSize' => 28, 'maxSize' => 32, 'font' => 'Jura.ttf'),
- 'StayPuft' => array('spacing' =>-1.5,'minSize' => 28, 'maxSize' => 32, 'font' => 'StayPuft.ttf'),
- 'Times' => array('spacing' => -2, 'minSize' => 28, 'maxSize' => 34, 'font' => 'TimesNewRomanBold.ttf'),
- 'VeraSans' => array('spacing' => -1, 'minSize' => 20, 'maxSize' => 28, 'font' => 'VeraSansBold.ttf'),
- );
-
- var $imagecreatefromfunc = '';
- var $imagefunc = '';
-
- //constructor
- function LibImageCorner($src_file, $dest_file) {
- $this->src_file = $src_file;
- $this->dest_file = $dest_file;
- //
- $srcinfo = @getimagesize($src_file);
- $this->src_w = $srcinfo['0'];
- $this->src_h = $srcinfo['1'];
- switch($srcinfo['mime'] ) {
- case 'image/jpeg':
- $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
- $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
- break;
- case 'image/gif':
- $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
- $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';
- break;
- case 'image/png':
- $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng': '';
- $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';
- break;
- }
- //
- $this->_image();
- }
-
- //methods
- function _image() {
- if( function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg') ) {
- $imagecreatefromfunc = $this->imagecreatefromfunc;
- $imagefunc = $this->imagefunc;
-
- if( $this->src_w >= $this->dest_w || $this->src_h >= $this->dest_h ) {
- //
- if( $this->cutting == 3 ) {
- $x_ratio = $this->dest_w / $this->src_w;
- $y_ratio = $this->dest_h / $this->src_h;
- if( ($x_ratio * $this->src_h) < $this->dest_h ) {
- $dest_h = ceil($x_ratio * $this->src_h);
- $dest_w = $this->dest_w;
- } else {
- $dest_w = ceil($y_ratio * $this->src_w);
- $dest_h = $this->dest_h;
- }
- $src_w = $this->src_w;
- $src_h = $this->src_h;
- if( $this->src_w > $this->dest_w ) {
- $src_w = $this->dest_w;
- }
- if( $this->src_h > $this->dest_h ) {
- $src_h = $this->dest_h;
- }
- $dest_w = $this->dest_w;
- $dest_h = $this->dest_h;
- } elseif( $this->cutting == 2 ) {
- $x_ratio = $this->dest_w / $this->src_w;
- $y_ratio = $this->dest_h / $this->src_h;
- if( ($x_ratio * $this->src_h) < $this->dest_h ) {
- $dest_h = ceil($x_ratio * $this->src_h);
- $dest_w = $this->dest_w;
- } else {
- $dest_w = ceil($y_ratio * $this->src_w);
- $dest_h = $this->dest_h;
- }
- $src_w = $this->src_w;
- $src_h = $this->src_h;
- } elseif( $this->cutting == 1 ) {
- $x_ratio = $this->dest_w / $this->src_w;
- $y_ratio = $this->dest_h / $this->src_h;
- if( ($x_ratio * $this->src_h) < $this->dest_h ) {
- $dest_h = ceil($x_ratio * $this->src_h);
- $dest_w = $this->dest_w;
- } else {
- $dest_w = ceil($y_ratio * $this->src_w);
- $dest_h = $this->dest_h;
- }
- $src_w = $this->src_w;
- $src_h = $this->src_h;
- } else {
- $src_w = $this->src_w;
- $src_h = $this->src_h;
- $dest_w = $this->dest_w;
- $dest_h = $this->dest_h;
- }
-
- //
- $src_im = $imagecreatefromfunc($this->src_file);
-
- //
- $dest_im = imagecreatetruecolor($dest_w, $dest_h);
- imagecopyresampled($dest_im, $src_im, $this->dest_x, $this->dest_y, $this->src_x, $this->src_y, $dest_w, $dest_h, $src_w, $src_h);
-
- //
- $dest_im = $this->_alpha($dest_im, $dest_w, $dest_h);
-
- //
- $dest_im = $this->_corner($dest_im, $dest_w, $dest_h);
-
- //
- $dest_im = $this->_text($dest_im);
-
- //
- if( $this->attachinfo['mime'] == 'image/jpeg' ) {
- $imagefunc($dest_im, $this->dest_file, 100);
- } else {
- $imagefunc($dest_im, $this->dest_file);
- }
-
- //
- imagedestroy($src_im);
- imagedestroy($dest_im);
- }
- }
- }
-
- function _corner($dest_im, $dest_width, $dest_height) {
- $corner_im = imagecreatefrompng($this->corner_file);
- $corner_width = imagesx($corner_im);
- $corner_height = imagesy($corner_im);
- $corner_imr = imagecreatetruecolor($this->corner_radius, $this->corner_radius);
- imagecopyresampled($corner_imr, $corner_im, 0, 0, 0, 0, $this->corner_radius, $this->corner_radius, $corner_width, $corner_height);
-
- $corner_width = imagesx($corner_imr);
- $corner_height = imagesy($corner_imr);
- $col_white = imagecolorallocate($dest_im, 255, 255, 255);
- $col_black = imagecolorallocate($dest_im, 0, 0, 0);
-
- // Top-left corner
- if( $this->corner_topleft == true ) {
- $dest_x = 0;
- $dest_y = 0;
- imagecolortransparent($corner_imr, $col_black);
- imagecopymerge($dest_im, $corner_imr, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
- }
-
- // Bottom-left corner
- if( $this->corner_bottomleft == true ) {
- $dest_x = 0;
- $dest_y = $dest_height - $corner_height;
- $rotated = imagerotate($corner_imr, 90, 0);
- imagecolortransparent($rotated, $col_black);
- imagecopymerge($dest_im, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
- }
-
- // Bottom-right corner
- if( $this->corner_bottomright == true ) {
- $dest_x = $dest_width - $corner_width;
- $dest_y = $dest_height - $corner_height;
- $rotated = imagerotate($corner_imr, 180, 0);
- imagecolortransparent($rotated, $col_black);
- imagecopymerge($dest_im, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
- }
-
- // Top-right corner
- if( $this->corner_topright == true ) {
- $dest_x = $dest_width - $corner_width;
- $dest_y = 0;
- $rotated = imagerotate($corner_imr, 270, 0);
- imagecolortransparent($rotated, $col_black);
- imagecopymerge($dest_im, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
- }
-
- // Rotate image
- //$dest_im = imagerotate($dest_im, $this->angle, $col_white);
-
- return $dest_im;
- }
-
- //
- function _alpha($dest_im, $dest_width, $dest_height) {
- if( $this->alpha_w == 0 ) {
- $this->alpha_w = $dest_width;
- $this->alpha_h = $dest_height;
- }
- for($i=0; $i<$this->alpha_top; $i++){
- $alphax = round($i/$this->alpha_top*127)+20;
- if( $alphax >= 128 ) {
- $alphax = 127;
- }
- $alpha = imagecolorallocatealpha($dest_im, 20, 20, 20, $alphax);
- imagefilledrectangle($dest_im, $this->alpha_x, $this->alpha_y+$i, $this->alpha_w, $this->alpha_y+$i+1, $alpha);
- }
- for($i=$this->alpha_h-$this->alpha_bottom; $i<$this->alpha_h; $i++){
- $alphax = round(($this->alpha_h-$i)/$this->alpha_bottom*110)+30;
- if( $alphax >= 128 ) {
- $alphax = 127;
- }
- $alpha = imagecolorallocatealpha($dest_im, 80, 80, 80, $alphax);
- imagefilledrectangle($dest_im, $this->alpha_x, $this->alpha_y+$i, $this->alpha_w, $this->alpha_y+$i+1, $alpha);
- }
-
- return $dest_im;
- }
-
- //
- function _text($dest_im) {
- $white = imagecolorallocate($dest_im, 255, 255, 255);
- $black = imagecolorallocate($dest_im, 0, 0, 0);
-
- $fontcfg = $this->font_list[array_rand($this->font_list)];
- $fontfile = $this->font_path . $fontcfg['font'];
-
- $text_size = 30;
- $text_angle = 0;
- $text_box = imagettfbbox($text_size, $text_angle, $fontfile, $this->text_str);
- $text_w = $text_box['2'] - $text_box['0'];
- $text_h = $text_box['3'] - $text_box['5'];
- $text_x = ceil(($this->dest_w - $text_w)/2);
- $text_y = $text_h+10;
- imagettftext($dest_im, $text_size, $text_angle, $text_x, $text_y, $white, $fontfile, $this->text_str);
-
- return $dest_im;
- }
- }
- //
- ?>