/** * Create new re-sized version of the original image. * * @param string $file Incoming file * @param string $folder Folder for the new image. * @param string $filename Filename for the new image. * @param int $maxWidth Maximum width for the image. * @param int $maxHeight Maximum height for the image. * @param int $quality Quality for the file (1-100). * @param int $scale See available KunenaImage constants. * @param int $crop Define if you want crop the image. * * @return bool True on success. */ public static function version($file, $folder, $filename, $maxWidth = 800, $maxHeight = 800, $quality = 70, $scale = KunenaImage::SCALE_INSIDE, $crop = 0) { try { // Create target directory if it does not exist. if (!KunenaFolder::exists($folder) && !KunenaFolder::create($folder)) { return false; } // Make sure that index.html exists in the folder. KunenaFolder::createIndex($folder); $info = KunenaImage::getImageFileProperties($file); if ($info->width > $maxWidth || $info->height > $maxHeight) { // Make sure that quality is in allowed range. if ($quality < 1 || $quality > 100) { $quality = 70; } // Calculate quality for PNG. if ($info->type == IMAGETYPE_PNG) { $quality = intval(($quality - 1) / 10); } $options = array('quality' => $quality); // Resize image and copy it to temporary file. $image = new KunenaImage($file); if ($crop && $info->width > $info->height) { $image = $image->resize($info->width * $maxHeight / $info->height, $maxHeight, false, $scale); $image = $image->crop($maxWidth, $maxHeight); } elseif ($crop && $info->width < $info->height) { $image = $image->resize($maxWidth, $info->height * $maxWidth / $info->width, false, $scale); $image = $image->crop($maxWidth, $maxHeight); } else { $image = $image->resize($maxWidth, $maxHeight, false, $scale); } $temp = KunenaPath::tmpdir() . '/kunena_' . md5(rand()); $image->toFile($temp, $info->type, $options); unset($image); // Move new file to its proper location. if (!KunenaFile::move($temp, "{$folder}/{$filename}")) { unlink($temp); return false; } } else { // Copy original file to the new location. if (!KunenaFile::copy($file, "{$folder}/{$filename}")) { return false; } } } catch (Exception $e) { return false; } return true; }
public static function version($file, $newpath, $newfile, $maxwidth = 800, $maxheight = 800, $quality = 70, $scale = CKunenaImage::SCALE_INSIDE) { require_once KPATH_SITE . '/lib/kunena.file.class.php'; // create upload directory if it does not exist $imageinfo = self::getProperties($file); if (!$imageinfo) { return false; } if (!JFolder::exists($newpath)) { if (!JFolder::create($newpath)) { return false; } } KunenaFolder::createIndex($newpath); if ($imageinfo->width > $maxwidth || $imageinfo->height > $maxheight) { $image = new CKunenaImage($file); if ($image->getError()) { return false; } if ($quality < 1 || $quality > 100) { $quality = 70; } $options = array('quality' => $quality); $image = $image->resize($maxwidth, $maxheight, true, $scale); $type = $image->getType(); $temp = KunenaPath::tmpdir() . '/kunena_' . md5(rand()); $image->toFile($temp, $type, $options); unset($image); if (!KunenaFile::move($temp, $newpath . '/' . $newfile)) { unlink($temp); return false; } } else { if (!KunenaFile::copy($file, $newpath . '/' . $newfile)) { return false; } } return true; }
/** * */ public function install() { $tmp = JPATH_ROOT . '/tmp/'; $tmp_kunena = JPATH_ROOT . '/tmp/kinstall/'; $dest = KPATH_SITE . '/template/'; $file = $this->app->input->files->get('install_package', null, 'raw'); if (!JSession::checkToken('post')) { $this->app->enqueueMessage(JText::_('COM_KUNENA_ERROR_TOKEN'), 'error'); $this->setRedirect(KunenaRoute::_($this->baseurl, false)); return; } if (empty($file['tmp_name']) || !is_uploaded_file($file['tmp_name']) || !empty($file['error'])) { $this->app->enqueueMessage(JText::sprintf('COM_KUNENA_A_TEMPLATE_MANAGER_INSTALL_EXTRACT_MISSING', $this->escape($file['name'])), 'notice'); } else { $success = KunenaFile::upload($file['tmp_name'], $tmp . $file['name'], false, true); if ($success) { try { JArchive::extract($tmp . $file['name'], $tmp_kunena); } catch (Exception $e) { $this->app->enqueueMessage(JText::sprintf('COM_KUNENA_A_TEMPLATE_MANAGER_INSTALL_EXTRACT_FAILED', $this->escape($file['name'])), 'notice'); } } if (is_dir($tmp_kunena)) { $templates = KunenaTemplateHelper::parseXmlFiles($tmp_kunena); if (!empty($templates)) { foreach ($templates as $template) { // Never overwrite locked templates if (in_array($template->directory, $this->locked)) { continue; } if (is_dir($dest . $template->directory)) { if (is_file($dest . $template->directory . '/params.ini')) { if (is_file($tmp_kunena . $template->sourcedir . '/params.ini')) { KunenaFile::delete($tmp_kunena . $template->sourcedir . '/params.ini'); } KunenaFile::move($dest . $template->directory . '/params.ini', $tmp_kunena . $template->sourcedir . '/params.ini'); } KunenaFolder::delete($dest . $template->directory); } $success = KunenaFolder::move($tmp_kunena . $template->sourcedir, $dest . $template->directory); if ($success !== true) { $this->app->enqueueMessage(JText::sprintf('COM_KUNENA_A_TEMPLATE_MANAGER_INSTALL_FAILED', $template->directory), 'notice'); } else { $this->app->enqueueMessage(JText::sprintf('COM_KUNENA_A_TEMPLATE_MANAGER_INSTALL_SUCCESS', $template->directory)); } } // Delete the tmp install directory if (is_dir($tmp_kunena)) { KunenaFolder::delete($tmp_kunena); } // Clear all cache, just in case. KunenaCacheHelper::clearAll(); } else { $this->app->enqueueMessage(JText::_('COM_KUNENA_A_TEMPLATE_MANAGER_TEMPLATE_MISSING_FILE'), 'error'); } } else { $this->app->enqueueMessage(JText::_('COM_KUNENA_A_TEMPLATE_MANAGER_TEMPLATE') . ' ' . JText::_('COM_KUNENA_A_TEMPLATE_MANAGER_UNINSTALL') . ': ' . JText::_('COM_KUNENA_A_TEMPLATE_MANAGER_DIR_NOT_EXIST'), 'error'); } } $this->setRedirect(KunenaRoute::_($this->baseurl, false)); }