Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- // file
- // - src - src
- // - new - new file name
- // - mode - generate mode = выяснить что я имел ввиду :)
- class images
- {
- var $srt = array
- (
- 'ic',
- 'time_create desc',
- 'time_update desc',
- );
- function images()
- {
- global $conf;
- $this->conf = $conf;
- $this->clear();
- }
- function clear()
- {
- $this->id = 0;
- $this->value = array
- (
- 'ic' => '',
- 'info' => '',
- 'descr' => '',
- 'file' => false,
- 'hash' => 'undefined',
- 'chash' => 'undefined',
- );
- $this->set_ctype(0);
- }
- // 0 - ok
- // 1 - file error
- // 2 - not image
- // 3 - exist
- function set_file($fn)
- {
- $v = &$this->value;
- $v['file'] = $fn;
- return 0;
- }
- function set_ctype($t)
- {
- if (!isset($this->conf['image_ctypes'][$t]))
- {
- reset($this->conf['image_ctypes']);
- $t = key($this->conf['image_ctypes']);
- }
- $this->value['ctype'] = $t;
- }
- // value
- // - ctype
- // - ic
- function save()
- {
- $v = &$this->value;
- if (false === $v['file'])
- {
- return $this->_err('no file');
- }
- if(false === ($im = $this->_open($v['file']) ) )
- return $this->_err('error opening' );
- if( false === ($fn = $this->_generate($im)) )
- return $this->_err('error generating');
- return true;
- }
- function _generate($im)
- {
- $v = &$this->value;
- // generate tmp files
- $fl = array();
- $ok = true;
- $st = $this->conf['image_ctypes'][$v['ctype']][2];
- $j = 100/count($st);
- $k = 0;
- foreach ($st as $i=>$s)
- {
- if (false === ($fn = $this->_convert($im, $s)))
- {
- return $this->_err('error converting');
- }
- rename($fn, dirname($im[3]) . '/' . basename($im[3], strrchr($im[3], '.')) . '_' . $i . $this->_getExtension($im[0]));
- }
- }
- // open image
- function _open($fn)
- {
- $i = "Image:";
- if (false === ($s = $this->_exec('identify', '-verbose '.$fn))
- || false === ($p = strpos($s, $i)))
- //|| !preg_match_all('/^[^ ]+ ([^ ]+) (\d+)x(\d+)/m', substr($s, 0, $p), $m))
- {
- return false;
- }
- $s = substr($s, $p);
- $s = preg_replace('/^\s+(\d+):/m', ' $1:', $s);
- if (0 == preg_match_all('/^( *)([^:]+):(?: (.*))?$/m', $s, $m, PREG_SET_ORDER))
- {
- return false;
- }
- $j = array();
- $t = array(&$j);
- for (reset($m), $x = '', $l = 0; $c = current($m); next($m))
- {
- if (0 == ($d = strlen($c[1]))
- && $i != $c[2].':')
- {
- continue;
- }
- if ($d > $l)
- {
- $t[$l][$x] = array();
- $t[$d] = &$t[$l][$x];
- }
- $x = 0 == $d ? count($t[$d]) : strtolower($c[2]);
- if (isset($c[3]))
- {
- $t[$d][$x] = $c[3];
- }
- $l = $d;
- }
- foreach ($j as $i=>$v)
- {
- if (isset($v['delay']))
- {
- $v['delay'] = intval($v['delay']);
- }
- }
- if (false === ($f = reset($j))
- || !isset($f['format'])
- || !isset($f['geometry'])
- || !preg_match('/^(\d+)x(\d+)([^ ]+)/', $f['geometry'].$f['format'], $m))
- {
- return false;
- }
- return array
- (
- 0 => $m[3], // format
- 1 => array_slice($m, 1, 2), // x y
- 2 => $j, // info
- 3 => $fn, // file name
- );
- }
- function _exec($c, $a)
- {
- $d = array
- (
- 0 => array('pipe', 'r'),
- 1 => array('pipe', 'w'),
- 2 => array('pipe', 'w'),
- );
- $p = array
- (
- 'convert' => $this->conf['imagick_dir'],
- 'identify' => $this->conf['imagick_dir'],
- 'gifsicle' => $this->conf['gifsicle_dir'],
- );
- $p = isset($p[$c]) ? $p[$c] : '';
- $h = proc_open('nice ' . $p.$c.' '.$a, $d, $p);
- if (!is_resource($h))
- {
- return $this->_err('unable to exec');
- }
- $s = array_fill(0, 2, '');
- fwrite($p[$l = 0], '');
- fclose($p[$l++]);
- for ($i = 0; $i < 2; $i++)
- {
- while (!feof($p[$l])
- && ($v = fgets($p[$l], 1024)))
- {
- $s[$i].= $v;
- }
- fclose($p[$l++]);
- }
- //echo '- EXEC ['.$c."]\n";
- return 0 !== ($r = proc_close($h)) ? false : $s[0];
- }
- // u - unit
- // s - settings
- function _convert($u, $s)
- {
- if (5 != count($s)
- || false === ($f = $this->_parse_format($u[0]))
- || false === ($m = $this->_parse_mode($u[1], $s[1]))
- || false === ($h = $this->_parse_mark($s[2]))
- || false === ($g = $this->_parse_color($s[3]))
- || false === ($b = $this->_parse_border($s[4])))
- {
- return $this->_err('error parsing file formats');
- }
- $t = $u[1];
- $z = $m[1][0]/$m[1][1] > $u[1][0]/$u[1][1] ? 0 : 1;
- $t[1 - $z] = max($m[1][1 - $z]/$m[1][$z]*$u[1][$z], $u[1][1 - $z] - 2*$m[2][1 - $z]);
- if (0 == $m[0])
- {
- for ($q = array(), $i = 0; $i < 2; $i++)
- {
- array_push($q, min($t[$i], $m[1][$i]));
- }
- }
- else
- {
- $z = $t[0]/$t[1] > $m[1][0]/$m[1][1] ? 0 : 1; // in box
- for ($q = array(), $i = 0; $i < 2; $i++)
- {
- array_push($q, min(1, $m[1][$z]/$t[$z])*$t[$i]);
- }
- }
- for ($w = array(), $i = 0; $i < 2; $i++) // cutting
- {
- //$t[$i] = round($t[$i]);
- $q[$i] = round($q[$i]);
- $w[$i] = floor(($u[1][$i] - $t[$i])/2);
- }
- $c = ''; // command
- if ('' != $m[4])
- {
- $c.= '-rotate '.$m[4].'90 ';
- }
- if (0 < max($w))
- {
- $c.= '-shave '.implode('x', $w).' ';//.' +repage ';
- }
- if ($q != $u[1])
- {
- $c.= '-filter Box -resize '.implode('x', $q).'! ';
- }
- $w = array_fill(0, 2, array()); // background
- for ($i = 0; $i < 2; $i++)
- {
- $x = (1 == $m[0] ? $m[1][$i] - $q[$i] : 0) + 2*$m[3][$i];
- $x-= ($y = floor($x/2));
- array_push($w[0], floor($x));
- array_push($w[1], $y);
- }
- foreach ($w as $i=>$v)
- {
- if (0 < max($v))
- {
- $c.= '' != $g ? '-background '.$g.' ' : '';
- $c.= '-gravity '.(0 == $i ? 'northwest' : 'southeast');
- $c.= ' -splice '.implode('x', $v).' ';
- }
- }
- if (0 < max($b[0])) // border
- {
- $c.= '' != $b[1] ? '-bordercolor '.$b[1].' ' : '';
- $c.= '-border '.implode('x', $b[0]).' ';
- }
- if ('' != $h[0])
- {
- $c.= '-gravity '.$h[1].' '.$h[0].' -compose src-over -composite ';
- }
- if (false === ($t = tempnam('/tmp', 'image_')))
- {
- return $this->_err('error creating temp file');
- }
- if (1 == ($j = count($u[2]))
- || false === $f[0])
- {
- $s = $u[3].' '.(1 != $j ? ' -delete 1-'.($j - 1).' ' : '').$c.' '.$f[1].':'.$t;
- if (false !== $this->_exec('convert', $s)
- && false !== chmod($t, 0666))
- {
- return $t;
- }
- }
- else
- {
- $s = '-coalesce ';
- foreach ($u[2] as $i=>$v)
- {
- $s.= isset($v['delay']) ? '-delay '.$v['delay'].' ' : '';
- //$s.= isset($v['background color']) ? '-set background '.$v['background color'].' ' : '';
- //$s.= '-set transparent white ';
- //$s.= isset($v['transparent color']) ? '-transparent '.$v['transparent color'].' ' : '';
- $s.= '\( -coalesce '.$u[3].' ';
- $s.= $j != $i + 1 ? '-delete '.($i + 1).'--1 ' : '';
- $s.= 0 != $i ? '-compose copy -delete 0'.($i > 1 ? '-'.($i - 1) : '').' ' : '';
- $s.= $c.'\) ';
- }
- $s.= '-fuzz 7% -layers compare-any MIFF:'.$t;
- //$s.= '-fuzz 7% -layers OptimizePlus MIFF:'.$t;
- if (false !== $this->_exec('convert', $s))
- {
- $s = $t.' +append -quantize rgb ';// transparent ';
- $s.= '-unique-colors -colors 256 MIFF:'.$t.'map';
- if (false !== $this->_exec('convert', $s))
- {
- $s = $t.' -map '.$t.'map '.$f[1].':'.$t;
- if (false !== $this->_exec('convert', $s)
- && false !== chmod($t, 0666))
- {
- unlink($t.'map');
- if (false === strpos($f[1], 'GIF')
- || false !== $this->_exec('gifsicle', '-O2 '.$t.' -o '.$t))
- {
- return $t;
- }
- }
- unlink($t.'map');
- }
- }
- }
- unlink($t);
- return false;
- }
- function _parse_mark($s)
- {
- if (false === ($p = strpos($s, '/')))
- {
- $x = 'east';
- }
- else
- {
- $x = substr($s, $p + 1);
- $s = substr($s, 0, $p);
- }
- return array
- (
- //TODO: add dir to образец image
- 0 => $s ? /*fs::path('var/src').*/$s : '',
- 1 => $x,
- );
- }
- function _parse_border($s)
- {
- $s = explode('/', $s);
- $s = array_pad($s, 2, '');
- if ($s[0] == $s[1]
- && '' == $s[0])
- {
- return array(array(0, 0), '');
- }
- return false === ($c = $this->_parse_color($s[1]))
- || false === ($x = $this->_parse_xy(array(1, 1), $s[0]))
- ? $this->_err('error parsing border')
- : array($x, $c);
- }
- function _parse_color($s)
- {
- if (0 == strlen($s))
- {
- return '';
- }
- elseif ((3 != ($c = count($s = explode(',', $s)))
- && 4 != $c)
- || 255 < max($s)
- || 0 > min($s))
- {
- return $this->_err('parse color error');
- }
- $s = 'rgb'.(4 == $c ? 'a' : '').'\('.implode(',', $s).'\)';
- return $s;
- }
- function _parse_xy($b, $s)
- {
- $s = explode('x', $s, 2);
- if (1 == count($s))
- {
- array_push($s, $s[0]);
- }
- $r = array();
- $j = false;
- foreach ($s as $i=>$c)
- {
- if (0 == strlen($c))
- {
- array_push($r, false);
- $j = $i;
- }
- elseif ('%' == $c{0})
- {
- $c = substr($c, 1);
- if (!is_numeric($c)
- || $c < 0)
- {
- return $this->_err('parse XY (percents) error');
- }
- array_push($r, $c/100*$b[$i]);
- }
- else
- {
- if (!is_numeric($c)
- || $c < 0)
- {
- return $this->_err('parse XY error');
- }
- array_push($r, intval($c));
- }
- }
- if (false !== $j)
- {
- if ($r[0] === $r[1])
- {
- return $b;
- }
- $r[$j] = $r[1 - $j]/$b[1 - $j]*$b[$j];
- }
- return $r;
- }
- function _parse_format($s)
- {
- $m = array
- (
- 'GIF' => array(true, true, 'GIF'),
- 'GIF87' => array(true, false, 'GIF87'),
- 'JPEG' => array(true, false, '-quality 90 JPEG'),
- );
- return !isset($m[$s])
- || true !== $m[$s][0]
- ? $this->_err('error parsing file format') : array_slice($m[$s], 1);
- }
- function _getExtension($format)
- {
- switch ($format)
- {
- case 'GIF':
- case 'GIF87':
- return '.gif';
- case 'JPEG':
- return '.jpg';
- }
- }
- function _parse_mode(&$b, $s)
- {
- $s = explode('/', $s);
- $s = array_pad($s, 4, '');
- $r = array(); // result
- $m = array // modes
- (
- 's' => 0, // simple resize
- '=' => 1, // inscribe
- '~' => 2, // ?
- );
- if (0 != strlen($s[0])
- && isset($m[$s[0]{0}]))
- {
- array_push($r, $m[$s[0]{0}]);
- $s[0] = substr($s[0], 1);
- }
- else
- {
- array_push($r, reset($m));
- }
- $j = $this->_parse_xy($b, reset($s));
- $m = array
- (
- '+',
- '-',
- );
- if (in_array($d = array_pop($s), $m)
- && (($j[0]/$j[1] < 1 && $b[0]/$b[1] > 1) || ($j[0]/$j[1] > 1 && $b[0]/$b[1] < 1)))
- {
- $b = array_reverse($b);
- }
- else
- {
- $d = '';
- }
- do
- {
- array_push($r, $j);
- }
- while (false !== ($c = next($s))
- && false !== ($j = $this->_parse_xy($b, $c)));
- array_push($r, $d);
- return 5 == count($r)
- ? $r
- : $this->_err('parse mode error');
- }
- function _err($error)
- {
- // logRequest($error, false);
- logRequest($error . ' ' . $this->value['file'], false);
- return false;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement