Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Model/Behavior/UploadBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ class UploadBehavior extends ModelBehavior {

public function setup(Model $model, $settings = array()) {
$defaults = array(
'gsPath' => "gs",
'path' => ':webroot/upload/:model/:id/:basename_:style.:extension',
'styles' => array(),
'resizeToMaxWidth' => false,
'quality' => 75,
'dpi' => 72,
'PDFtoImage' => false,
'alpha' => false
);

Expand Down Expand Up @@ -139,6 +142,7 @@ private function _writeFiles(&$model) {
if (is_dir($destDir) && is_writable($destDir)) {
$move = !empty($toWrite['remote']) ? 'rename' : 'move_uploaded_file';
if (@$move($toWrite['tmp_name'], $settings['path'])) {
self::_convertPDFtoImageIfPDF($settings, $toWrite);
$this->afterMove($settings['path']); // <==== Calling afterMove() callback method
if($this->maxWidthSize) {
$this->_resize($settings['path'], $settings['path'], $this->maxWidthSize.'w', $settings['quality'], $settings['alpha']);
Expand Down Expand Up @@ -458,6 +462,51 @@ private function _validateDimension($upload, $mode, $axis, $value) {
return false;
}

private function _convertPDFtoImageIfPDF( &$settings, &$toWrite ) {
$srcFile = $settings['path'];
$pdfToImage = $settings['PDFtoImage'];

// pdfToImage: (jpg|png)
// destFile ex: example_thumb.pdf -> example_thum.jpg
$pattern = '/\.pdf$/i';
$destFile = preg_replace($pattern, ".$pdfToImage", $srcFile);
$destFilename = preg_replace($pattern, ".$pdfToImage", $toWrite['name']);

$pathinfo = UploadBehavior::_pathinfo($srcFile);

if (isset($pdfToImage) && preg_match('/(jpg|png)/', $pdfToImage) &&
strtolower($pathinfo['extension']) === 'pdf') {
$result = $this->_convertPDFtoImage($srcFile, $destFile, $settings);
if ($result) {
// change original file to image file
$settings['path'] = $destFile;
$toWrite['name'] = $destFilename;

return true;
}
}

return false;
}

private function _convertPDFtoImage($srcFile, $destFile, $settings) {
$gsPath = $settings['gsPath'];
$imageFileType = $settings['PDFtoImage'];
$dpi = $settings['dpi'];
$quality = $settings['quality'];

if ( $imageFileType == 'jpg' ) {
$deviceType = 'jpeg';
}
else {
$deviceType = $imageFileType;
}
set_time_limit(900); // 15min
exec("{$gsPath} -q -dSAFER -dBATCH -dNOPAUSE -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -sDEVICE={$deviceType} -r{$dpi} -dJPEGQ={$quality} -sOutputFile={$destFile} {$srcFile}", $arr);

return true;
}

public function phpUploadError(Model $model, $value, $uploadErrors = array('UPLOAD_ERR_INI_SIZE', 'UPLOAD_ERR_FORM_SIZE', 'UPLOAD_ERR_PARTIAL', 'UPLOAD_ERR_NO_FILE', 'UPLOAD_ERR_NO_TMP_DIR', 'UPLOAD_ERR_CANT_WRITE', 'UPLOAD_ERR_EXTENSION')) {
$value = array_shift($value);
if (!is_array($uploadErrors)) {
Expand Down
6 changes: 6 additions & 0 deletions View/Helper/UploadHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public function uploadUrl($data, $field, $options = array())

if(isset($id) && !empty($filename))
{
$settings = UploadBehavior::interpolate($model, $id, $field, $filename, $options['style'], array('webroot' => ''));
// change output filename's ext
if(isset($settings['PDFtoImage'])) {
$imageFileType = $settings['PDFtoImage'];
$filename = preg_replace('/\.pdf$/', ".$imageFileType", $filename);
}
$settings = UploadBehavior::interpolate($model, $id, $field, $filename, $options['style'], array('webroot' => ''));
$url = isset($settings['url']) ? $settings['url'] : $settings['path'];
}
Expand Down