public static function thumbnail($image_path, $thumb_path, $image_name, $thumbnail_width = 0, $thumbnail_height = 0) { require_once JPATH_ROOT . '/jelibs/phpthumb/phpthumb.class.php'; // create phpThumb object $phpThumb = new phpThumb(); // this is very important when using a single object to process multiple images $phpThumb->resetObject(); // set data source $phpThumb->setSourceFilename($image_path . DS . $image_name); // set parameters (see "URL Parameters" in phpthumb.readme.txt) if ($thumbnail_width) { $phpThumb->setParameter('w', $thumbnail_width); } if ($thumbnail_height) { $phpThumb->setParameter('h', $thumbnail_height); } $phpThumb->setParameter('zc', 'l'); // set parameters $phpThumb->setParameter('config_output_format', 'jpeg'); // generate & output thumbnail $output_filename = str_replace('/', DS, $thumb_path) . DS . 't-' . $thumbnail_width . 'x' . $thumbnail_height . '-' . $image_name; # .'_'.$thumbnail_width.'.'.$phpThumb->config_output_format; $capture_raw_data = false; if ($phpThumb->GenerateThumbnail()) { // $output_size_x = ImageSX($phpThumb->gdimg_output); // $output_size_y = ImageSY($phpThumb->gdimg_output); // if ($output_filename || $capture_raw_data) { //// if ($capture_raw_data && $phpThumb->RenderOutput()) { //// // RenderOutput renders the thumbnail data to $phpThumb->outputImageData, not to a file or the browser //// mysql_query("INSERT INTO `table` (`thumbnail`) VALUES ('".mysql_escape_string($phpThumb->outputImageData)."') WHERE (`id` = '".$id."')"); //// } elseif ($phpThumb->RenderToFile($output_filename)) { //// // do something on success //// echo 'Successfully rendered:<br><img src="'.$output_filename.'">'; //// } else { //// // do something with debug/error messages //// echo 'Failed (size='.$thumbnail_width.'):<pre>'.implode("\n\n", $phpThumb->debugmessages).'</pre>'; //// } // $phpThumb->purgeTempFiles(); // } else { $phpThumb->RenderToFile($output_filename); // } } else { // do something with debug/error messages // echo 'Failed (size='.$thumbnail_width.').<br>'; // echo '<div style="background-color:#FFEEDD; font-weight: bold; padding: 10px;">'.$phpThumb->fatalerror.'</div>'; // echo '<form><textarea rows="10" cols="60" wrap="off">'.htmlentities(implode("\n* ", $phpThumb->debugmessages)).'</textarea></form><hr>'; } return $output_filename; }
static function uploadImages($field, $item, $delImage = 0, $itemType = 'albums', $width = 0, $height = 0) { $jFileInput = new JInput($_FILES); $file = $jFileInput->get('jform', array(), 'array'); // If there is no uploaded file, we have a problem... if (!is_array($file)) { // JError::raiseWarning('', 'No file was selected.'); return ''; } // Build the paths for our file to move to the components 'upload' directory $fileName = $file['name'][$field]; $tmp_src = $file['tmp_name'][$field]; $image = ''; $oldImage = ''; $flagDelete = false; // $item = $this->getItem(); // if delete old image checked or upload new file if ($delImage || $fileName) { $oldImage = JPATH_ROOT . DS . str_replace('/', DS, $item->images); // unlink file if (is_file($oldImage)) { @unlink($oldImage); } $flagDelete = true; $image = ''; } $date = date('Y') . DS . date('m') . DS . date('d'); $dest = JPATH_ROOT . DS . 'images' . DS . $itemType . DS . $date . DS . $item->id . DS; // Make directory @mkdir($dest, 0777, true); if (isset($fileName) && $fileName) { $filepath = JPath::clean($dest . $fileName); /* if (JFile::exists($filepath)) { JError::raiseWarning(100, JText::_('COM_MEDIA_ERROR_FILE_EXISTS')); // File exists } */ // Move uploaded file jimport('joomla.filesystem.file'); if (!JFile::upload($tmp_src, $filepath)) { JError::raiseWarning(100, JText::_('COM_MEDIA_ERROR_UNABLE_TO_UPLOAD_FILE')); // Error in upload return ''; } // if upload success, resize image if ($width) { require_once JPATH_ROOT . DS . 'jelibs/phpthumb/phpthumb.class.php'; // create phpThumb object $phpThumb = new phpThumb(); if (include_once JPATH_ROOT . DS . 'jelibs/phpthumb/phpThumb.config.php') { foreach ($PHPTHUMB_CONFIG as $key => $value) { $keyname = 'config_' . $key; $phpThumb->setParameter($keyname, $value); } } // this is very important when using a single object to process multiple images $phpThumb->resetObject(); $phpThumb->setSourceFilename($filepath); // set parameters (see "URL Parameters" in phpthumb.readme.txt) $phpThumb->setParameter('w', $width); if ($height) { $phpThumb->setParameter('h', $height); } $phpThumb->setParameter('config_output_format', 'jpeg'); // set value to return $image = 'images/' . $itemType . '/' . str_replace(DS, '/', $date) . '/' . $item->id . '/' . $fileName; if ($phpThumb->GenerateThumbnail()) { if ($image) { if (!$phpThumb->RenderToFile($filepath)) { // do something on failed die('Failed (size=' . $width . '):<pre>' . implode("\n\n", $phpThumb->debugmessages) . '</pre>'); } $phpThumb->purgeTempFiles(); } } else { // do something with debug/error messages echo 'Failed (size=' . $width . ').<br>'; echo '<div style="background-color:#FFEEDD; font-weight: bold; padding: 10px;">' . $phpThumb->fatalerror . '</div>'; echo '<form><textarea rows="100" cols="300" wrap="off">' . htmlentities(implode("\n* ", $phpThumb->debugmessages)) . '</textarea></form><hr>'; die; } } else { // set value to return $image = 'images/' . $itemType . '/' . str_replace(DS, '/', $date) . '/' . $item->id . '/' . $fileName; } } else { if (!$flagDelete) { $image = $item->images; } } return $image; }
// // ////////////////////////////////////////////////////////////// // Note: phpThumb.php is where the caching code is located, if // you instantiate your own phpThumb() object that code is // bypassed and it's up to you to handle the reading and // writing of cached files, if appropriate. require_once '../phpthumb.class.php'; // create phpThumb object $phpThumb = new phpThumb(); // create 3 sizes of thumbnail $thumbnail_widths = array(160, 320, 640); $capture_raw_data = false; // set to true to insert to database rather than render to screen or file (see below) foreach ($thumbnail_widths as $thumbnail_width) { // this is very important when using a single object to process multiple images $phpThumb->resetObject(); // set data source -- do this first, any settings must be made AFTER this call $phpThumb->setSourceFilename('images/loco.jpg'); // for static demo only //$phpThumb->setSourceFilename($_FILES['userfile']['tmp_name']); // or $phpThumb->setSourceData($binary_image_data); // or $phpThumb->setSourceImageResource($gd_image_resource); // PLEASE NOTE: // You must set any relevant config settings here. The phpThumb // object mode does NOT pull any settings from phpThumb.config.php //$phpThumb->setParameter('config_document_root', '/home/groups/p/ph/phpthumb/htdocs/'); //$phpThumb->setParameter('config_cache_directory', '/tmp/persistent/phpthumb/cache/'); // set parameters (see "URL Parameters" in phpthumb.readme.txt) $phpThumb->setParameter('w', $thumbnail_width); //$phpThumb->setParameter('h', 100); //$phpThumb->setParameter('fltr', 'gam|1.2');
protected function makeUploadFile($fileKey, $options) { // if no file, return false if (!isset($this->FILES[$fileKey]['name']) || empty($this->FILES[$fileKey]['name'])) { return false; } if ($this->FILES[$fileKey]['error'] != 0) { $haveError = 1; $this->error[] = sprintf(lang::translate('cannot_upload_file_for_'), $this->title(isset($options['label']) ? $options['label'] : $field)); } if ($this->FILES[$fileKey]['size'] > $this->maximulFileSize * 1024) { $haveError = 1; $this->error[] = sprintf(lang::translate('the_file_is_larger_than_'), $this->title(isset($options['label']) ? $options['label'] : $field), number_format($this->maximulFileSize / 1024, 1, '.', '')); } // $this->error can be set upper, from filters or other if (isset($haveError)) { return false; } $newNameOfFile = uniqid(); if (substr($options['file']['location'], -1) != '/') { $options['file']['location'] = $options['file']['location'] . '/'; } $ext = $this->extension($this->FILES[$fileKey]['name']); $_final_newNameOfFile = $this->folder . $options['file']['location'] . $newNameOfFile . '.' . $ext; if (!is_dir($this->folder . $options['file']['location'])) { mkdir($this->folder . $options['file']['location'], 0777) or die('Error at line "' . __LINE__ . '" in method "' . __METHOD__ . '"' . (isset($this->section) ? ', section ' . $this->section : '') . '<br />Cannot create folder.'); } if (false == @copy($this->FILES[$fileKey]['tmp_name'], $_final_newNameOfFile)) { $haveError = 1; $this->error[] = sprintf(lang::translate('cannot_upload_file_for_'), $this->title(isset($options['label']) ? $options['label'] : $field)); } // stop if cannot upload if (isset($haveError)) { return false; } // if we have to resize if (empty($this->error) && isset($options['file']['resize'])) { $phpThumb = new phpThumb(); // create sizes of thumbnail $capture_raw_data = false; // set to true to insert to database rather than render to screen or file (see below) foreach ($options['file']['resize'] as $thumbnailSize => $typeOfResize) { list($_w, $_h) = explode('x', $thumbnailSize); // this is very important when using a single object to process multiple images $phpThumb->resetObject(); // set data source -- do this first, any settings must be made AFTER this call $phpThumb->setSourceFilename($_final_newNameOfFile); // $phpThumb->setSourceFilename($_FILES['userfile']['tmp_name']); // or $phpThumb->setSourceData($binary_image_data); // or $phpThumb->setSourceImageResource($gd_image_resource); // PLEASE NOTE: // You must set any relevant config settings here. The phpThumb // object mode does NOT pull any settings from phpThumb.config.php # $phpThumb->setParameter('config_document_root', $this->folder); //$phpThumb->setParameter('config_cache_directory', '/tmp/persistent/phpthumb/cache/'); // make image rotate if (isset($options['file']['rotate'])) { $phpThumb->ra = (int) $options['file']['rotate']; } if (isset($options['file']['background']) && strlen($options['file']['background']) == 6) { $phpThumb->setParameter('bg', $options['file']['background']); } else { $phpThumb->setParameter('bg', 'FFFFFF'); } if (isset($options['file']['fixed-aspect']) && $typeOfResize == 'resize') { $phpThumb->setParameter('far', 'FFFFFF'); } $phpThumb->setParameter('w', $_w); $phpThumb->setParameter('h', $_h); // make crop, not resize if ($typeOfResize == 'crop') { $phpThumb->setParameter('zc', 'C'); } //$phpThumb->setParameter('fltr', 'gam|1.2'); if (isset($options['file']['watermark'])) { if (is_array($options['file']['watermark']) && isset($options['file']['watermark'][$thumbnailSize])) { $phpThumb->setParameter('fltr', 'wmi|' . $options['file']['watermark'][$thumbnailSize] . '|C|75|20|20'); } else { if (is_file($options['file']['watermark'])) { $phpThumb->setParameter('fltr', 'wmi|' . $options['file']['watermark'] . '|C|75|20|20'); } } } // set options (see phpThumb.config.php) // here you must preface each option with "config_" $phpThumb->setParameter('config_output_format', $ext); // $phpThumb->setParameter('config_imagemagick_path', '/usr/local/bin/convert'); // $phpThumb->setParameter('config_imagemagick_path', IMAGICKPATH); // $phpThumb->setParameter('config_allow_src_above_docroot', true); // needed if you're working outside DOCUMENT_ROOT, in a temp dir for example // generate & output thumbnail $folderToMove = $this->folder . $options['file']['location'] . $thumbnailSize . '/'; if (!is_dir($folderToMove)) { mkdir($folderToMove, 0777) or die('Error at line "' . __LINE__ . '" in method "' . __METHOD__ . '"' . (isset($this->section) ? ', section ' . $this->section : '') . '<br />Cannot create folder at.'); } $output_filename = $folderToMove . $newNameOfFile . '.' . $phpThumb->config_output_format; if ($phpThumb->GenerateThumbnail()) { $output_size_x = ImageSX($phpThumb->gdimg_output); $output_size_y = ImageSY($phpThumb->gdimg_output); $phpThumb->RenderToFile($output_filename); $phpThumb->purgeTempFiles(); } else { $this->error[] = sprintf(lang::translate('fail_cannot_resize_file'), $phpThumb->fatalerror, htmlentities(implode("\n* ", $phpThumb->debugmessages))); @unlink($output_filename); @unlink($_final_newNameOfFile); return false; } } // delete original file after resize if (isset($options['file']['keep-original']) && $options['file']['keep-original'] == false) { @unlink($this->folder . $options['file']['location'] . $newNameOfFile . '.' . $ext); } } return $newNameOfFile . '.' . $ext; }
private function upload($field, $baseDir, $newFileName = false, $deleteOld = false, $oldFilename = '') { $jFileInput = new JInput($_FILES); $fileInput = $jFileInput->get('jform', array(), 'array'); if (empty($fileInput)) { return false; } $field = explode('.', $field); $uploadFileName = $fileInput['name']; $uploadFileTemp = $fileInput['tmp_name']; foreach ($field as $f) { if (!empty($uploadFileName[$f]) && !empty($uploadFileTemp[$f])) { $uploadFileName = $uploadFileName[$f]; $uploadFileTemp = $uploadFileTemp[$f]; } else { $uploadFileName = ''; $uploadFileTemp = ''; break; } } if (empty($uploadFileName)) { return false; } if ($deleteOld && $oldFilename) { $oldFilename = is_array($oldFilename) ? $oldFilename : array($oldFilename); foreach ($oldFilename as $key => &$value) { $value = $baseDir . DS . $oldFilename[$key]; // $oldFilename[$key] = $value; JFile::delete($value); } } $uploadFileExt = JFile::getExt($uploadFileName); if (!is_dir($baseDir)) { if (!mkdir($baseDir, 0777, true)) { return false; } } $fileName = $newFileName ? $newFileName . '.' . $uploadFileExt : $uploadFileName; $filePath = $baseDir . DS . $fileName; $resultUpload = JFile::upload($uploadFileTemp, $filePath); if ($resultUpload) { // resize require_once JPATH_ROOT . '/jelibs/phpthumb/phpthumb.class.php'; // create phpThumb object $phpThumb = new phpThumb(); if (file_exists(JPATH_ROOT . '/jelibs/phpthumb/phpThumb.config.php') && (include_once JPATH_ROOT . '/jelibs/phpthumb/phpThumb.config.php')) { foreach ($PHPTHUMB_CONFIG as $key => $value) { $keyname = 'config_' . $key; $phpThumb->setParameter($keyname, $value); } } else { echo '<div style="color: red; border: 1px red dashed; font-weight: bold; padding: 10px; margin: 10px; display: inline-block;">Error reading ../phpThumb.config.php</div><br>'; } // this is very important when using a single object to process multiple images $phpThumb->resetObject(); // set data source $phpThumb->setSourceFilename($filePath); // set parameters (see "URL Parameters" in phpthumb.readme.txt) $phpThumb->setParameter('w', CFG_BUSINESS_LOGO_WIDTH); $phpThumb->setParameter('h', CFG_BUSINESS_LOGO_HEIGHT); $phpThumb->setParameter('zc', 'l'); // set parameters $phpThumb->setParameter('config_output_format', 'jpeg'); // generate & output thumbnail //$output_filename = 't-' . CFG_BUSINESS_LOGO_WIDTH . 'x' . CFG_BUSINESS_LOGO_HEIGHT . '-' . $fileName; $capture_raw_data = false; if ($phpThumb->GenerateThumbnail()) { $phpThumb->RenderToFile($filePath); } else { // do something with debug/error messages echo 'Failed (size=' . $thumbnail_width . ').<br>'; echo '<div style="background-color:#FFEEDD; font-weight: bold; padding: 10px;">' . $phpThumb->fatalerror . '</div>'; echo '<form><textarea rows="40" cols="180" wrap="off">' . htmlentities(implode("\n* ", $phpThumb->debugmessages)) . '</textarea></form><hr>'; die; } } return $fileName; }
public static function RenderImage($url, &$params, $nw, $nh, $nzc, $nq) { $img_cache = $params->get('img_cache', 'cache'); $cache_maxfiles_img = $params->get('cache_maxfiles_img', '200'); $cache_maxsize_img = $params->get('cache_maxsize_img', '10'); $cache_maxage_img = $params->get('cache_maxage_img', '30'); $w = $nw ? $nw : intval($params->get('imageWidth')); $h = $nh ? $nh : intval($params->get('imageHeight')); $zc = $nzc ? $nzc : intval($params->get('Zoom_Crop', 1)); $q = $nq ? $nq : '99'; $f = $params->def('img_ext', 'jpg'); $thumb_filtercolor = intval($params->get('thumb_filtercolor', 0)); $colorized = $params->get('colorized', '25'); $colorpicker = $params->get('colorpicker', '#0000ff'); $thumb_th = $params->def('thumb_th', 0); $thumb_th_seting = $params->def('thumb_th_seting', 0); $link_enabled = $params->get('link_enabled', 1); $error_image = JPATH_BASE . '/media/mod_junewsultra/' . $params->def('noimage'); $thumb_filters = $params->def('thumb_filters', 1); $usm = $params->def('thumb_unsharp', 1); $thumb_unsharp_amount = $params->def('thumb_unsharp_amount', 80); $thumb_unsharp_radius = $params->def('thumb_unsharp_radius', 1); $thumb_unsharp_threshold = $params->def('thumb_unsharp_threshold', 3); $thumb_blur = $params->def('thumb_blur', 0); $thumb_blur_seting = $params->def('thumb_blur_seting', 1); $thumb_brit = $params->def('thumb_brit', 0); $thumb_brit_seting = $params->def('thumb_brit_seting', 50); $thumb_cont = $params->def('thumb_cont', 0); $thumb_cont_seting = $params->def('thumb_cont_seting', 50); if (preg_match('#^(http|https|ftp)://#i', $url)) { $url = $url; } else { $url = !file_exists($url) ? $error_image : JPATH_BASE . '/' . $url; } $path_parts = pathinfo($url); $imgfile = explode(".", basename($url)); $img_name = $imgfile[0]; $imgurl = str_replace(array('.', ',', '-', '_', '|', '+', '=', '%', '^', '&', '(', ')', ' ', '"', '\''), '', $img_name); $subfolder = $img_cache . '/junewsultra/' . $w . 'x' . $h . '/' . substr(strtolower(MD5($path_parts['dirname'])), -1); $target = $subfolder . '/junews_' . substr(strtolower($imgurl), 0, 150) . '-' . MD5($url . $q . $zc . $f . $params->get('use_imagemagick') . $usm . $thumb_unsharp_amount . $thumb_unsharp_radius . $thumb_unsharp_threshold . $thumb_filtercolor . $colorized . $colorpicker . $thumb_blur . $thumb_blur_seting . $thumb_brit . $thumb_brit_seting . $thumb_cont . $thumb_cont_seting . $thumb_th . $thumb_th_seting) . '.' . $f; if ($link_enabled == 0 && preg_match('#^(http|https|ftp)://#i', $url)) { $outpute = $error_image; return $outpute; } modJUNewsUltraHelper::MakeDirectory($dir = JPATH_BASE . '/' . $subfolder, $mode = 0777); if (file_exists(JPATH_BASE . '/' . $target)) { $outpute = JURI::base(false) . $target; } else { include_once JPATH_BASE . '/modules/mod_junewsultra/img/phpthumb.class.php'; $phpThumb = new phpThumb(); $phpThumb->resetObject(); $phpThumb->setParameter('config_max_source_pixels', round(max(intval(ini_get('memory_limit')), intval(get_cfg_var('memory_limit'))) * 1048576 / 6)); $phpThumb->setParameter('config_temp_directory', JPATH_BASE . '/' . $img_cache . '/junewsultra/'); $phpThumb->setParameter('config_cache_directory', JPATH_BASE . '/' . $img_cache . '/junewsultra/'); $phpThumb->setCacheDirectory(); $phpThumb->setParameter('config_cache_maxfiles', $cache_maxfiles_img); $phpThumb->setParameter('config_cache_maxsize', $cache_maxsize_img * 1024 * 1024); $phpThumb->setParameter('config_cache_maxage', 86400 * $cache_maxage_img); $phpThumb->setSourceFilename($url); if ($w) { $phpThumb->setParameter('w', $w); } if ($h) { $phpThumb->setParameter('h', $h); } if ($q) { $phpThumb->setParameter('q', $q); } $phpThumb->setParameter('f', $f); $phpThumb->setParameter('zc', $zc); $phpThumb->setParameter('aoe', '1'); switch ($thumb_filtercolor) { case '1': $phpThumb->setParameter('fltr', 'gray'); break; case '2': $phpThumb->setParameter('fltr', 'sep'); break; case '3': $phpThumb->setParameter('fltr', 'th|' . $thumb_th_seting); break; case '4': $phpThumb->setParameter('fltr', 'clr|' . $colorized . '|' . str_replace('#', '', $colorpicker)); break; default: break; } if ($thumb_filters == 1) { if ($usm == 1) { $phpThumb->setParameter('fltr', 'usm|' . $thumb_unsharp_amount . '|' . $thumb_unsharp_radius . '|' . $thumb_unsharp_threshold); } if ($thumb_blur == 1) { $phpThumb->setParameter('fltr', 'blur|' . $thumb_blur_seting); } if ($thumb_brit == 1) { $phpThumb->setParameter('fltr', 'brit|' . $thumb_brit_seting); } if ($thumb_cont == 1) { $phpThumb->setParameter('fltr', 'cont|' . $thumb_cont_seting); } } if ($params->get('use_imagemagick') == 1) { if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') { $imagemagick = $params->get('imagemagick_path') == '' ? 'C:/ImageMagick/convert.exe' : $params->get('imagemagick_path'); } else { $imagemagick = $params->get('imagemagick_path', ''); } $phpThumb->setParameter('config_imagemagick_path', $imagemagick); $phpThumb->setParameter('config_prefer_imagemagick', $params->get('use_imagemagick') == 1 ? true : false); $phpThumb->setParameter('config_imagemagick_use_thumbnail', true); } if ($phpThumb->GenerateThumbnail()) { if ($phpThumb->RenderToFile(JPATH_BASE . '/' . $target)) { $outpute = JURI::base(false) . $target; } else { $outpute = ''; } } else { $outpute = ''; } } return $outpute; }