public function generatePdfImages($fileName, $filePages) { $pages = 0; $json = array(); if (file_exists(DIR_DOWNLOAD . $this->config->get('msconf_temp_download_path') . $fileName)) { if (preg_match('/[^-0-9,]/', $filePages)) { $json['errors'][] = $this->language->get('ms_error_product_invalid_pdf_range'); } else { $offsets = explode(',', $filePages); foreach ($offsets as $offset) { if (!preg_match('/^[0-9]+(-[0-9]+)?$/', $offset)) { $json['errors'][] = $this->language->get('ms_error_product_invalid_pdf_range'); break; } } } if (!empty($json['errors'])) { return $json; } $pathinfo = pathinfo(DIR_DOWNLOAD . $this->config->get('msconf_temp_download_path') . $fileName); $list = glob(DIR_IMAGE . $this->config->get('msconf_temp_image_path') . $pathinfo['filename'] . '*\\.png'); //var_dump($list); foreach ($list as $pagePreview) { //var_dump('unlinking ' . $pagePreview); @unlink($pagePreview); } $name = DIR_DOWNLOAD . $this->config->get('msconf_temp_download_path') . $fileName . "[" . $filePages . "]"; $im = new imagick($name); $pages = $im->getNumberImages(); $im->setImageFormat("png"); $im->setImageCompressionQuality(100); $pathinfo = pathinfo(DIR_DOWNLOAD . $this->config->get('msconf_temp_download_path') . $fileName); $json['token'] = substr($pathinfo['basename'], 0, strrpos($pathinfo['basename'], '.')); if ($im->writeImages(DIR_IMAGE . $this->config->get('msconf_temp_image_path') . $pathinfo['filename'] . '.png', false)) { $list = glob(DIR_IMAGE . $this->config->get('msconf_temp_image_path') . $pathinfo['filename'] . '*\\.png'); foreach ($list as $pagePreview) { $pathinfo = pathinfo($pagePreview); $this->session->data['multiseller']['files'][] = $pathinfo['basename']; $thumb = $this->resizeImage($this->config->get('msconf_temp_image_path') . $pathinfo['basename'], $this->config->get('msconf_image_preview_width'), $this->config->get('msconf_image_preview_height')); $json['images'][] = array('name' => $pathinfo['basename'], 'thumb' => $thumb); } //var_dump($this->session->data['multiseller']['files']); return $json; } } }
/** * Rotate image * * @param string $path image file * @param int $degree rotete degrees * @param string $bgcolor square background color in #rrggbb format * @param string $destformat image destination format * @return string|false * @author nao-pon * @author Troex Nevelin **/ protected function imgRotate($path, $degree, $bgcolor = '#ffffff', $destformat = null) { if (($s = @getimagesize($path)) == false || $degree % 360 === 0) { return false; } $result = false; // try lossless rotate if ($degree % 90 === 0 && in_array($s[2], array(IMAGETYPE_JPEG, IMAGETYPE_JPEG2000))) { $count = $degree / 90 % 4; $exiftran = array(1 => '-9', 2 => '-1', 3 => '-2'); $jpegtran = array(1 => '90', 2 => '180', 3 => '270'); $quotedPath = escapeshellarg($path); $cmds = array('exiftran -i ' . $exiftran[$count] . ' ' . $path, 'jpegtran -rotate ' . $jpegtran[$count] . ' -copy all -outfile ' . $quotedPath . ' ' . $quotedPath); foreach ($cmds as $cmd) { if ($this->procExec($cmd) === 0) { $result = true; break; } } if ($result) { return $path; } } switch ($this->imgLib) { case 'imagick': try { $img = new imagick($path); } catch (Exception $e) { return false; } if ($img->getNumberImages() > 1) { $img = $img->coalesceImages(); do { $img->rotateImage(new ImagickPixel($bgcolor), $degree); } while ($img->nextImage()); $img = $img->optimizeImageLayers(); $result = $img->writeImages($path, true); } else { $img->rotateImage(new ImagickPixel($bgcolor), $degree); $result = $img->writeImage($path); } $img->destroy(); return $result ? $path : false; break; case 'gd': $img = self::gdImageCreate($path, $s['mime']); $degree = 360 - $degree; list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x"); $bgcolor = imagecolorallocate($img, $r, $g, $b); $tmp = imageRotate($img, $degree, (int) $bgcolor); $result = self::gdImage($tmp, $path, $destformat, $s['mime']); imageDestroy($img); imageDestroy($tmp); return $result ? $path : false; break; } return false; }
public function jxUploadDownloads() { $json = array(); $file = array(); $json['errors'] = $this->MsLoader->MsFile->checkPostMax($_POST, $_FILES); if ($json['errors']) { return $this->response->setOutput(json_encode($json)); } // allow a maximum of N images $msconf_downloads_limits = $this->config->get('msconf_downloads_limits'); foreach ($_FILES as $file) { if ($msconf_downloads_limits[1] > 0 && $this->request->post['fileCount'] >= $msconf_downloads_limits[1]) { $json['errors'][] = sprintf($this->language->get('ms_error_product_download_maximum'), $msconf_downloads_limits[1]); $json['cancel'] = 1; $this->response->setOutput(json_encode($json)); return; } else { $errors = $this->MsLoader->MsFile->checkDownload($file); if ($errors) { $json['errors'] = array_merge($json['errors'], $errors); } else { $fileData = $this->MsLoader->MsFile->uploadDownload($file); if ($this->config->get('msconf_enable_pdf_generator') && extension_loaded('imagick')) { $ext = explode('.', $file['name']); $ext = end($ext); if (strtolower($ext) == 'pdf') { $im = new imagick(DIR_DOWNLOAD . $this->config->get('msconf_temp_download_path') . $fileData['fileName']); $pages = $im->getNumberImages() - 1; } } $json['files'][] = array('fileName' => $fileData['fileName'], 'fileMask' => $fileData['fileMask'], 'filePages' => isset($pages) ? $pages : ''); } } } return $this->response->setOutput(json_encode($json)); }
// maximum amount of memory map to allocate for the pixel cache IMagick::setResourceLimit(imagick::RESOURCETYPE_MAP, 256); include "conn.php"; mkdir("../upload/"); $series = $_REQUEST['series']; $publisher = $_REQUEST['publisher']; $file_name = explode(".", $_FILES['file']['name']); $fname = md5($file_name[1] . date() . rand(0, 100000)) . "." . $file_name[1]; while (file_exists("../upload/" . $fname)) { $fname = md5($file_name[1] . date() . rand(0, 100000)) . "." . $file_name[1]; } move_uploaded_file($_FILES["file"]["tmp_name"], "../upload/" . $fname); $img = new imagick(); $img->setResolution(600, 600); $img->readimage("../upload/" . $fname); $pdfLength = $img->getNumberImages(); $sql = "INSERT INTO `magazine` (`size`, `series`, `issue`, `createtime`, `isnew`, `status`) VALUES (" . $pdfLength . "," . $series . ",0, '" . date("Y-m-d h:i:s") . "', 1, 0)"; mysql_query($sql, $conn); $id = mysql_insert_id($conn); if (!file_exists("../../magazine/")) { mkdir("../../magazine/"); } mkdir("../../magazine/" . $id); mkdir("../../magazine/" . $id . "/small"); $img->setImageFormat('jpeg'); for ($i = 0; $i < $pdfLength; $i++) { $img->setIteratorIndex($i); $img->scaleImage(1000, 0); $fname = md5($file_name[1] . date() . rand(0, 100000)) . ".jpg"; while (file_exists("../../magazine/" . $id . "/" . $fname)) { $fname = md5($file_name[1] . date() . rand(0, 100000)) . ".jpg";
/** * Rotate image * * @param string $path image file * @param int $degree rotete degrees * @param string $bgcolor square background color in #rrggbb format * @param string $destformat image destination format * @param int $jpgQuality JEPG quality (1-100) * @return string|false * @author nao-pon * @author Troex Nevelin **/ protected function imgRotate($path, $degree, $bgcolor = '#ffffff', $destformat = null, $jpgQuality = null) { if (($s = getimagesize($path)) == false || $degree % 360 === 0) { return false; } $result = false; // try lossless rotate if ($degree % 90 === 0 && in_array($s[2], array(IMAGETYPE_JPEG, IMAGETYPE_JPEG2000))) { $count = $degree / 90 % 4; $exiftran = array(1 => '-9', 2 => '-1', 3 => '-2'); $jpegtran = array(1 => '90', 2 => '180', 3 => '270'); $quotedPath = escapeshellarg($path); $cmds = array('exiftran -i ' . $exiftran[$count] . ' ' . $path, 'jpegtran -rotate ' . $jpegtran[$count] . ' -copy all -outfile ' . $quotedPath . ' ' . $quotedPath); foreach ($cmds as $cmd) { if ($this->procExec($cmd) === 0) { $result = true; break; } } if ($result) { return $path; } } if (!$jpgQuality) { $jpgQuality = $this->options['jpgQuality']; } elFinder::extendTimeLimit(300); switch ($this->imgLib) { case 'imagick': try { $img = new imagick($path); } catch (Exception $e) { return false; } if ($s[2] === IMAGETYPE_GIF || $s[2] === IMAGETYPE_PNG) { $bgcolor = 'rgba(255, 255, 255, 0.0)'; } if ($img->getNumberImages() > 1) { $img = $img->coalesceImages(); do { $img->rotateImage(new ImagickPixel($bgcolor), $degree); } while ($img->nextImage()); $img = $img->optimizeImageLayers(); $result = $img->writeImages($path, true); } else { $img->rotateImage(new ImagickPixel($bgcolor), $degree); $result = $this->imagickImage($img, $path, $destformat, $jpgQuality); } $img->clear(); return $result ? $path : false; break; case 'convert': extract($this->imageMagickConvertPrepare($path, $destformat, $jpgQuality, $s)); if ($s[2] === IMAGETYPE_GIF || $s[2] === IMAGETYPE_PNG) { $bgcolor = 'rgba(255, 255, 255, 0.0)'; } $cmd = sprintf('convert %s%s%s -background "%s" -rotate %d%s %s', $quotedPath, $coalesce, $jpgQuality, $bgcolor, $degree, $deconstruct, $quotedDstPath); $result = false; if ($this->procExec($cmd) === 0) { $result = true; } return $result ? $path : false; break; case 'gd': $img = $this->gdImageCreate($path, $s['mime']); $degree = 360 - $degree; $bgNum = -1; $bgIdx = false; if ($s[2] === IMAGETYPE_GIF) { $bgIdx = imagecolortransparent($img); if ($bgIdx !== -1) { $c = imagecolorsforindex($img, $bgIdx); $w = imagesx($img); $h = imagesy($img); $newImg = imagecreatetruecolor($w, $h); imagepalettecopy($newImg, $img); $bgNum = imagecolorallocate($newImg, $c['red'], $c['green'], $c['blue']); imagefill($newImg, 0, 0, $bgNum); imagecolortransparent($newImg, $bgNum); imagecopy($newImg, $img, 0, 0, 0, 0, $w, $h); imagedestroy($img); $img = $newImg; $newImg = null; } } else { if ($s[2] === IMAGETYPE_PNG) { $bgNum = imagecolorallocatealpha($img, 255, 255, 255, 127); } } if ($bgNum === -1) { list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x"); $bgNum = imagecolorallocate($img, $r, $g, $b); } $tmp = imageRotate($img, $degree, $bgNum); if ($bgIdx !== -1) { imagecolortransparent($tmp, $bgNum); } $result = $this->gdImage($tmp, $path, $destformat, $s['mime'], $jpgQuality); imageDestroy($img); imageDestroy($tmp); return $result ? $path : false; break; } return false; }
/** * Convert to output format. This method convert from pdf to specified format with optimizing * @throw Exception * @access public * @param string $outputPath - path to file. May content only path or path with filename * @param int/string[=ALL] $page - number of document page wich will be converted into image. If specified 'ALL' - will be converted all pages. * @param string $format - output format (see self::getFormats()) * @param array $resolution - array with x&y resolution DPI * @param int $depth - bit depth image * @return string/bool[false] - return image path of last converted page */ public function convert($outputPath = '', $page = 'ALL', $format = 'png', $resolution = array('x' => 300, 'y' => 300), $depth = 8) { if (!Imagick::queryFormats(strtoupper($format))) { throw new Exception('Unsupported format'); } $startTime = microtime(true); $im = new imagick(); $im->setResolution($resolution['x'], $resolution['y']); $format = strtolower($format); $im->setFormat($format); if ($outputPath) { if (is_dir($outputPath)) { $outputPath = $outputPath . pathinfo($this->filePathWithoutType, PATHINFO_FILENAME); } $outputFileName = $outputPath; } else { $outputFileName = $this->filePathWithoutType; } if ($page === 'ALL') { $im->readImage($this->filePathWithoutType . '.pdf'); $im->setImageFormat($format); $im->setImageAlphaChannel(11); // it's a new constant imagick::ALPHACHANNEL_REMOVE $im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); $im->setOption($format . ':bit-depth', $depth); $im->writeImages($outputFileName . "." . $format, false); $logString = '[POINT] File "' . $this->filePathWithoutType . '.pdf" converted to "' . $format . '" with ' . $im->getNumberImages() . ' pages (ex: ' . (microtime(true) - $startTime) . 's)'; $this->setLog($logString); //Optimizing if ($format == 'png' && $this->optipngChecking()) { $startTime = microtime(true); for ($page = $i = 0; $i < $im->getNumberImages(); $i++) { $this->execute('optipng -o=5', $outputFileName . "-" . (int) $i . "." . $format); } $logString = '[POINT] Files "' . $outputFileName . '-x.' . $format . '" optimized (ex: ' . (microtime(true) - $startTime) . 's)'; $this->setLog($logString); } } else { $im->readImage($this->filePathWithoutType . '.pdf[' . (int) $page . ']'); $im->setImageFormat($format); $im->setImageAlphaChannel(11); // it's a new constant imagick::ALPHACHANNEL_REMOVE $im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); $im->setOption($format . ':color-type', 2); $im->setOption($format . ':bit-depth', $depth); $im->writeImage($outputFileName . "-" . (int) $page . "." . $format); $logString = '[POINT] File "' . $outputFileName . '.pdf" converted to "' . $format . '" one page (ex: ' . (microtime(true) - $startTime) . 's)'; $this->setLog($logString); //Optimizing if ($format == 'png' && $this->optipngChecking()) { $startTime = microtime(true); $this->execute('optipng -o=5', $outputFileName . "-" . (int) $page . "." . $format); $logString = '[POINT] File "' . $outputFileName . "-" . (int) $page . "." . $format . '" optimized (ex: ' . (microtime(true) - $startTime) . 's)'; $this->setLog($logString); } } if (file_exists($outputFileName . "-" . (int) $page . "." . $format)) { return $outputFileName . "-" . (int) $page . "." . $format; } else { return false; } }
function getNumPagesPdf($filepath) { $fp = @fopen(preg_replace("/\\[(.*?)\\]/i", "", $filepath), "r"); $max = 0; while (!feof($fp)) { $line = fgets($fp, 255); if (preg_match('/\\/Count [0-9]+/', $line, $matches)) { preg_match('/[0-9]+/', $matches[0], $matches2); if ($max < $matches2[0]) { $max = $matches2[0]; } } } fclose($fp); if ($max == 0) { $im = new imagick($filepath); $max = $im->getNumberImages(); } return $max; }