Skip to content

Commit b09ed80

Browse files
author
Tom Boutell
committed
Merge pull request punkave#70 from psyray/improve-postscript-support
Improve Postcript files support
2 parents 1b1e09c + 6ade267 commit b09ed80

File tree

1 file changed

+45
-30
lines changed

1 file changed

+45
-30
lines changed

BlueImp/UploadHandler.php

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ function __construct($options=null) {
6161
'max_width' => 80,
6262
'max_height' => 80
6363
)
64-
)
64+
),
65+
'ps_file_extensions' => array(
66+
'ai',
67+
'psd',
68+
'pdf',
69+
'eps')
6570
);
6671
if ($options) {
6772
$this->options = array_replace_recursive($this->options, $options);
@@ -118,25 +123,19 @@ protected function create_scaled_image($file_name, $options) {
118123
$new_file_path = $options['upload_dir'].$file_name;
119124

120125
if (extension_loaded('imagick')) {
121-
// Special PSD & AI case
122-
$file_ext = strtolower(substr(strrchr($file_name, '.'), 1));
123-
switch ($file_ext) {
124-
case 'ai':
125-
case 'psd':
126-
case 'pdf':
127-
try {
128-
$im = new \Imagick($file_path);
129-
$im->flattenImages();
130-
$im->setImageFormat('png');
131-
$file_name .= '.png';
132-
$file_path .= '.png';
133-
$new_file_path .= '.png';
134-
$im->writeImage($file_path);
135-
} catch (\ImagickException $e) {
136-
return false;
137-
}
138-
break;
139-
126+
// Special Postscript case
127+
if($this->is_ps_file($file_name)) {
128+
try {
129+
$im = new \Imagick($file_path);
130+
$im->flattenImages();
131+
$im->setImageFormat('png');
132+
$file_name .= '.png';
133+
$file_path .= '.png';
134+
$new_file_path .= '.png';
135+
$im->writeImage($file_path);
136+
} catch (\ImagickException $e) {
137+
return false;
138+
}
140139
}
141140
}
142141

@@ -353,15 +352,9 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
353352
}
354353
$file->url = $this->options['upload_url'].rawurlencode($file->name);
355354
foreach($this->options['image_versions'] as $version => $options) {
356-
$thumbnail_ext = '';
357355
if ($this->create_scaled_image($file->name, $options)) {
358-
if ($this->options['upload_dir'] !== $options['upload_dir']) {
359-
$file_ext = strtolower(substr(strrchr($file->name, '.'), 1));
360-
if($file_ext == 'ai' || $file_ext == 'psd' || $file_ext == 'pdf') {
361-
$thumbnail_ext = '.png';
362-
}
363-
364-
$file->{$version.'_url'} = $options['upload_url'].rawurlencode($file->name).$thumbnail_ext;
356+
if ($this->options['upload_dir'] !== $options['upload_dir']) {
357+
$file->{$version.'_url'} = $this->add_png_to_ps_extension($options['upload_url'].rawurlencode($file->name));
365358
} else {
366359
clearstatcache();
367360
$file_size = filesize($file_path);
@@ -452,9 +445,15 @@ public function delete() {
452445
basename(stripslashes($_REQUEST['file'])) : null;
453446
$file_path = $this->options['upload_dir'].$file_name;
454447
$success = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path);
448+
// If Postcript file, remove original PNG file used to generate thumbnails
449+
if($success && $this->is_ps_file($file_name)) {
450+
$file_path_ps = $this->add_png_to_ps_extension($file_path);
451+
if(is_file($file_path_ps))
452+
unlink($file_path_ps);
453+
}
455454
if ($success) {
456455
foreach($this->options['image_versions'] as $version => $options) {
457-
$file = $options['upload_dir'].$file_name;
456+
$file = $this->add_png_to_ps_extension($options['upload_dir'].$file_name);
458457
if (is_file($file)) {
459458
unlink($file);
460459
}
@@ -464,4 +463,20 @@ public function delete() {
464463
echo json_encode($success);
465464
}
466465

467-
}
466+
public function add_png_to_ps_extension($filename) {
467+
$thumbnail_ext = '';
468+
// For Postscript files, add png extension
469+
if($this->is_ps_file($filename)) {
470+
$thumbnail_ext = '.png';
471+
}
472+
return $filename.$thumbnail_ext;
473+
}
474+
475+
public function is_ps_file($filename) {
476+
$file_ext = strtolower(substr(strrchr($filename, '.'), 1));
477+
if(in_array($file_ext, $this->options['ps_file_extensions']))
478+
return true;
479+
else
480+
return false;
481+
}
482+
}

0 commit comments

Comments
 (0)