mirror of https://github.com/rusefi/msqur.git
Bisect image stuff.
This commit is contained in:
parent
cc71936970
commit
03fea81073
|
@ -1,6 +1,6 @@
|
|||
# README #
|
||||
|
||||
This README would normally document whatever steps are necessary to get your application up and running.
|
||||
TuneShare
|
||||
|
||||
# TODO #
|
||||
1. Upload file
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
|
||||
class UploadHandler
|
||||
{
|
||||
|
||||
protected $options;
|
||||
|
||||
// PHP File Upload error message codes:
|
||||
|
@ -30,16 +29,9 @@ class UploadHandler
|
|||
'min_file_size' => 'File is too small',
|
||||
'accept_file_types' => 'Filetype not allowed',
|
||||
'max_number_of_files' => 'Maximum number of files exceeded',
|
||||
'max_width' => 'Image exceeds maximum width',
|
||||
'min_width' => 'Image requires a minimum width',
|
||||
'max_height' => 'Image exceeds maximum height',
|
||||
'min_height' => 'Image requires a minimum height',
|
||||
'abort' => 'File upload aborted',
|
||||
'image_resize' => 'Failed to resize image'
|
||||
'abort' => 'File upload aborted'
|
||||
);
|
||||
|
||||
protected $image_objects = array();
|
||||
|
||||
function __construct($options = null, $initialize = true, $error_messages = null) {
|
||||
$this->options = array(
|
||||
'script_url' => $this->get_full_url().'/',
|
||||
|
@ -77,8 +69,6 @@ class UploadHandler
|
|||
// Read files in chunks to avoid memory limits when download_via_php
|
||||
// is enabled, set to 0 to disable chunked reading of files:
|
||||
'readfile_chunk_size' => 10 * 1024 * 1024, // 10 MiB
|
||||
// Defines which files can be displayed inline when downloaded:
|
||||
'inline_file_types' => '/\.(gif|jpe?g|png)$/i',
|
||||
// Defines which files (based on their names) are accepted for upload:
|
||||
'accept_file_types' => '/.msq$/i',
|
||||
// The php.ini settings upload_max_filesize and post_max_size
|
||||
|
@ -87,65 +77,8 @@ class UploadHandler
|
|||
'min_file_size' => 1,
|
||||
// The maximum number of files for the upload directory:
|
||||
'max_number_of_files' => null,
|
||||
// Defines which files are handled as image files:
|
||||
'image_file_types' => '/\.(gif|jpe?g|png)$/i',
|
||||
// Image resolution restrictions:
|
||||
'max_width' => null,
|
||||
'max_height' => null,
|
||||
'min_width' => 1,
|
||||
'min_height' => 1,
|
||||
// Set the following option to false to enable resumable uploads:
|
||||
'discard_aborted_uploads' => true,
|
||||
// Set to 0 to use the GD library to scale and orient images,
|
||||
// set to 1 to use imagick (if installed, falls back to GD),
|
||||
// set to 2 to use the ImageMagick convert binary directly:
|
||||
'image_library' => 1,
|
||||
// Uncomment the following to define an array of resource limits
|
||||
// for imagick:
|
||||
/*
|
||||
'imagick_resource_limits' => array(
|
||||
imagick::RESOURCETYPE_MAP => 32,
|
||||
imagick::RESOURCETYPE_MEMORY => 32
|
||||
),
|
||||
*/
|
||||
// Command or path for to the ImageMagick convert binary:
|
||||
'convert_bin' => 'convert',
|
||||
// Uncomment the following to add parameters in front of each
|
||||
// ImageMagick convert call (the limit constraints seem only
|
||||
// to have an effect if put in front):
|
||||
/*
|
||||
'convert_params' => '-limit memory 32MiB -limit map 32MiB',
|
||||
*/
|
||||
// Command or path for to the ImageMagick identify binary:
|
||||
'identify_bin' => 'identify',
|
||||
'image_versions' => array(
|
||||
// The empty image version key defines options for the original image:
|
||||
'' => array(
|
||||
// Automatically rotate images based on EXIF meta data:
|
||||
'auto_orient' => true
|
||||
),
|
||||
// Uncomment the following to create medium sized images:
|
||||
/*
|
||||
'medium' => array(
|
||||
'max_width' => 800,
|
||||
'max_height' => 600
|
||||
),
|
||||
*/
|
||||
'thumbnail' => array(
|
||||
// Uncomment the following to use a defined directory for the thumbnails
|
||||
// instead of a subdirectory based on the version identifier.
|
||||
// Make sure that this directory doesn't allow execution of files if you
|
||||
// don't pose any restrictions on the type of uploaded files, e.g. by
|
||||
// copying the .htaccess file from the files directory for Apache:
|
||||
//'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/thumb/',
|
||||
//'upload_url' => $this->get_full_url().'/thumb/',
|
||||
// Uncomment the following to force the max
|
||||
// dimensions and e.g. create square thumbnails:
|
||||
//'crop' => true,
|
||||
'max_width' => 80,
|
||||
'max_height' => 80
|
||||
)
|
||||
)
|
||||
'discard_aborted_uploads' => true
|
||||
);
|
||||
if ($options) {
|
||||
$this->options = $options + $this->options;
|
||||
|
@ -208,7 +141,7 @@ class UploadHandler
|
|||
if (empty($version)) {
|
||||
$version_path = '';
|
||||
} else {
|
||||
$version_dir = @$this->options['image_versions'][$version]['upload_dir'];
|
||||
//$version_dir = @$this->options['image_versions'][$version]['upload_dir'];
|
||||
if ($version_dir) {
|
||||
return $version_dir.$this->get_user_path().$file_name;
|
||||
}
|
||||
|
@ -236,7 +169,7 @@ class UploadHandler
|
|||
if (empty($version)) {
|
||||
$version_path = '';
|
||||
} else {
|
||||
$version_url = @$this->options['image_versions'][$version]['upload_url'];
|
||||
//$version_url = @$this->options['image_versions'][$version]['upload_url'];
|
||||
if ($version_url) {
|
||||
return $version_url.$this->get_user_path().rawurlencode($file_name);
|
||||
}
|
||||
|
@ -296,16 +229,6 @@ class UploadHandler
|
|||
$this->get_upload_path($file_name)
|
||||
);
|
||||
$file->url = $this->get_download_url($file->name);
|
||||
foreach($this->options['image_versions'] as $version => $options) {
|
||||
if (!empty($version)) {
|
||||
if (is_file($this->get_upload_path($file_name, $version))) {
|
||||
$file->{$version.'Url'} = $this->get_download_url(
|
||||
$file->name,
|
||||
$version
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->set_additional_file_properties($file);
|
||||
return $file;
|
||||
}
|
||||
|
@ -387,32 +310,6 @@ class UploadHandler
|
|||
$file->error = $this->get_error_message('max_number_of_files');
|
||||
return false;
|
||||
}
|
||||
$max_width = @$this->options['max_width'];
|
||||
$max_height = @$this->options['max_height'];
|
||||
$min_width = @$this->options['min_width'];
|
||||
$min_height = @$this->options['min_height'];
|
||||
if (($max_width || $max_height || $min_width || $min_height)
|
||||
&& preg_match($this->options['image_file_types'], $file->name)) {
|
||||
list($img_width, $img_height) = $this->get_image_size($uploaded_file);
|
||||
}
|
||||
if (!empty($img_width)) {
|
||||
if ($max_width && $img_width > $max_width) {
|
||||
$file->error = $this->get_error_message('max_width');
|
||||
return false;
|
||||
}
|
||||
if ($max_height && $img_height > $max_height) {
|
||||
$file->error = $this->get_error_message('max_height');
|
||||
return false;
|
||||
}
|
||||
if ($min_width && $img_width < $min_width) {
|
||||
$file->error = $this->get_error_message('min_width');
|
||||
return false;
|
||||
}
|
||||
if ($min_height && $img_height < $min_height) {
|
||||
$file->error = $this->get_error_message('min_height');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -459,33 +356,11 @@ class UploadHandler
|
|||
$name = str_replace('.', '-', microtime(true));
|
||||
}
|
||||
// Add missing file extension for known image types:
|
||||
if (strpos($name, '.') === false &&
|
||||
preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) {
|
||||
$name .= '.'.$matches[1];
|
||||
}
|
||||
if (function_exists('exif_imagetype')) {
|
||||
switch(@exif_imagetype($file_path)){
|
||||
case IMAGETYPE_JPEG:
|
||||
$extensions = array('jpg', 'jpeg');
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
$extensions = array('png');
|
||||
break;
|
||||
case IMAGETYPE_GIF:
|
||||
$extensions = array('gif');
|
||||
break;
|
||||
}
|
||||
// Adjust incorrect image file extensions:
|
||||
if (!empty($extensions)) {
|
||||
$parts = explode('.', $name);
|
||||
$extIndex = count($parts) - 1;
|
||||
$ext = strtolower(@$parts[$extIndex]);
|
||||
if (!in_array($ext, $extensions)) {
|
||||
$parts[$extIndex] = $extensions[0];
|
||||
$name = implode('.', $parts);
|
||||
}
|
||||
}
|
||||
}
|
||||
//if (strpos($name, '.') === false &&
|
||||
//preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) {
|
||||
//$name .= '.'.$matches[1];
|
||||
//}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
|
@ -507,515 +382,15 @@ class UploadHandler
|
|||
// Handle form data, e.g. $_REQUEST['description'][$index]
|
||||
}
|
||||
|
||||
protected function get_scaled_image_file_paths($file_name, $version) {
|
||||
$file_path = $this->get_upload_path($file_name);
|
||||
if (!empty($version)) {
|
||||
$version_dir = $this->get_upload_path(null, $version);
|
||||
if (!is_dir($version_dir)) {
|
||||
mkdir($version_dir, $this->options['mkdir_mode'], true);
|
||||
}
|
||||
$new_file_path = $version_dir.'/'.$file_name;
|
||||
} else {
|
||||
$new_file_path = $file_path;
|
||||
}
|
||||
return array($file_path, $new_file_path);
|
||||
}
|
||||
|
||||
protected function gd_get_image_object($file_path, $func, $no_cache = false) {
|
||||
if (empty($this->image_objects[$file_path]) || $no_cache) {
|
||||
$this->gd_destroy_image_object($file_path);
|
||||
$this->image_objects[$file_path] = $func($file_path);
|
||||
}
|
||||
return $this->image_objects[$file_path];
|
||||
}
|
||||
|
||||
protected function gd_set_image_object($file_path, $image) {
|
||||
$this->gd_destroy_image_object($file_path);
|
||||
$this->image_objects[$file_path] = $image;
|
||||
}
|
||||
|
||||
protected function gd_destroy_image_object($file_path) {
|
||||
$image = @$this->image_objects[$file_path];
|
||||
return $image && imagedestroy($image);
|
||||
}
|
||||
|
||||
protected function gd_imageflip($image, $mode) {
|
||||
if (function_exists('imageflip')) {
|
||||
return imageflip($image, $mode);
|
||||
}
|
||||
$new_width = $src_width = imagesx($image);
|
||||
$new_height = $src_height = imagesy($image);
|
||||
$new_img = imagecreatetruecolor($new_width, $new_height);
|
||||
$src_x = 0;
|
||||
$src_y = 0;
|
||||
switch ($mode) {
|
||||
case '1': // flip on the horizontal axis
|
||||
$src_y = $new_height - 1;
|
||||
$src_height = -$new_height;
|
||||
break;
|
||||
case '2': // flip on the vertical axis
|
||||
$src_x = $new_width - 1;
|
||||
$src_width = -$new_width;
|
||||
break;
|
||||
case '3': // flip on both axes
|
||||
$src_y = $new_height - 1;
|
||||
$src_height = -$new_height;
|
||||
$src_x = $new_width - 1;
|
||||
$src_width = -$new_width;
|
||||
break;
|
||||
default:
|
||||
return $image;
|
||||
}
|
||||
imagecopyresampled(
|
||||
$new_img,
|
||||
$image,
|
||||
0,
|
||||
0,
|
||||
$src_x,
|
||||
$src_y,
|
||||
$new_width,
|
||||
$new_height,
|
||||
$src_width,
|
||||
$src_height
|
||||
);
|
||||
return $new_img;
|
||||
}
|
||||
|
||||
protected function gd_orient_image($file_path, $src_img) {
|
||||
if (!function_exists('exif_read_data')) {
|
||||
return false;
|
||||
}
|
||||
$exif = @exif_read_data($file_path);
|
||||
if ($exif === false) {
|
||||
return false;
|
||||
}
|
||||
$orientation = intval(@$exif['Orientation']);
|
||||
if ($orientation < 2 || $orientation > 8) {
|
||||
return false;
|
||||
}
|
||||
switch ($orientation) {
|
||||
case 2:
|
||||
$new_img = $this->gd_imageflip(
|
||||
$src_img,
|
||||
defined('IMG_FLIP_VERTICAL') ? IMG_FLIP_VERTICAL : 2
|
||||
);
|
||||
break;
|
||||
case 3:
|
||||
$new_img = imagerotate($src_img, 180, 0);
|
||||
break;
|
||||
case 4:
|
||||
$new_img = $this->gd_imageflip(
|
||||
$src_img,
|
||||
defined('IMG_FLIP_HORIZONTAL') ? IMG_FLIP_HORIZONTAL : 1
|
||||
);
|
||||
break;
|
||||
case 5:
|
||||
$tmp_img = $this->gd_imageflip(
|
||||
$src_img,
|
||||
defined('IMG_FLIP_HORIZONTAL') ? IMG_FLIP_HORIZONTAL : 1
|
||||
);
|
||||
$new_img = imagerotate($tmp_img, 270, 0);
|
||||
imagedestroy($tmp_img);
|
||||
break;
|
||||
case 6:
|
||||
$new_img = imagerotate($src_img, 270, 0);
|
||||
break;
|
||||
case 7:
|
||||
$tmp_img = $this->gd_imageflip(
|
||||
$src_img,
|
||||
defined('IMG_FLIP_VERTICAL') ? IMG_FLIP_VERTICAL : 2
|
||||
);
|
||||
$new_img = imagerotate($tmp_img, 270, 0);
|
||||
imagedestroy($tmp_img);
|
||||
break;
|
||||
case 8:
|
||||
$new_img = imagerotate($src_img, 90, 0);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
$this->gd_set_image_object($file_path, $new_img);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function gd_create_scaled_image($file_name, $version, $options) {
|
||||
if (!function_exists('imagecreatetruecolor')) {
|
||||
error_log('Function not found: imagecreatetruecolor');
|
||||
return false;
|
||||
}
|
||||
list($file_path, $new_file_path) =
|
||||
$this->get_scaled_image_file_paths($file_name, $version);
|
||||
$type = strtolower(substr(strrchr($file_name, '.'), 1));
|
||||
switch ($type) {
|
||||
case 'jpg':
|
||||
case 'jpeg':
|
||||
$src_func = 'imagecreatefromjpeg';
|
||||
$write_func = 'imagejpeg';
|
||||
$image_quality = isset($options['jpeg_quality']) ?
|
||||
$options['jpeg_quality'] : 75;
|
||||
break;
|
||||
case 'gif':
|
||||
$src_func = 'imagecreatefromgif';
|
||||
$write_func = 'imagegif';
|
||||
$image_quality = null;
|
||||
break;
|
||||
case 'png':
|
||||
$src_func = 'imagecreatefrompng';
|
||||
$write_func = 'imagepng';
|
||||
$image_quality = isset($options['png_quality']) ?
|
||||
$options['png_quality'] : 9;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
$src_img = $this->gd_get_image_object(
|
||||
$file_path,
|
||||
$src_func,
|
||||
!empty($options['no_cache'])
|
||||
);
|
||||
$image_oriented = false;
|
||||
if (!empty($options['auto_orient']) && $this->gd_orient_image(
|
||||
$file_path,
|
||||
$src_img
|
||||
)) {
|
||||
$image_oriented = true;
|
||||
$src_img = $this->gd_get_image_object(
|
||||
$file_path,
|
||||
$src_func
|
||||
);
|
||||
}
|
||||
$max_width = $img_width = imagesx($src_img);
|
||||
$max_height = $img_height = imagesy($src_img);
|
||||
if (!empty($options['max_width'])) {
|
||||
$max_width = $options['max_width'];
|
||||
}
|
||||
if (!empty($options['max_height'])) {
|
||||
$max_height = $options['max_height'];
|
||||
}
|
||||
$scale = min(
|
||||
$max_width / $img_width,
|
||||
$max_height / $img_height
|
||||
);
|
||||
if ($scale >= 1) {
|
||||
if ($image_oriented) {
|
||||
return $write_func($src_img, $new_file_path, $image_quality);
|
||||
}
|
||||
if ($file_path !== $new_file_path) {
|
||||
return copy($file_path, $new_file_path);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (empty($options['crop'])) {
|
||||
$new_width = $img_width * $scale;
|
||||
$new_height = $img_height * $scale;
|
||||
$dst_x = 0;
|
||||
$dst_y = 0;
|
||||
$new_img = imagecreatetruecolor($new_width, $new_height);
|
||||
} else {
|
||||
if (($img_width / $img_height) >= ($max_width / $max_height)) {
|
||||
$new_width = $img_width / ($img_height / $max_height);
|
||||
$new_height = $max_height;
|
||||
} else {
|
||||
$new_width = $max_width;
|
||||
$new_height = $img_height / ($img_width / $max_width);
|
||||
}
|
||||
$dst_x = 0 - ($new_width - $max_width) / 2;
|
||||
$dst_y = 0 - ($new_height - $max_height) / 2;
|
||||
$new_img = imagecreatetruecolor($max_width, $max_height);
|
||||
}
|
||||
// Handle transparency in GIF and PNG images:
|
||||
switch ($type) {
|
||||
case 'gif':
|
||||
case 'png':
|
||||
imagecolortransparent($new_img, imagecolorallocate($new_img, 0, 0, 0));
|
||||
case 'png':
|
||||
imagealphablending($new_img, false);
|
||||
imagesavealpha($new_img, true);
|
||||
break;
|
||||
}
|
||||
$success = imagecopyresampled(
|
||||
$new_img,
|
||||
$src_img,
|
||||
$dst_x,
|
||||
$dst_y,
|
||||
0,
|
||||
0,
|
||||
$new_width,
|
||||
$new_height,
|
||||
$img_width,
|
||||
$img_height
|
||||
) && $write_func($new_img, $new_file_path, $image_quality);
|
||||
$this->gd_set_image_object($file_path, $new_img);
|
||||
return $success;
|
||||
}
|
||||
|
||||
protected function imagick_get_image_object($file_path, $no_cache = false) {
|
||||
if (empty($this->image_objects[$file_path]) || $no_cache) {
|
||||
$this->imagick_destroy_image_object($file_path);
|
||||
$image = new \Imagick();
|
||||
if (!empty($this->options['imagick_resource_limits'])) {
|
||||
foreach ($this->options['imagick_resource_limits'] as $type => $limit) {
|
||||
$image->setResourceLimit($type, $limit);
|
||||
}
|
||||
}
|
||||
$image->readImage($file_path);
|
||||
$this->image_objects[$file_path] = $image;
|
||||
}
|
||||
return $this->image_objects[$file_path];
|
||||
}
|
||||
|
||||
protected function imagick_set_image_object($file_path, $image) {
|
||||
$this->imagick_destroy_image_object($file_path);
|
||||
$this->image_objects[$file_path] = $image;
|
||||
}
|
||||
|
||||
protected function imagick_destroy_image_object($file_path) {
|
||||
$image = @$this->image_objects[$file_path];
|
||||
return $image && $image->destroy();
|
||||
}
|
||||
|
||||
protected function imagick_orient_image($image) {
|
||||
$orientation = $image->getImageOrientation();
|
||||
$background = new \ImagickPixel('none');
|
||||
switch ($orientation) {
|
||||
case \imagick::ORIENTATION_TOPRIGHT: // 2
|
||||
$image->flopImage(); // horizontal flop around y-axis
|
||||
break;
|
||||
case \imagick::ORIENTATION_BOTTOMRIGHT: // 3
|
||||
$image->rotateImage($background, 180);
|
||||
break;
|
||||
case \imagick::ORIENTATION_BOTTOMLEFT: // 4
|
||||
$image->flipImage(); // vertical flip around x-axis
|
||||
break;
|
||||
case \imagick::ORIENTATION_LEFTTOP: // 5
|
||||
$image->flopImage(); // horizontal flop around y-axis
|
||||
$image->rotateImage($background, 270);
|
||||
break;
|
||||
case \imagick::ORIENTATION_RIGHTTOP: // 6
|
||||
$image->rotateImage($background, 90);
|
||||
break;
|
||||
case \imagick::ORIENTATION_RIGHTBOTTOM: // 7
|
||||
$image->flipImage(); // vertical flip around x-axis
|
||||
$image->rotateImage($background, 270);
|
||||
break;
|
||||
case \imagick::ORIENTATION_LEFTBOTTOM: // 8
|
||||
$image->rotateImage($background, 270);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
$image->setImageOrientation(\imagick::ORIENTATION_TOPLEFT); // 1
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function imagick_create_scaled_image($file_name, $version, $options) {
|
||||
list($file_path, $new_file_path) =
|
||||
$this->get_scaled_image_file_paths($file_name, $version);
|
||||
$image = $this->imagick_get_image_object(
|
||||
$file_path,
|
||||
!empty($options['no_cache'])
|
||||
);
|
||||
if ($image->getImageFormat() === 'GIF') {
|
||||
// Handle animated GIFs:
|
||||
$images = $image->coalesceImages();
|
||||
foreach ($images as $frame) {
|
||||
$image = $frame;
|
||||
$this->imagick_set_image_object($file_name, $image);
|
||||
break;
|
||||
}
|
||||
}
|
||||
$image_oriented = false;
|
||||
if (!empty($options['auto_orient'])) {
|
||||
$image_oriented = $this->imagick_orient_image($image);
|
||||
}
|
||||
$new_width = $max_width = $img_width = $image->getImageWidth();
|
||||
$new_height = $max_height = $img_height = $image->getImageHeight();
|
||||
if (!empty($options['max_width'])) {
|
||||
$new_width = $max_width = $options['max_width'];
|
||||
}
|
||||
if (!empty($options['max_height'])) {
|
||||
$new_height = $max_height = $options['max_height'];
|
||||
}
|
||||
if (!($image_oriented || $max_width < $img_width || $max_height < $img_height)) {
|
||||
if ($file_path !== $new_file_path) {
|
||||
return copy($file_path, $new_file_path);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
$crop = !empty($options['crop']);
|
||||
if ($crop) {
|
||||
$x = 0;
|
||||
$y = 0;
|
||||
if (($img_width / $img_height) >= ($max_width / $max_height)) {
|
||||
$new_width = 0; // Enables proportional scaling based on max_height
|
||||
$x = ($img_width / ($img_height / $max_height) - $max_width) / 2;
|
||||
} else {
|
||||
$new_height = 0; // Enables proportional scaling based on max_width
|
||||
$y = ($img_height / ($img_width / $max_width) - $max_height) / 2;
|
||||
}
|
||||
}
|
||||
$success = $image->resizeImage(
|
||||
$new_width,
|
||||
$new_height,
|
||||
isset($options['filter']) ? $options['filter'] : \imagick::FILTER_LANCZOS,
|
||||
isset($options['blur']) ? $options['blur'] : 1,
|
||||
$new_width && $new_height // fit image into constraints if not to be cropped
|
||||
);
|
||||
if ($success && $crop) {
|
||||
$success = $image->cropImage(
|
||||
$max_width,
|
||||
$max_height,
|
||||
$x,
|
||||
$y
|
||||
);
|
||||
if ($success) {
|
||||
$success = $image->setImagePage($max_width, $max_height, 0, 0);
|
||||
}
|
||||
}
|
||||
$type = strtolower(substr(strrchr($file_name, '.'), 1));
|
||||
switch ($type) {
|
||||
case 'jpg':
|
||||
case 'jpeg':
|
||||
if (!empty($options['jpeg_quality'])) {
|
||||
$image->setImageCompression(\imagick::COMPRESSION_JPEG);
|
||||
$image->setImageCompressionQuality($options['jpeg_quality']);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (!empty($options['strip'])) {
|
||||
$image->stripImage();
|
||||
}
|
||||
return $success && $image->writeImage($new_file_path);
|
||||
}
|
||||
|
||||
protected function imagemagick_create_scaled_image($file_name, $version, $options) {
|
||||
list($file_path, $new_file_path) =
|
||||
$this->get_scaled_image_file_paths($file_name, $version);
|
||||
$resize = @$options['max_width']
|
||||
.(empty($options['max_height']) ? '' : 'X'.$options['max_height']);
|
||||
if (!$resize && empty($options['auto_orient'])) {
|
||||
if ($file_path !== $new_file_path) {
|
||||
return copy($file_path, $new_file_path);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
$cmd = $this->options['convert_bin'];
|
||||
if (!empty($this->options['convert_params'])) {
|
||||
$cmd .= ' '.$this->options['convert_params'];
|
||||
}
|
||||
$cmd .= ' '.escapeshellarg($file_path);
|
||||
if (!empty($options['auto_orient'])) {
|
||||
$cmd .= ' -auto-orient';
|
||||
}
|
||||
if ($resize) {
|
||||
// Handle animated GIFs:
|
||||
$cmd .= ' -coalesce';
|
||||
if (empty($options['crop'])) {
|
||||
$cmd .= ' -resize '.escapeshellarg($resize.'>');
|
||||
} else {
|
||||
$cmd .= ' -resize '.escapeshellarg($resize.'^');
|
||||
$cmd .= ' -gravity center';
|
||||
$cmd .= ' -crop '.escapeshellarg($resize.'+0+0');
|
||||
}
|
||||
// Make sure the page dimensions are correct (fixes offsets of animated GIFs):
|
||||
$cmd .= ' +repage';
|
||||
}
|
||||
if (!empty($options['convert_params'])) {
|
||||
$cmd .= ' '.$options['convert_params'];
|
||||
}
|
||||
$cmd .= ' '.escapeshellarg($new_file_path);
|
||||
exec($cmd, $output, $error);
|
||||
if ($error) {
|
||||
error_log(implode('\n', $output));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function get_image_size($file_path) {
|
||||
if ($this->options['image_library']) {
|
||||
if (extension_loaded('imagick')) {
|
||||
$image = new \Imagick();
|
||||
try {
|
||||
if (@$image->pingImage($file_path)) {
|
||||
$dimensions = array($image->getImageWidth(), $image->getImageHeight());
|
||||
$image->destroy();
|
||||
return $dimensions;
|
||||
}
|
||||
return false;
|
||||
} catch (Exception $e) {
|
||||
error_log($e->getMessage());
|
||||
}
|
||||
}
|
||||
if ($this->options['image_library'] === 2) {
|
||||
$cmd = $this->options['identify_bin'];
|
||||
$cmd .= ' -ping '.escapeshellarg($file_path);
|
||||
exec($cmd, $output, $error);
|
||||
if (!$error && !empty($output)) {
|
||||
// image.jpg JPEG 1920x1080 1920x1080+0+0 8-bit sRGB 465KB 0.000u 0:00.000
|
||||
$infos = preg_split('/\s+/', $output[0]);
|
||||
$dimensions = preg_split('/x/', $infos[2]);
|
||||
return $dimensions;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!function_exists('getimagesize')) {
|
||||
error_log('Function not found: getimagesize');
|
||||
return false;
|
||||
}
|
||||
return @getimagesize($file_path);
|
||||
}
|
||||
|
||||
protected function create_scaled_image($file_name, $version, $options) {
|
||||
if ($this->options['image_library'] === 2) {
|
||||
return $this->imagemagick_create_scaled_image($file_name, $version, $options);
|
||||
}
|
||||
if ($this->options['image_library'] && extension_loaded('imagick')) {
|
||||
return $this->imagick_create_scaled_image($file_name, $version, $options);
|
||||
}
|
||||
return $this->gd_create_scaled_image($file_name, $version, $options);
|
||||
}
|
||||
|
||||
protected function destroy_image_object($file_path) {
|
||||
if ($this->options['image_library'] && extension_loaded('imagick')) {
|
||||
return $this->imagick_destroy_image_object($file_path);
|
||||
}
|
||||
}
|
||||
|
||||
protected function is_valid_image_file($file_path) {
|
||||
if (!preg_match($this->options['image_file_types'], $file_path)) {
|
||||
protected function is_valid_msq_file($file_path) {
|
||||
/*if (!preg_match($this->options['image_file_types'], $file_path)) {
|
||||
return false;
|
||||
}
|
||||
if (function_exists('exif_imagetype')) {
|
||||
return @exif_imagetype($file_path);
|
||||
}
|
||||
$image_info = $this->get_image_size($file_path);
|
||||
return $image_info && $image_info[0] && $image_info[1];
|
||||
}
|
||||
|
||||
protected function handle_image_file($file_path, $file) {
|
||||
$failed_versions = array();
|
||||
foreach($this->options['image_versions'] as $version => $options) {
|
||||
if ($this->create_scaled_image($file->name, $version, $options)) {
|
||||
if (!empty($version)) {
|
||||
$file->{$version.'Url'} = $this->get_download_url(
|
||||
$file->name,
|
||||
$version
|
||||
);
|
||||
} else {
|
||||
$file->size = $this->get_file_size($file_path, true);
|
||||
}
|
||||
} else {
|
||||
$failed_versions[] = $version ? $version : 'original';
|
||||
}
|
||||
}
|
||||
if (count($failed_versions)) {
|
||||
$file->error = $this->get_error_message('image_resize')
|
||||
.' ('.implode($failed_versions,', ').')';
|
||||
}
|
||||
// Free memory:
|
||||
$this->destroy_image_object($file_path);
|
||||
$image_info = $this->get_image_size($file_path);*/
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
|
||||
|
@ -1056,9 +431,6 @@ class UploadHandler
|
|||
$file_size = $this->get_file_size($file_path, $append_file);
|
||||
if ($file_size === $file->size) {
|
||||
$file->url = $this->get_download_url($file->name);
|
||||
if ($this->is_valid_image_file($file_path)) {
|
||||
$this->handle_image_file($file_path, $file);
|
||||
}
|
||||
} else {
|
||||
$file->size = $file_size;
|
||||
if (!$content_range && $this->options['discard_aborted_uploads']) {
|
||||
|
@ -1147,13 +519,11 @@ class UploadHandler
|
|||
|
||||
protected function get_file_type($file_path) {
|
||||
switch (strtolower(pathinfo($file_path, PATHINFO_EXTENSION))) {
|
||||
case 'jpeg':
|
||||
case 'jpg':
|
||||
return 'image/jpeg';
|
||||
case 'png':
|
||||
return 'image/png';
|
||||
case 'gif':
|
||||
return 'image/gif';
|
||||
case 'zip':
|
||||
case 'tar':
|
||||
case 'gzip':
|
||||
case 'bz2':
|
||||
return '';
|
||||
case 'msq':
|
||||
return 'application/xml';
|
||||
default:
|
||||
|
@ -1315,16 +685,6 @@ class UploadHandler
|
|||
foreach($file_names as $file_name) {
|
||||
$file_path = $this->get_upload_path($file_name);
|
||||
$success = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path);
|
||||
if ($success) {
|
||||
foreach($this->options['image_versions'] as $version => $options) {
|
||||
if (!empty($version)) {
|
||||
$file = $this->get_upload_path($file_name, $version);
|
||||
if (is_file($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$response[$file_name] = $success;
|
||||
}
|
||||
return $this->generate_response($response, $print_response);
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
<html>
|
||||
<head>
|
||||
<?php
|
||||
$filename = null;
|
||||
$msq = null;
|
||||
|
||||
if (!empty($_GET["tune"]))
|
||||
{
|
||||
$filename = htmlspecialchars($_GET["tune"]);
|
||||
echo "<title>$filename</title>";
|
||||
//TODO Need massive security here
|
||||
$msq = simplexml_load_file("files/$filename");
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "No tune file specified.";
|
||||
}
|
||||
?>
|
||||
</head>
|
||||
<body>
|
||||
<?php
|
||||
if ($msq)
|
||||
{
|
||||
/*
|
||||
* <bibliography author="TunerStudio MS 2.0.6 - EFI Analytics, Inc." tuneComment=" <br> <br> <br> <br> <br> <br> " writeDate="Mon Jul 15 09:16:28 EDT 2013"/>
|
||||
* <versionInfo fileFormat="4.0" firmwareInfo="" nPages="15" signature="MS3 Format 0262.09 "/>
|
||||
*/
|
||||
|
||||
//var_dump($msq);
|
||||
echo "Format Version: " . $msq->versionInfo['fileFormat'] . "<br/>";
|
||||
echo "MS Signature: " . $msq->versionInfo['signature'];
|
||||
|
||||
//foreach ($movies->xpath('//settings/setting') as $setting) {
|
||||
// echo $setting->name, 'value: ', $setting->value, PHP_EOL;
|
||||
//}
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "Unable to open: $filename";
|
||||
}
|
||||
?>
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue