2011-07-12 12:29:35 +02:00
|
|
|
<?php
|
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
/*
|
2013-01-20 11:23:46 +01:00
|
|
|
* Copyright (c) 2010-2013 Tinyboard Development Group
|
2012-04-11 18:49:22 +02:00
|
|
|
*/
|
|
|
|
|
2013-09-06 12:12:04 +02:00
|
|
|
defined('TINYBOARD') or exit;
|
2012-04-11 18:49:22 +02:00
|
|
|
|
|
|
|
class Image {
|
|
|
|
public $src, $format, $image, $size;
|
2013-08-04 02:50:37 +02:00
|
|
|
public function __construct($src, $format = false, $size = false) {
|
2012-04-11 18:49:22 +02:00
|
|
|
global $config;
|
2011-07-12 12:29:35 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
$this->src = $src;
|
|
|
|
$this->format = $format;
|
2012-12-24 05:34:06 +01:00
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if ($config['thumb_method'] == 'imagick') {
|
2012-04-11 18:49:22 +02:00
|
|
|
$classname = 'ImageImagick';
|
2013-08-04 02:34:59 +02:00
|
|
|
} elseif (in_array($config['thumb_method'], array('convert', 'convert+gifsicle', 'gm', 'gm+gifsicle'))) {
|
2012-04-11 18:49:22 +02:00
|
|
|
$classname = 'ImageConvert';
|
|
|
|
} else {
|
|
|
|
$classname = 'Image' . strtoupper($this->format);
|
2012-04-12 16:18:19 +02:00
|
|
|
if (!class_exists($classname)) {
|
2014-01-04 19:26:24 +01:00
|
|
|
error(_('Unsupported file format: ') . $this->format);
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:50:37 +02:00
|
|
|
$this->image = new $classname($this, $size);
|
2012-12-24 05:34:06 +01:00
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if (!$this->image->valid()) {
|
2012-04-11 18:49:22 +02:00
|
|
|
$this->delete();
|
|
|
|
error($config['error']['invalidimg']);
|
2012-03-10 10:53:41 +01:00
|
|
|
}
|
|
|
|
|
2012-08-27 13:50:15 +02:00
|
|
|
$this->size = (object)array('width' => $this->image->_width(), 'height' => $this->image->_height());
|
2012-04-12 16:18:19 +02:00
|
|
|
if ($this->size->width < 1 || $this->size->height < 1) {
|
2012-04-11 18:49:22 +02:00
|
|
|
$this->delete();
|
|
|
|
error($config['error']['invalidimg']);
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
public function resize($extension, $max_width, $max_height) {
|
|
|
|
global $config;
|
2012-12-24 05:34:06 +01:00
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if ($config['thumb_method'] == 'imagick') {
|
2012-04-11 18:49:22 +02:00
|
|
|
$classname = 'ImageImagick';
|
2012-04-12 16:18:19 +02:00
|
|
|
} elseif ($config['thumb_method'] == 'convert') {
|
2012-04-11 18:49:22 +02:00
|
|
|
$classname = 'ImageConvert';
|
2012-12-24 05:34:06 +01:00
|
|
|
} elseif ($config['thumb_method'] == 'convert+gifsicle') {
|
|
|
|
$classname = 'ImageConvert';
|
|
|
|
$gifsicle = true;
|
2013-08-04 02:34:59 +02:00
|
|
|
} elseif ($config['thumb_method'] == 'gm') {
|
|
|
|
$classname = 'ImageConvert';
|
|
|
|
$gm = true;
|
|
|
|
} elseif ($config['thumb_method'] == 'gm+gifsicle') {
|
|
|
|
$classname = 'ImageConvert';
|
|
|
|
$gm = true;
|
|
|
|
$gifsicle = true;
|
2012-04-11 18:49:22 +02:00
|
|
|
} else {
|
|
|
|
$classname = 'Image' . strtoupper($extension);
|
2012-04-12 16:18:19 +02:00
|
|
|
if (!class_exists($classname)) {
|
2014-01-04 19:26:24 +01:00
|
|
|
error(_('Unsupported file format: ') . $extension);
|
2012-04-11 18:49:22 +02:00
|
|
|
}
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
|
|
|
|
$thumb = new $classname(false);
|
|
|
|
$thumb->src = $this->src;
|
2012-04-17 16:07:37 +02:00
|
|
|
$thumb->format = $this->format;
|
2012-04-11 18:49:22 +02:00
|
|
|
$thumb->original_width = $this->size->width;
|
|
|
|
$thumb->original_height = $this->size->height;
|
|
|
|
|
|
|
|
$x_ratio = $max_width / $this->size->width;
|
|
|
|
$y_ratio = $max_height / $this->size->height;
|
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if (($this->size->width <= $max_width) && ($this->size->height <= $max_height)) {
|
2012-04-11 18:49:22 +02:00
|
|
|
$width = $this->size->width;
|
|
|
|
$height = $this->size->height;
|
|
|
|
} elseif (($x_ratio * $this->size->height) < $max_height) {
|
|
|
|
$height = ceil($x_ratio * $this->size->height);
|
|
|
|
$width = $max_width;
|
|
|
|
} else {
|
|
|
|
$width = ceil($y_ratio * $this->size->width);
|
|
|
|
$height = $max_height;
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
|
|
|
|
$thumb->_resize($this->image->image, $width, $height);
|
2013-08-04 03:47:24 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
return $thumb;
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
public function to($dst) {
|
|
|
|
$this->image->to($dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete() {
|
|
|
|
file_unlink($this->src);
|
|
|
|
}
|
|
|
|
public function destroy() {
|
|
|
|
$this->image->_destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ImageGD {
|
|
|
|
public function GD_create() {
|
|
|
|
$this->image = imagecreatetruecolor($this->width, $this->height);
|
|
|
|
}
|
|
|
|
public function GD_copyresampled() {
|
|
|
|
imagecopyresampled($this->image, $this->original, 0, 0, 0, 0, $this->width, $this->height, $this->original_width, $this->original_height);
|
|
|
|
}
|
|
|
|
public function GD_resize() {
|
|
|
|
$this->GD_create();
|
|
|
|
$this->GD_copyresampled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ImageBase extends ImageGD {
|
|
|
|
public $image, $src, $original, $original_width, $original_height, $width, $height;
|
|
|
|
public function valid() {
|
|
|
|
return (bool)$this->image;
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:50:37 +02:00
|
|
|
public function __construct($img, $size = false) {
|
2012-04-12 16:18:19 +02:00
|
|
|
if (method_exists($this, 'init'))
|
2012-04-11 18:49:22 +02:00
|
|
|
$this->init();
|
2011-07-12 12:29:35 +02:00
|
|
|
|
2013-08-04 02:50:37 +02:00
|
|
|
if ($size && $size[0] > 0 && $size[1] > 0) {
|
|
|
|
$this->width = $size[0];
|
|
|
|
$this->height = $size[1];
|
|
|
|
}
|
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if ($img !== false) {
|
2012-04-11 18:49:22 +02:00
|
|
|
$this->src = $img->src;
|
|
|
|
$this->from();
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function _width() {
|
2012-04-12 16:18:19 +02:00
|
|
|
if (method_exists($this, 'width'))
|
2012-04-11 18:49:22 +02:00
|
|
|
return $this->width();
|
|
|
|
// use default GD functions
|
|
|
|
return imagesx($this->image);
|
|
|
|
}
|
|
|
|
public function _height() {
|
2012-04-12 16:18:19 +02:00
|
|
|
if (method_exists($this, 'height'))
|
2012-04-11 18:49:22 +02:00
|
|
|
return $this->height();
|
|
|
|
// use default GD functions
|
|
|
|
return imagesy($this->image);
|
|
|
|
}
|
|
|
|
public function _destroy() {
|
2012-04-12 16:18:19 +02:00
|
|
|
if (method_exists($this, 'destroy'))
|
2012-04-11 18:49:22 +02:00
|
|
|
return $this->destroy();
|
|
|
|
// use default GD functions
|
|
|
|
return imagedestroy($this->image);
|
|
|
|
}
|
|
|
|
public function _resize($original, $width, $height) {
|
|
|
|
$this->original = &$original;
|
|
|
|
$this->width = $width;
|
|
|
|
$this->height = $height;
|
2011-07-12 12:29:35 +02:00
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if (method_exists($this, 'resize'))
|
2012-04-11 18:49:22 +02:00
|
|
|
$this->resize();
|
|
|
|
else
|
2011-07-12 12:29:35 +02:00
|
|
|
// use default GD functions
|
2012-04-11 18:49:22 +02:00
|
|
|
$this->GD_resize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ImageImagick extends ImageBase {
|
|
|
|
public function init() {
|
|
|
|
$this->image = new Imagick();
|
|
|
|
$this->image->setBackgroundColor(new ImagickPixel('transparent'));
|
|
|
|
}
|
|
|
|
public function from() {
|
|
|
|
try {
|
|
|
|
$this->image->readImage($this->src);
|
|
|
|
} catch(ImagickException $e) {
|
|
|
|
// invalid image
|
|
|
|
$this->image = false;
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
public function to($src) {
|
2013-08-01 09:51:50 +02:00
|
|
|
global $config;
|
2013-07-21 21:50:45 +02:00
|
|
|
if ($config['strip_exif']) {
|
|
|
|
$this->image->stripImage();
|
|
|
|
}
|
2012-04-12 16:18:19 +02:00
|
|
|
if (preg_match('/\.gif$/i', $src))
|
2012-04-11 18:49:22 +02:00
|
|
|
$this->image->writeImages($src, true);
|
|
|
|
else
|
|
|
|
$this->image->writeImage($src);
|
|
|
|
}
|
|
|
|
public function width() {
|
|
|
|
return $this->image->getImageWidth();
|
|
|
|
}
|
|
|
|
public function height() {
|
|
|
|
return $this->image->getImageHeight();
|
|
|
|
}
|
|
|
|
public function destroy() {
|
|
|
|
return $this->image->destroy();
|
|
|
|
}
|
|
|
|
public function resize() {
|
|
|
|
global $config;
|
|
|
|
|
2012-12-22 20:16:31 +01:00
|
|
|
if ($this->format == 'gif' && ($config['thumb_ext'] == 'gif' || $config['thumb_ext'] == '')) {
|
2011-07-22 11:40:57 +02:00
|
|
|
$this->image = new Imagick();
|
2012-04-11 18:49:22 +02:00
|
|
|
$this->image->setFormat('gif');
|
2011-07-28 12:25:06 +02:00
|
|
|
|
2012-08-27 13:50:15 +02:00
|
|
|
$keep_frames = array();
|
2012-04-12 16:18:19 +02:00
|
|
|
for ($i = 0; $i < $this->original->getNumberImages(); $i += floor($this->original->getNumberImages() / $config['thumb_keep_animation_frames']))
|
2012-04-11 18:49:22 +02:00
|
|
|
$keep_frames[] = $i;
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
$delay = 0;
|
2012-04-12 16:18:19 +02:00
|
|
|
foreach ($this->original as $frame) {
|
2012-04-11 18:49:22 +02:00
|
|
|
$delay += $frame->getImageDelay();
|
2012-03-10 12:56:28 +01:00
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if (in_array($i, $keep_frames)) {
|
2012-04-11 18:49:22 +02:00
|
|
|
// $frame->scaleImage($this->width, $this->height, false);
|
|
|
|
$frame->sampleImage($this->width, $this->height);
|
|
|
|
$frame->setImagePage($this->width, $this->height, 0, 0);
|
|
|
|
$frame->setImageDelay($delay);
|
|
|
|
$delay = 0;
|
2012-03-10 12:56:28 +01:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
$this->image->addImage($frame->getImage());
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->image = clone $this->original;
|
|
|
|
$this->image->scaleImage($this->width, $this->height, false);
|
2011-07-22 11:40:57 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ImageConvert extends ImageBase {
|
2013-08-04 02:34:59 +02:00
|
|
|
public $width, $height, $temp, $gm = false, $gifsicle = false;
|
2011-07-22 11:40:57 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
public function init() {
|
|
|
|
global $config;
|
2012-03-17 00:32:50 +01:00
|
|
|
|
2013-08-04 02:34:59 +02:00
|
|
|
if ($config['thumb_method'] == 'gm' || $config['thumb_method'] == 'gm+gifsicle')
|
|
|
|
$this->gm = true;
|
|
|
|
if ($config['thumb_method'] == 'convert+gifsicle' || $config['thumb_method'] == 'gm+gifsicle')
|
|
|
|
$this->gifsicle = true;
|
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
$this->temp = false;
|
|
|
|
}
|
2013-08-04 03:47:24 +02:00
|
|
|
public function get_size($src, $try_gd_first = true) {
|
|
|
|
if ($try_gd_first) {
|
|
|
|
if ($size = @getimagesize($src))
|
|
|
|
return $size;
|
|
|
|
}
|
|
|
|
$size = shell_exec_error(($this->gm ? 'gm ' : '') . 'identify -format "%w %h" ' . escapeshellarg($src . '[0]'));
|
|
|
|
if (preg_match('/^(\d+) (\d+)$/', $size, $m))
|
|
|
|
return array($m[1], $m[2]);
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-04 02:34:59 +02:00
|
|
|
public function from() {
|
2013-08-04 02:50:37 +02:00
|
|
|
if ($this->width > 0 && $this->height > 0) {
|
|
|
|
$this->image = true;
|
|
|
|
return;
|
|
|
|
}
|
2013-08-04 03:47:24 +02:00
|
|
|
$size = $this->get_size($this->src, false);
|
|
|
|
if ($size) {
|
2013-08-04 11:11:46 +02:00
|
|
|
$this->width = $size[0];
|
|
|
|
$this->height = $size[1];
|
2012-03-17 00:32:50 +01:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
$this->image = true;
|
|
|
|
} else {
|
|
|
|
// mark as invalid
|
|
|
|
$this->image = false;
|
2012-03-17 00:32:50 +01:00
|
|
|
}
|
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
public function to($src) {
|
2013-07-21 21:50:45 +02:00
|
|
|
global $config;
|
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if (!$this->temp) {
|
2013-07-21 21:50:45 +02:00
|
|
|
if ($config['strip_exif']) {
|
2013-08-04 02:34:59 +02:00
|
|
|
if($error = shell_exec_error(($this->gm ? 'gm ' : '') . 'convert ' .
|
|
|
|
escapeshellarg($this->src) . ' -auto-orient -strip ' . escapeshellarg($src))) {
|
|
|
|
$this->destroy();
|
2014-01-04 19:26:24 +01:00
|
|
|
error(_('Failed to redraw image!'), null, $error);
|
2013-08-04 02:34:59 +02:00
|
|
|
}
|
2013-07-21 21:50:45 +02:00
|
|
|
} else {
|
2013-08-04 02:34:59 +02:00
|
|
|
if($error = shell_exec_error(($this->gm ? 'gm ' : '') . 'convert ' .
|
|
|
|
escapeshellarg($this->src) . ' -auto-orient ' . escapeshellarg($src))) {
|
|
|
|
$this->destroy();
|
2014-01-04 19:26:24 +01:00
|
|
|
error(_('Failed to redraw image!'), null, $error);
|
2013-08-04 02:34:59 +02:00
|
|
|
}
|
2013-07-21 21:50:45 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
} else {
|
|
|
|
rename($this->temp, $src);
|
|
|
|
chmod($src, 0664);
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
public function width() {
|
|
|
|
return $this->width;
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
public function height() {
|
|
|
|
return $this->height;
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
public function destroy() {
|
|
|
|
@unlink($this->temp);
|
|
|
|
$this->temp = false;
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
public function resize() {
|
|
|
|
global $config;
|
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if ($this->temp) {
|
2012-04-11 18:49:22 +02:00
|
|
|
// remove old
|
|
|
|
$this->destroy();
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
|
2013-08-28 09:20:29 +02:00
|
|
|
$this->temp = tempnam($config['tmp'], 'convert');
|
2013-08-03 02:52:58 +02:00
|
|
|
|
|
|
|
$config['thumb_keep_animation_frames'] = (int)$config['thumb_keep_animation_frames'];
|
2013-08-01 10:24:34 +02:00
|
|
|
|
2012-12-22 20:16:31 +01:00
|
|
|
if ($this->format == 'gif' && ($config['thumb_ext'] == 'gif' || $config['thumb_ext'] == '') && $config['thumb_keep_animation_frames'] > 1) {
|
2012-12-24 05:34:06 +01:00
|
|
|
if ($this->gifsicle) {
|
2013-08-07 01:40:17 +02:00
|
|
|
if (($error = shell_exec("gifsicle -w --unoptimize -O2 --resize {$this->width}x{$this->height} < " .
|
2013-08-28 09:20:29 +02:00
|
|
|
escapeshellarg($this->src . '') . " \"#0-{$config['thumb_keep_animation_frames']}\" -o " .
|
|
|
|
escapeshellarg($this->temp))) || !file_exists($this->temp)) {
|
|
|
|
$this->destroy();
|
2014-01-04 19:26:24 +01:00
|
|
|
error(_('Failed to resize image!'), null, $error);
|
2013-08-28 09:20:29 +02:00
|
|
|
}
|
2013-07-31 09:35:07 +02:00
|
|
|
} else {
|
2013-08-04 04:20:39 +02:00
|
|
|
if ($config['convert_manual_orient'] && ($this->format == 'jpg' || $this->format == 'jpeg'))
|
2013-08-04 04:14:25 +02:00
|
|
|
$convert_args = str_replace('-auto-orient', ImageConvert::jpeg_exif_orientation($this->src), $config['convert_args']);
|
2013-08-04 04:20:39 +02:00
|
|
|
elseif ($config['convert_manual_orient'])
|
|
|
|
$convert_args = str_replace('-auto-orient', '', $config['convert_args']);
|
2013-08-04 04:14:25 +02:00
|
|
|
else
|
|
|
|
$convert_args = &$config['convert_args'];
|
|
|
|
if (($error = shell_exec_error(($this->gm ? 'gm ' : '') . 'convert ' .
|
|
|
|
sprintf($convert_args,
|
2013-08-04 02:34:59 +02:00
|
|
|
$this->width,
|
|
|
|
$this->height,
|
|
|
|
escapeshellarg($this->src),
|
|
|
|
$this->width,
|
|
|
|
$this->height,
|
2013-08-28 09:20:29 +02:00
|
|
|
escapeshellarg($this->temp)))) || !file_exists($this->temp)) {
|
|
|
|
$this->destroy();
|
2013-08-03 06:22:28 +02:00
|
|
|
error('Failed to resize image!', null, $error);
|
2013-08-28 09:20:29 +02:00
|
|
|
}
|
2013-08-04 03:47:24 +02:00
|
|
|
if ($size = $this->get_size($this->temp)) {
|
|
|
|
$this->width = $size[0];
|
|
|
|
$this->height = $size[1];
|
|
|
|
}
|
2012-12-24 05:34:06 +01:00
|
|
|
}
|
2012-04-17 16:07:37 +02:00
|
|
|
} else {
|
2013-08-04 04:20:39 +02:00
|
|
|
if ($config['convert_manual_orient'] && ($this->format == 'jpg' || $this->format == 'jpeg'))
|
2013-08-04 04:14:25 +02:00
|
|
|
$convert_args = str_replace('-auto-orient', ImageConvert::jpeg_exif_orientation($this->src), $config['convert_args']);
|
2013-08-04 04:20:39 +02:00
|
|
|
elseif ($config['convert_manual_orient'])
|
|
|
|
$convert_args = str_replace('-auto-orient', '', $config['convert_args']);
|
2013-08-04 04:14:25 +02:00
|
|
|
else
|
|
|
|
$convert_args = &$config['convert_args'];
|
|
|
|
if (($error = shell_exec_error(($this->gm ? 'gm ' : '') . 'convert ' .
|
|
|
|
sprintf($convert_args,
|
2013-08-04 02:34:59 +02:00
|
|
|
$this->width,
|
|
|
|
$this->height,
|
|
|
|
escapeshellarg($this->src . '[0]'),
|
|
|
|
$this->width,
|
|
|
|
$this->height,
|
2013-08-28 09:20:29 +02:00
|
|
|
escapeshellarg($this->temp)))) || !file_exists($this->temp)) {
|
2013-12-26 20:58:39 +01:00
|
|
|
if (strpos($error, "known incorrect sRGB profile") === "false") {
|
|
|
|
$this->destroy();
|
|
|
|
error('Failed to resize image!', null, $error);
|
|
|
|
}
|
2013-08-28 09:20:29 +02:00
|
|
|
}
|
2013-08-04 03:47:24 +02:00
|
|
|
if ($size = $this->get_size($this->temp)) {
|
|
|
|
$this->width = $size[0];
|
|
|
|
$this->height = $size[1];
|
|
|
|
}
|
2012-04-17 16:07:37 +02:00
|
|
|
}
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
2013-08-04 04:14:25 +02:00
|
|
|
|
|
|
|
// For when -auto-orient doesn't exist (older versions)
|
|
|
|
static public function jpeg_exif_orientation($src, $exif = false) {
|
|
|
|
if (!$exif) {
|
2013-08-05 05:02:15 +02:00
|
|
|
$exif = @exif_read_data($src);
|
2013-08-04 04:14:25 +02:00
|
|
|
if (!isset($exif['Orientation']))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch($exif['Orientation']) {
|
|
|
|
case 1:
|
|
|
|
// Normal
|
|
|
|
return false;
|
|
|
|
case 2:
|
|
|
|
// 888888
|
|
|
|
// 88
|
|
|
|
// 8888
|
|
|
|
// 88
|
|
|
|
// 88
|
|
|
|
|
|
|
|
return '-flop';
|
|
|
|
case 3:
|
|
|
|
|
|
|
|
// 88
|
|
|
|
// 88
|
|
|
|
// 8888
|
|
|
|
// 88
|
|
|
|
// 888888
|
|
|
|
|
|
|
|
return '-flip -flop';
|
|
|
|
case 4:
|
|
|
|
// 88
|
|
|
|
// 88
|
|
|
|
// 8888
|
|
|
|
// 88
|
|
|
|
// 888888
|
|
|
|
|
|
|
|
return '-flip';
|
|
|
|
case 5:
|
|
|
|
// 8888888888
|
|
|
|
// 88 88
|
|
|
|
// 88
|
|
|
|
|
|
|
|
return '-rotate 90 -flop';
|
|
|
|
case 6:
|
|
|
|
// 88
|
|
|
|
// 88 88
|
|
|
|
// 8888888888
|
|
|
|
|
|
|
|
return '-rotate 90';
|
|
|
|
case 7:
|
|
|
|
// 88
|
|
|
|
// 88 88
|
|
|
|
// 8888888888
|
|
|
|
|
|
|
|
return '-rotate "-90" -flop';
|
|
|
|
case 8:
|
|
|
|
// 8888888888
|
|
|
|
// 88 88
|
|
|
|
// 88
|
|
|
|
|
|
|
|
return '-rotate "-90"';
|
|
|
|
}
|
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
}
|
2011-07-12 12:29:35 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
class ImagePNG extends ImageBase {
|
|
|
|
public function from() {
|
|
|
|
$this->image = @imagecreatefrompng($this->src);
|
|
|
|
}
|
|
|
|
public function to($src) {
|
|
|
|
global $config;
|
2013-08-03 02:52:58 +02:00
|
|
|
imagepng($this->image, $src);
|
2012-04-11 18:49:22 +02:00
|
|
|
}
|
|
|
|
public function resize() {
|
|
|
|
$this->GD_create();
|
|
|
|
imagecolortransparent($this->image, imagecolorallocatealpha($this->image, 0, 0, 0, 0));
|
|
|
|
imagesavealpha($this->image, true);
|
|
|
|
imagealphablending($this->image, false);
|
|
|
|
$this->GD_copyresampled();
|
|
|
|
}
|
|
|
|
}
|
2011-07-12 12:29:35 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
class ImageGIF extends ImageBase {
|
|
|
|
public function from() {
|
2013-08-03 02:52:58 +02:00
|
|
|
$this->image = @imagecreatefromgif($this->src);
|
2012-04-11 18:49:22 +02:00
|
|
|
}
|
|
|
|
public function to($src) {
|
2012-04-12 16:18:19 +02:00
|
|
|
imagegif ($this->image, $src);
|
2012-04-11 18:49:22 +02:00
|
|
|
}
|
|
|
|
public function resize() {
|
|
|
|
$this->GD_create();
|
|
|
|
imagecolortransparent($this->image, imagecolorallocatealpha($this->image, 0, 0, 0, 0));
|
|
|
|
imagesavealpha($this->image, true);
|
|
|
|
$this->GD_copyresampled();
|
|
|
|
}
|
|
|
|
}
|
2011-07-12 12:29:35 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
class ImageJPG extends ImageBase {
|
|
|
|
public function from() {
|
|
|
|
$this->image = @imagecreatefromjpeg($this->src);
|
|
|
|
}
|
|
|
|
public function to($src) {
|
|
|
|
imagejpeg($this->image, $src);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class ImageJPEG extends ImageJPG {
|
|
|
|
}
|
2011-07-12 12:29:35 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
class ImageBMP extends ImageBase {
|
|
|
|
public function from() {
|
|
|
|
$this->image = @imagecreatefrombmp($this->src);
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
public function to($src) {
|
|
|
|
imagebmp($this->image, $src);
|
|
|
|
}
|
|
|
|
}
|
2011-07-12 12:29:35 +02:00
|
|
|
|
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
/*********************************************/
|
|
|
|
/* Fonction: imagecreatefrombmp */
|
|
|
|
/* Author: DHKold */
|
|
|
|
/* Contact: admin@dhkold.com */
|
|
|
|
/* Date: The 15th of June 2005 */
|
|
|
|
/* Version: 2.0B */
|
|
|
|
/*********************************************/
|
2011-07-12 12:29:35 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
function imagecreatefrombmp($filename) {
|
|
|
|
if (! $f1 = fopen($filename,"rb")) return FALSE;
|
|
|
|
$FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
|
|
|
|
if ($FILE['file_type'] != 19778) return FALSE;
|
|
|
|
$BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
|
|
|
|
'/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
|
|
|
|
'/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
|
|
|
|
$BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
|
|
|
|
if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
|
|
|
|
$BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
|
|
|
|
$BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
|
|
|
|
$BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
|
|
|
|
$BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
|
|
|
|
$BMP['decal'] = 4-(4*$BMP['decal']);
|
|
|
|
if ($BMP['decal'] == 4) $BMP['decal'] = 0;
|
2011-07-12 12:29:35 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
$PALETTE = array();
|
|
|
|
if ($BMP['colors'] < 16777216)
|
|
|
|
{
|
|
|
|
$PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
|
|
|
|
}
|
2011-07-12 12:29:35 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
$IMG = fread($f1,$BMP['size_bitmap']);
|
|
|
|
$VIDE = chr(0);
|
2011-07-12 12:29:35 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
$res = imagecreatetruecolor($BMP['width'],$BMP['height']);
|
|
|
|
$P = 0;
|
|
|
|
$Y = $BMP['height']-1;
|
|
|
|
while ($Y >= 0)
|
|
|
|
{
|
|
|
|
$X=0;
|
|
|
|
while ($X < $BMP['width'])
|
|
|
|
{
|
|
|
|
if ($BMP['bits_per_pixel'] == 24)
|
|
|
|
$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
|
|
|
|
elseif ($BMP['bits_per_pixel'] == 16)
|
|
|
|
{
|
|
|
|
$COLOR = unpack("n",substr($IMG,$P,2));
|
|
|
|
$COLOR[1] = $PALETTE[$COLOR[1]+1];
|
|
|
|
}
|
|
|
|
elseif ($BMP['bits_per_pixel'] == 8)
|
|
|
|
{
|
|
|
|
$COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
|
|
|
|
$COLOR[1] = $PALETTE[$COLOR[1]+1];
|
|
|
|
}
|
|
|
|
elseif ($BMP['bits_per_pixel'] == 4)
|
|
|
|
{
|
|
|
|
$COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
|
|
|
|
if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
|
|
|
|
$COLOR[1] = $PALETTE[$COLOR[1]+1];
|
|
|
|
}
|
|
|
|
elseif ($BMP['bits_per_pixel'] == 1)
|
|
|
|
{
|
|
|
|
$COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
|
|
|
|
if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
|
|
|
|
elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
|
|
|
|
elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
|
|
|
|
elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
|
|
|
|
elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
|
|
|
|
elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
|
|
|
|
elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
|
|
|
|
elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
|
|
|
|
$COLOR[1] = $PALETTE[$COLOR[1]+1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
imagesetpixel($res,$X,$Y,$COLOR[1]);
|
|
|
|
$X++;
|
|
|
|
$P += $BMP['bytes_per_pixel'];
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
$Y--;
|
|
|
|
$P+=$BMP['decal'];
|
|
|
|
}
|
|
|
|
fclose($f1);
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function imagebmp(&$img, $filename='') {
|
|
|
|
$widthOrig = imagesx($img);
|
|
|
|
$widthFloor = ((floor($widthOrig/16))*16);
|
|
|
|
$widthCeil = ((ceil($widthOrig/16))*16);
|
|
|
|
$height = imagesy($img);
|
|
|
|
|
|
|
|
$size = ($widthCeil*$height*3)+54;
|
|
|
|
|
|
|
|
// Bitmap File Header
|
|
|
|
$result = 'BM'; // header (2b)
|
|
|
|
$result .= int_to_dword($size); // size of file (4b)
|
|
|
|
$result .= int_to_dword(0); // reserved (4b)
|
|
|
|
$result .= int_to_dword(54); // byte location in the file which is first byte of IMAGE (4b)
|
|
|
|
// Bitmap Info Header
|
|
|
|
$result .= int_to_dword(40); // Size of BITMAPINFOHEADER (4b)
|
|
|
|
$result .= int_to_dword($widthCeil); // width of bitmap (4b)
|
|
|
|
$result .= int_to_dword($height); // height of bitmap (4b)
|
|
|
|
$result .= int_to_word(1); // biPlanes = 1 (2b)
|
|
|
|
$result .= int_to_word(24); // biBitCount = {1 (mono) or 4 (16 clr ) or 8 (256 clr) or 24 (16 Mil)} (2b
|
|
|
|
$result .= int_to_dword(0); // RLE COMPRESSION (4b)
|
|
|
|
$result .= int_to_dword(0); // width x height (4b)
|
|
|
|
$result .= int_to_dword(0); // biXPelsPerMeter (4b)
|
|
|
|
$result .= int_to_dword(0); // biYPelsPerMeter (4b)
|
|
|
|
$result .= int_to_dword(0); // Number of palettes used (4b)
|
|
|
|
$result .= int_to_dword(0); // Number of important colour (4b)
|
|
|
|
|
|
|
|
// is faster than chr()
|
|
|
|
$arrChr = array();
|
2012-04-12 16:18:19 +02:00
|
|
|
for ($i=0; $i<256; $i++){
|
2012-04-11 18:49:22 +02:00
|
|
|
$arrChr[$i] = chr($i);
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
|
|
|
|
// creates image data
|
|
|
|
$bgfillcolor = array('red'=>0, 'green'=>0, 'blue'=>0);
|
|
|
|
|
|
|
|
// bottom to top - left to right - attention blue green red !!!
|
|
|
|
$y=$height-1;
|
|
|
|
for ($y2=0; $y2<$height; $y2++) {
|
|
|
|
for ($x=0; $x<$widthFloor; ) {
|
|
|
|
$rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
|
|
|
|
$result .= $arrChr[$rgb['blue']].$arrChr[$rgb['green']].$arrChr[$rgb['red']];
|
|
|
|
$rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
|
|
|
|
$result .= $arrChr[$rgb['blue']].$arrChr[$rgb['green']].$arrChr[$rgb['red']];
|
|
|
|
$rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
|
|
|
|
$result .= $arrChr[$rgb['blue']].$arrChr[$rgb['green']].$arrChr[$rgb['red']];
|
|
|
|
$rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
|
|
|
|
$result .= $arrChr[$rgb['blue']].$arrChr[$rgb['green']].$arrChr[$rgb['red']];
|
|
|
|
$rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
|
|
|
|
$result .= $arrChr[$rgb['blue']].$arrChr[$rgb['green']].$arrChr[$rgb['red']];
|
|
|
|
$rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
|
|
|
|
$result .= $arrChr[$rgb['blue']].$arrChr[$rgb['green']].$arrChr[$rgb['red']];
|
|
|
|
$rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
|
|
|
|
$result .= $arrChr[$rgb['blue']].$arrChr[$rgb['green']].$arrChr[$rgb['red']];
|
|
|
|
$rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
|
|
|
|
$result .= $arrChr[$rgb['blue']].$arrChr[$rgb['green']].$arrChr[$rgb['red']];
|
|
|
|
}
|
|
|
|
for ($x=$widthFloor; $x<$widthCeil; $x++) {
|
|
|
|
$rgb = ($x<$widthOrig) ? imagecolorsforindex($img, imagecolorat($img, $x, $y)) : $bgfillcolor;
|
|
|
|
$result .= $arrChr[$rgb['blue']].$arrChr[$rgb['green']].$arrChr[$rgb['red']];
|
|
|
|
}
|
|
|
|
$y--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// see imagegif
|
2012-04-12 16:18:19 +02:00
|
|
|
if ($filename == '') {
|
2012-04-11 18:49:22 +02:00
|
|
|
echo $result;
|
|
|
|
} else {
|
|
|
|
$file = fopen($filename, 'wb');
|
|
|
|
fwrite($file, $result);
|
|
|
|
fclose($file);
|
2011-07-12 12:29:35 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
}
|
|
|
|
// imagebmp helpers
|
|
|
|
function int_to_dword($n) {
|
|
|
|
return chr($n & 255).chr(($n >> 8) & 255).chr(($n >> 16) & 255).chr(($n >> 24) & 255);
|
|
|
|
}
|
|
|
|
function int_to_word($n) {
|
|
|
|
return chr($n & 255).chr(($n >> 8) & 255);
|
|
|
|
}
|
2011-07-12 12:29:35 +02:00
|
|
|
|