/** * Resize an image, auto catch it from remote host and generate a new thumb in cache dir. * * @param string $url Image URL, recommend a absolute URL. * @param integer $width Image width, do not include 'px'. * @param integer $height Image height, do not include 'px'. * @param boolean $zc Crop or not. * @param integer $q Image quality * @param string $file_type File type. * * @return string The cached thumb URL. */ public static function resize($url = null, $width = 100, $height = 100, $zc = 0, $q = 85, $file_type = 'jpg') { if (!$url) { return self::getDefaultImage($width, $height, $zc, $q, $file_type); } $path = self::getImagePath($url); try { $img = new JImage(); if (JFile::exists($path)) { $img->loadFile($path); } else { return self::getDefaultImage($width, $height, $zc, $q, $file_type); } // get Width Height $imgdata = JImage::getImageFileProperties($path); // set save data if ($file_type != 'png' && $file_type != 'gif') { $file_type = 'jpg'; } $file_name = md5($url . $width . $height . $zc . $q . implode('', (array) $imgdata)) . '.' . $file_type; $file_path = self::$cache_path . DS . $file_name; $file_url = trim(self::$cache_url, '/') . '/' . $file_name; // img exists? if (JFile::exists($file_path)) { return $file_url; } // crop if ($zc) { $img = self::crop($img, $width, $height, $imgdata); } // resize $img = $img->resize($width, $height); // save switch ($file_type) { case 'gif': $type = IMAGETYPE_GIF; break; case 'png': $type = IMAGETYPE_PNG; break; default: $type = IMAGETYPE_JPEG; break; } JFolder::create(self::$cache_path); $img->toFile($file_path, $type, array('quality' => $q)); return $file_url; } catch (Exception $e) { if (JDEBUG) { echo $e->getMessage(); } return self::getDefaultImage($width, $height, $zc, $q, $file_type); } }
protected function _resize($imagePath, $opts = null) { $imagePath = urldecode($imagePath); # start configuration $cacheFolder = $this->cache_folder . '/'; # path to your cache folder, must be writeable by web server // s2s: use $this->cache_folder //$remoteFolder = $cacheFolder.'remote/'; # path to the folder you wish to download remote images into $defaults = array('crop' => false, 'scale' => false, 'thumbnail' => false, 'maxOnly' => false, 'canvas-color' => 'transparent', 'output-filename' => false, 'quality' => 100, 'cache_http_minutes' => 20); $opts = array_merge($defaults, $opts); $purl = parse_url($imagePath); $finfo = pathinfo($imagePath); $ext = $finfo['extension']; //echo '<pre>';var_dump($purl);var_dump($finfo);die; //var_dump($finfo);die; # check for remote image.. if (isset($purl['scheme']) && ($purl['scheme'] == 'http' || $purl['scheme'] == 'https')) { # grab the image, and cache it so we have something to work with.. list($filename) = explode('?', $finfo['basename']); $local_filepath = $cacheFolder . 'remote/' . $filename; $download_image = true; if (file_exists($_SERVER['DOCUMENT_ROOT'] . $local_filepath)) { if (filemtime($local_filepath) < strtotime('+' . $opts['cache_http_minutes'] . ' minutes')) { $download_image = false; } } if ($download_image == true) { $img = file_get_contents($imagePath); file_put_contents($local_filepath, $img); } $imagePath = $local_filepath; } //echo $ext.' '.$imagePath;die; $absoluteImagePath = JPath::clean(JPATH_ROOT . '/' . $imagePath); //echo $absoluteImagePath;die; /*s2s: maxOnly FIX - start*/ if ($opts['maxOnly']) { $imagesize = getimagesize($absoluteImagePath); //echo'<pre>';var_dump($imagesize);die; if (isset($opts['w'])) { if ($opts['w'] > $imagesize[0]) { $opts['w'] = $imagesize[0]; } } if (isset($opts['h'])) { if ($opts['h'] > $imagesize[1]) { $opts['h'] = $imagesize[1]; } } $opts['maxOnly'] = false; } // check if original size is less than option size if ($opts['crop']) { // fix crop: in some cases doesn't work (in exec mode) $imagesize = getimagesize($absoluteImagePath); // 0 => width, 1 => height //echo'<pre>';var_dump($imagesize);die; if ($imagesize[0] > $imagesize[1] && $imagesize[0] / $imagesize[1] < $opts['w'] / $opts['h'] || $imagesize[0] < $imagesize[1] && $imagesize[0] / $imagesize[1] > $opts['w'] / $opts['h']) { $opts['crop'] = true; $opts['resize'] = TRUE; } } /*s2s - end*/ //$path_to_convert = 'convert'; # this could be something like /usr/bin/convert or /opt/local/share/bin/convert //s2s ORIGINALE //$path_to_convert = $this->imagick_path_to_convert; // s2s imagick convert path from config ## you shouldn't need to configure anything else beyond this point list($orig_w, $orig_h) = getimagesize($absoluteImagePath); if (isset($opts['w'])) { if (stripos($opts['w'], '%') !== false) { $w = (int) ((double) str_replace('%', '', $opts['w']) / 100 * $orig_w); } else { $w = (int) $opts['w']; } } if (isset($opts['h'])) { if (stripos($opts['h'], '%') !== false) { $h = (int) ((double) str_replace('%', '', $opts['h']) / 100 * $orig_h); } else { $h = (int) $opts['h']; } } if (!isset($opts['w']) && !isset($opts['h'])) { list($w, $h) = array($orig_w, $orig_h); } //$filename = md5_file($absoluteImagePath); //$imageExt = JFile::getExt($absoluteImagePaths); $imageName = preg_replace('/\\.' . $ext . '$/', '', JFile::getName($absoluteImagePath)); $imageNameNew = $imageName . '_w' . $w . 'xh' . $h; if (!empty($w) and !empty($h)) { $imageNameNew = $imageName . '_w' . $w . '_h' . $h . (isset($opts['crop']) && $opts['crop'] == true ? "_cp" : "") . (isset($opts['scale']) && $opts['scale'] == true ? "_sc" : "") . '_q' . $opts['quality']; } elseif (!empty($w)) { $imageNameNew = $imageName . '_w' . $w . '_q' . $opts['quality']; } elseif (!empty($h)) { $imageNameNew = $imageName . '_h' . $h . '_q' . $opts['quality']; } else { return false; } //$absoluteImagePathNew = str_replace($imageName.'.'.$ext, $imageNameNew.'.'.$ext, $absoluteImagePath);//JPath::clean(JPATH_ROOT.'/'.$relativePathNew); $absoluteImagePathNew = JPath::clean(JPATH_ROOT . '/' . $cacheFolder . $imageNameNew . '.' . $ext); $relativeImagePathNew = preg_replace('/\\\\/', '/', str_replace(JPATH_ROOT, "", $absoluteImagePathNew)); if (JFile::exists($absoluteImagePathNew)) { $imageDateOrigin = filemtime($absoluteImagePath); $imageDateThumb = filemtime($absoluteImagePathNew); $clearCache = $imageDateOrigin > $imageDateThumb; if ($clearCache == false) { return $relativeImagePathNew; } } //echo $relativeImagePathNew;die; if ($this->imagick_process == 'jimage' && class_exists('JImage')) { // s2s Use Joomla JImage class (GD) if (empty($w)) { $w = 0; } if (empty($h)) { $h = 0; } //echo $imagePath;die; // Keep proportions if w or h is not defined list($width, $height) = getimagesize($absoluteImagePath); //echo $width.' '.$height;die; if (!$w) { $w = $h / $height * $width; } if (!$h) { $h = $w / $width * $height; } //echo $imagePath;die; //echo (JURI::root().$imagePath);die; // http://stackoverflow.com/questions/10842734/how-resize-image-in-joomla try { $image = new JImage(); $image->loadFile($absoluteImagePath); //echo'<pre>';var_dump($image);die; } catch (Exception $e) { return str_replace(JPATH_ROOT, "", $absoluteImagePath); // "Attempting to load an image of unsupported type: image/x-ms-bmp" } if ($opts['crop'] === true) { $rw = $w; $rh = $h; if ($width / $height < $rw / $rh) { $rw = $w; $rh = $rw / $width * $height; } else { $rh = $h; $rw = $rh / $height * $width; } $resizedImage = $image->resize($rw, $rh)->crop($w, $h); } else { $resizedImage = $image->resize($w, $h); } $properties = JImage::getImageFileProperties($absoluteImagePath); // fix compression level must be 0 through 9 (in case of png) $quality = $opts['quality']; if ($properties->type == IMAGETYPE_PNG) { $quality = round(9 - $quality * 9 / 100); // 100 quality = 0 compression, 0 quality = 9 compression } //echo '<pre>';var_dump($properties);die; $resizedImage->toFile($absoluteImagePathNew, $properties->type, array('quality' => $quality)); } //echo $relativeImagePathNew;die; # return cache file path return $relativeImagePathNew; }
public static function createThumb($path, $width = 100, $height = 100, $crop = 2) { $myImage = new JImage(); $myImage->loadFile(JPATH_SITE . DS . $path); if ($myImage->isLoaded()) { // $filename = end(explode('/', $path)); $filename = JFile::getName($path); $filefolder = substr(md5(self::getFolderPath($path)), 1, 10); $newfilename = $width . 'x' . $height . '_' . $filefolder . '_' . JFile::makeSafe($filename); $fileExists = JFile::exists(JPATH_CACHE . '/' . $newfilename); if (!$fileExists) { $resizedImage = $myImage->resize($width, $height, true, $crop); $properties = $myImage->getImageFileProperties($path); $mime = $properties->mime; if ($mime == 'image/jpeg') { $type = IMAGETYPE_JPEG; } elseif ($mime = 'image/png') { $type = IMAGETYPE_PNG; } elseif ($mime = 'image/gif') { $type = IMAGETYPE_GIF; } $resizedImage->toFile(JPATH_CACHE . '/' . $newfilename, $type); } return $newfilename; } else { return "My file is not loaded"; } }
public static function getProportion($path) { $myImage = new JImage(); $imgPath = JPATH_SITE . DS . $path; $myImage->loadFile($imgPath); if ($myImage->isLoaded()) { $properties = $myImage->getImageFileProperties($imgPath); return $properties->height / $properties->width * 100; } else { return; } }
public static function createThumb($path, $width = 100, $height = 100, $crop = 2, $cachefolder = 'hgimages', $external = 0) { $myImage = new JImage(); if (!$external) { $myImage->loadFile(JPATH_SITE . DS . $path); } else { $myImage->loadFile($path); } if ($myImage->isLoaded()) { // $filename = end(explode('/', $path)); $filename = JFile::getName($path); $filefolder = substr(md5(self::getFolderPath($path)), 1, 10); $newfilename = $width . 'x' . $height . 'x' . $crop . '_' . $filefolder . '_' . JFile::makeSafe($filename); $hgimages = JPATH_CACHE . '/' . $cachefolder . '/'; if (!JFolder::exists($hgimages)) { JFolder::create($hgimages); } $fileExists = JFile::exists($hgimages . $newfilename); if (!$fileExists) { switch ($crop) { // Case for self::CROP case 4: $resizedImage = $myImage->crop($width, $height, null, null, true); break; // Case for self::CROP_RESIZE // Case for self::CROP_RESIZE case 5: $resizedImage = $myImage->cropResize($width, $height, true); break; default: $resizedImage = $myImage->resize($width, $height, true, $crop); break; } $properties = $myImage->getImageFileProperties($path); $mime = $properties->mime; if ($mime == 'image/jpeg') { $type = IMAGETYPE_JPEG; } elseif ($mime = 'image/png') { $type = IMAGETYPE_PNG; } elseif ($mime = 'image/gif') { $type = IMAGETYPE_GIF; } $resizedImage->toFile($hgimages . $newfilename, $type); } return $newfilename; } else { return "My file is not loaded"; } }
/** * Method to load the images from the relative source * * @param JRegistry $params The module params object * * @return object[] An array of image objects * * @since 1.0 */ public static function getImages($params) { // Create the folder path $folder = JPath::clean(JPATH_BASE . DIRECTORY_SEPARATOR . $params->get('image_folder')); $cacheFolder = JPath::clean(JPATH_BASE . '/cache/mod_qluegallery/thumbs/' . $params->get('image_folder')); // Make sure the folder we are trying to load actually exists if (!JFolder::exists($folder)) { JError::raiseWarning(500, JText::_('MOD_QLUEGALLERY_NO_FOLDER_EXISTS')); return null; } // Load all images from the folder $images = JFolder::files($folder, '\\.(?:gif|jpg|png|jpeg)$'); // Limit our found images $images = array_slice($images, 0, (int) $params->get('limit', 1)); // Loop through each image and apply the image path foreach ($images as $key => $image) { // Path to the file $file = JPath::clean($folder . '/' . $image); $dimensions = $params->get('thumbnail_width', 150) . 'x' . $params->get('thumbnail_height', 150); $thumbnail = pathinfo($image, PATHINFO_FILENAME); $thumbExt = pathinfo($image, PATHINFO_EXTENSION); $thumbnail .= '_' . $dimensions . '.' . $thumbExt; // Create our image object $img = new stdClass(); $img->file = $image; $img->full_path = JUri::root(true) . str_replace(JPATH_BASE, '', $file); $img->properties = JImage::getImageFileProperties($file); $img->thumbnail = str_replace(JPATH_BASE, '', $cacheFolder . '/' . $thumbnail); // If the thumbnail does not exist, create it if (!file_exists($cacheFolder . DIRECTORY_SEPARATOR . $thumbnail)) { // Get the image source $gd = new JImage($file); // Create the thumb folder if it does not exist if (!JFolder::exists($cacheFolder)) { JFolder::create($cacheFolder); } // Create the thumbnails $gd->createThumbs($dimensions, JImage::CROP_RESIZE, $cacheFolder); } // Make sure the file paths are safe to use $img->full_path = str_replace('\\', '/', $img->full_path); $img->thumbnail = str_replace('\\', '/', $img->thumbnail); $images[$key] = $img; } return $images; }
/** * Test the JImage::getImageFileProperties method without a valid image file. * * @return void * * @expectedException InvalidArgumentException * @since 11.3 */ public function testGetImageFilePropertiesWithInvalidFile() { JImage::getImageFileProperties(JPATH_TESTS . '/suite/joomla/image/stubs/bogus.image'); }
/** * Plugin that manipulate uploaded images * * @param string $context The context of the content being passed to the plugin. * @param object &$object_file The file object. * * @return object The file object. */ public function onContentAfterSave($context, &$object_file) { // Are we in the right context? if ($context != 'com_media.file') { return; } $file = pathinfo($object_file->filepath); // Skip if the pass through keyword is set if (preg_match('/' . $this->params->get('passthrough') . '_/', $file['filename'])) { return; } $image = new JImage(); // Load the file $image->loadFile($object_file->filepath); // Get the properties $properties = $image->getImageFileProperties($object_file->filepath); // Skip if the width is less or equal to the required if ($properties->width <= $this->params->get('maxwidth')) { return; } // Get the image type if (preg_match('/jp(e)g/', mb_strtolower($properties->mime))) { $imageType = 'IMAGETYPE_JPEG'; } if (preg_match('/gif/', mb_strtolower($properties->mime))) { $imageType = 'IMAGETYPE_GIF'; } if (preg_match('/png/', mb_strtolower($properties->mime))) { $imageType = 'IMAGETYPE_PNG'; } // Resize the image $image->resize($this->params->get('maxwidth'), '', false); // Overwrite the file $image->toFile($object_file->filepath, $imageType, array('quality' => $this->params->get('quality'))); return $object_file; }
/** * Validate image size. * * <code> * $myFile = "/tmp/myfile.jpg"; * * $validator = new PrismFileValidatorImageSize($myFile); * * if (!$validator->isValid()) { * echo $validator->getMessage(); * } * </code> * * @return bool */ public function isValid() { if (!\JFile::exists($this->file)) { $this->message = \JText::sprintf('LIB_PRISM_ERROR_FILE_DOES_NOT_EXISTS', $this->file); return false; } $imageProperties = \JImage::getImageFileProperties($this->file); // Check the minimum width of the image. if ($this->minWidth > 0 and $imageProperties->width < $this->minWidth) { $this->message = \JText::sprintf('LIB_PRISM_ERROR_FILE_IMAGE_MIN_WIDTH', $this->minWidth); return false; } // Check the minimum height of the image. if ($this->minHeight > 0 and $imageProperties->height < $this->minHeight) { $this->message = \JText::sprintf('LIB_PRISM_ERROR_FILE_IMAGE_MIN_HEIGHT', $this->minHeight); return false; } // Check the maximum width of the image. if ($this->maxWidth > 0 and $imageProperties->width > $this->maxWidth) { $this->message = \JText::sprintf('LIB_PRISM_ERROR_FILE_IMAGE_MAX_WIDTH', $this->maxWidth); return false; } // Check the maximum height of the image. if ($this->maxHeight > 0 and $imageProperties->height > $this->maxHeight) { $this->message = \JText::sprintf('LIB_PRISM_ERROR_FILE_IMAGE_MAX_HEIGHT', $this->maxHeight); return false; } return true; }
$imageinfo = getimagesize($image['tmp_name']); $mime_types = $sconfig->mime_types; $okMIMETypes = $mime_types; $validFileTypes = explode(",", $okMIMETypes); if (is_int($imageinfo[0]) || is_int($imageinfo[1]) || in_array($imageinfo['mime'], $validFileTypes)) { $image['name'] = preg_replace("/[^A-Za-z.0-9]/i", "-", $image['name']); $newName = 'profile-' . $uid . '-' . $time . '-' . $image['name']; $newName2 = 'profile-x-' . $uid . '-' . $time . '-' . $image['name']; $uploadPath = $base_path . $uid . DS . $newName; $uploadPath2 = $base_path . $uid . DS . $newName2; $stampPath = JPATH_COMPONENT . DS . 'assets' . DS . 'images' . DS . 'stamp.png'; $file_name = $newName2; JFile::upload($image['tmp_name'], $uploadPath); #################### $image = new JImage($uploadPath); $properties = JImage::getImageFileProperties($uploadPath); $resizedImage = $image->resize('250', '250', true); $mime = $properties->mime; if ($mime == 'image/jpeg') { $type = IMAGETYPE_JPEG; } elseif ($mime = 'image/png') { $type = IMAGETYPE_PNG; } elseif ($mime = 'image/gif') { $type = IMAGETYPE_GIF; } $resizedImage->toFile($uploadPath, $type); //create image ..... //echo 'creem imaginea<br />'; watermark_image($uploadPath, $uploadPath2, $mime); #################### ######adaugare in baza de date
/** resize images */ public static function resizeImage($originalPath, $thumbPath, $width, $height) { $imageObj = new JImage($originalPath); $properties = JImage::getImageFileProperties($originalPath); $resizedImage = $imageObj->resize($width, $height, true); $mime = $properties->mime; if ($mime == 'image/jpeg') { $type = IMAGETYPE_JPEG; } elseif ($mime = 'image/png') { $type = IMAGETYPE_PNG; } elseif ($mime = 'image/gif') { $type = IMAGETYPE_GIF; } $resizedImage->toFile($thumbPath, $type); }
protected function prepareImageProperties($file) { $imageProperties = \JImage::getImageFileProperties($file); $properties = array('width' => $imageProperties->width, 'height' => $imageProperties->height, 'mime' => $imageProperties->mime, 'filesize' => $imageProperties->filesize); return $properties; }
public static function getFileProperties($source) { return JImage::getImageFileProperties($source); }
public function save($data) { $app = JFactory::getApplication(); $input = $app->input; $_data = array('id' => $data->id, 'asset_id' => $data->asset_id, 'media' => '{}'); $params = $this->getState('params'); // Get some params $mime_types = $params->get('image_mime_type', 'image/jpeg,image/gif,image/png,image/bmp'); $mime_types = explode(',', $mime_types); $file_types = $params->get('image_file_type', 'bmp,gif,jpg,jpeg,png'); $file_types = explode(',', $file_types); $file_sizes = $params->get('image_file_size', 10); $file_sizes = $file_sizes * 1024 * 1024; // Get and Process data $image_data = $input->get('jform', null, 'array'); if (isset($image_data['media'])) { if (isset($image_data['media'][$this->getName()])) { $image_data = $image_data['media']['image']; } } $media = null; if ($data->media && !empty($data->media)) { $media = new JRegistry(); $media->loadString($data->media); $media = $media->get('image'); } // Set data when save as copy article if ($input->getCmd('task') == 'save2copy' && $input->getInt('id')) { if (isset($image_data['url_remove']) && $image_data['url_remove']) { $image_data['url_remove'] = null; $image_data['url'] = ''; } if (isset($image_data['url_hover_remove']) && $image_data['url_hover_remove']) { $image_data['url_hover_remove'] = ''; $image_data['url_hover'] = ''; } if (!isset($image_data['url_server']) || isset($image_data['url_server']) && empty($image_data['url_server'])) { if (isset($image_data['url']) && $image_data['url']) { $ext = JFile::getExt($image_data['url']); $path_copy = str_replace('.' . $ext, '_o.' . $ext, $image_data['url']); if (JFile::exists(JPATH_ROOT . DIRECTORY_SEPARATOR . $path_copy)) { $image_data['url_server'] = $path_copy; $image_data['url'] = ''; } } } if (!isset($image_data['url_hover_server']) || isset($image_data['url_hover_server']) && empty($image_data['url_hover_server'])) { if (isset($image_data['url_hover']) && $image_data['url_hover']) { $ext = JFile::getExt($image_data['url_hover']); $path_copy = str_replace('.' . $ext, '_o.' . $ext, $image_data['url_hover']); if (JFile::exists(JPATH_ROOT . DIRECTORY_SEPARATOR . $path_copy)) { $image_data['url_hover_server'] = $path_copy; $image_data['url_hover'] = ''; } } } } // Remove image and image hover with resized if ($image_size = $params->get('image_size', array())) { $image_size = $this->prepareImageSize($image_size); if (is_array($image_size) && count($image_size)) { foreach ($image_size as $_size) { $size = json_decode($_size); // Delete old image files if (isset($image_data['url_remove']) && $image_data['url_remove'] && $media && isset($media->url) && !empty($media->url)) { $image_url = $media->url; $image_url = str_replace('.' . JFile::getExt($image_url), '_' . $size->image_name_prefix . '.' . JFile::getExt($image_url), $image_url); JFile::delete(JPATH_ROOT . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $image_url)); } // Delete old image hover files if (isset($image_data['url_hover_remove']) && $image_data['url_hover_remove'] && $media && isset($media->url_hover) && !empty($media->url_hover)) { $image_url = $media->url_hover; $image_url = str_replace('.' . JFile::getExt($image_url), '_' . $size->image_name_prefix . '.' . JFile::getExt($image_url), $image_url); JFile::delete(JPATH_ROOT . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $image_url)); } } } } // Remove Image file when tick to remove file box if (isset($image_data['url_remove']) && $image_data['url_remove']) { // Before upload image to file must delete original file if ($media && isset($media->url) && !empty($media->url)) { $image_url = $media->url; $image_url = str_replace('.' . JFile::getExt($image_url), '_o' . '.' . JFile::getExt($image_url), $image_url); if (JFile::delete(JPATH_ROOT . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $image_url))) { $image_data['url'] = ''; unset($image_data['url_remove']); } } } else { unset($image_data['url']); } // Remove Image hover file when tick to remove file box if (isset($image_data['url_hover_remove']) && $image_data['url_hover_remove']) { // Before upload image to file must delete original file if ($media && isset($media->url_hover) && !empty($media->url_hover)) { $image_url = $media->url_hover; $image_url = str_replace('.' . JFile::getExt($image_url), '_o' . '.' . JFile::getExt($image_url), $image_url); if (JFile::delete(JPATH_ROOT . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $image_url))) { $image_data['url_hover'] = ''; unset($image_data['url_hover_remove']); } } } else { unset($image_data['url_hover']); } $images = array(); $images_hover = array(); $imageObj = new JImage(); // Upload image or image hover if ($files = $input->files->get('jform', array(), 'array')) { if (isset($files['media']) && isset($files['media']['image'])) { $files = $files['media']['image']; // Get image from form if (isset($files['url_client']['name']) && !empty($files['url_client']['name'])) { $images = $files['url_client']; } // Get image hover data from form if (isset($files['url_hover_client']['name']) && !empty($files['url_hover_client']['name'])) { $images_hover = $files['url_hover_client']; } } } $path = ''; $path_hover = ''; jimport('joomla.filesystem.file'); $imageType = null; $imageMimeType = null; $imageSize = null; $image_hoverType = null; $image_hoverMimeType = null; $image_hoverSize = null; // Create original image with new name (upload from client) if (count($images) && !empty($images['tmp_name'])) { // Get image file type $imageType = JFile::getExt($images['name']); $imageType = strtolower($imageType); // Get image's mime type $imageMimeType = $images['type']; // Get image's size $imageSize = $images['size']; $path = COM_TZ_PORTFOLIO_PLUS_MEDIA_ARTICLE_ROOT . DIRECTORY_SEPARATOR; $path .= $data->alias . '-' . $data->id . '_o'; $path .= '.' . JFile::getExt($images['name']); if ($input->getCmd('task') == 'save2copy' && $input->getInt('id')) { $image_data['url_server'] = null; } } elseif (isset($image_data['url_server']) && !empty($image_data['url_server'])) { // Create original image with new name (upload from server) // Get image file type $imageType = JFile::getExt($image_data['url_server']); $imageType = strtolower($imageType); // Get image's mime type $imageObj->loadFile(JPATH_ROOT . DIRECTORY_SEPARATOR . $image_data['url_server']); $imageMimeType = $imageObj->getImageFileProperties($imageObj->getPath()); $imageMimeType = $imageMimeType->mime; // Get image's size $imageSize = $imageMimeType->filesize; $path = COM_TZ_PORTFOLIO_PLUS_MEDIA_ARTICLE_ROOT . DIRECTORY_SEPARATOR; $path .= $data->alias . '-' . $data->id . '_o'; $path .= '.' . JFile::getExt($image_data['url_server']); } // Create original image hover with new name (upload from client) if (count($images_hover) && !empty($images_hover['tmp_name'])) { // Get image hover file type $image_hoverType = JFile::getExt($images_hover['name']); $image_hoverType = strtolower($image_hoverType); // Get image hover's mime type $image_hoverMimeType = $images_hover['type']; // Get image's size $image_hoverSize = $images_hover['size']; $path_hover = COM_TZ_PORTFOLIO_PLUS_MEDIA_ARTICLE_ROOT . DIRECTORY_SEPARATOR; $path_hover .= $data->alias . '-' . $data->id . '-h_o'; $path_hover .= '.' . JFile::getExt($images_hover['name']); if ($input->getCmd('task') == 'save2copy' && $input->getInt('id')) { $image_data['url_hover_server'] = null; } } elseif (isset($image_data['url_hover_server']) && !empty($image_data['url_hover_server'])) { // Create original image with new name (upload from server) // Get image hover file type $image_hoverType = JFile::getExt($image_data['url_hover_server']); $image_hoverType = strtolower($image_hoverType); // Get image hover's mime type $imageObj->loadFile(JPATH_ROOT . DIRECTORY_SEPARATOR . $image_data['url_hover_server']); $image_hoverMimeType = $imageObj->getImageFileProperties($imageObj->getPath()); $image_hoverMimeType = $image_hoverMimeType->mime; // Get image hover's size $image_hoverSize = $image_hoverMimeType->filesize; $path_hover = COM_TZ_PORTFOLIO_PLUS_MEDIA_ARTICLE_ROOT . DIRECTORY_SEPARATOR; $path_hover .= $data->alias . '-' . $data->id . '-h_o'; $path_hover .= '.' . JFile::getExt($image_data['url_hover_server']); } // Upload original image if ($path && !empty($path)) { //-- Check image information --// // Check MIME Type if (!in_array($imageMimeType, $mime_types)) { $app->enqueueMessage(JText::_('PLG_MEDIATYPE_IMAGE_ERROR_WARNINVALID_MIME'), 'notice'); return false; } // Check file type if (!in_array($imageType, $file_types)) { $app->enqueueMessage(JText::_('PLG_MEDIATYPE_IMAGE_ERROR_WARNFILETYPE'), 'notice'); return false; } // Check file size if ($imageSize > $file_sizes) { $app->enqueueMessage(JText::_('PLG_MEDIATYPE_IMAGE_ERROR_WARNFILETOOLARGE'), 'notice'); return false; } //-- End check image information --// // Before upload image to file must delete original file if ($media && isset($media->url) && !empty($media->url)) { $image_url = $media->url; $image_url = str_replace('.' . JFile::getExt($image_url), '_o' . '.' . JFile::getExt($image_url), $image_url); JFile::delete(JPATH_ROOT . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $image_url)); } if (isset($images['tmp_name']) && !empty($images['tmp_name']) && !JFile::upload($images['tmp_name'], $path)) { $path = ''; } elseif (isset($image_data['url_server']) && !empty($image_data['url_server']) && !JFile::copy(JPATH_ROOT . DIRECTORY_SEPARATOR . $image_data['url_server'], $path)) { $path = ''; } } // Upload original image hover if ($path_hover && !empty($path_hover)) { //-- Check image information --// // Check MIME Type if (!in_array($image_hoverMimeType, $mime_types)) { $app->enqueueMessage(JText::_('PLG_MEDIATYPE_IMAGE_ERROR_WARNINVALID_MIME'), 'notice'); return false; } // Check file type if (!in_array($image_hoverType, $file_types)) { $app->enqueueMessage(JText::_('PLG_MEDIATYPE_IMAGE_ERROR_WARNFILETYPE'), 'notice'); return false; } // Check file size if ($image_hoverSize > $file_sizes) { $app->enqueueMessage(JText::_('PLG_MEDIATYPE_IMAGE_ERROR_WARNFILETOOLARGE'), 'notice'); return false; } //-- End check image information --// // Before upload image hover file to file must delete original file if ($media && isset($media->url_hover) && !empty($media->url_hover)) { $image_url = $media->url_hover; $image_url = str_replace('.' . JFile::getExt($image_url), '_o' . '.' . JFile::getExt($image_url), $image_url); JFile::delete(JPATH_ROOT . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $image_url)); } if (isset($images_hover['tmp_name']) && !empty($images_hover['tmp_name']) && !JFile::upload($images_hover['tmp_name'], $path_hover)) { $path_hover = ''; } elseif (isset($image_data['url_hover_server']) && !empty($image_data['url_hover_server']) && !JFile::copy(JPATH_ROOT . DIRECTORY_SEPARATOR . $image_data['url_hover_server'], $path_hover)) { $path_hover = ''; } } // Upload image and image hover with resize if ($image_size = $params->get('image_size')) { $image_size = $this->prepareImageSize($image_size); $image = null; $image_hover = null; if (is_array($image_size) && count($image_size)) { foreach ($image_size as $_size) { $size = json_decode($_size); // Upload image with resize if ($path) { // Create new ratio from new with of image size param $imageObj->loadFile($path); $imgProperties = $imageObj->getImageFileProperties($imageObj->getPath()); $newH = $imgProperties->height * $size->width / $imgProperties->width; $newImage = $imageObj->resize($size->width, $newH); $newPath = COM_TZ_PORTFOLIO_PLUS_MEDIA_ARTICLE_ROOT . DIRECTORY_SEPARATOR . $data->alias . '-' . $data->id . '_' . $size->image_name_prefix . '.' . JFile::getExt($path); // Before generate image to file must delete old files if ($media && isset($media->url) && !empty($media->url)) { $image_url = $media->url; $image_url = str_replace('.' . JFile::getExt($image_url), '_' . $size->image_name_prefix . '.' . JFile::getExt($image_url), $image_url); JFile::delete(JPATH_ROOT . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $image_url)); } // Generate image to file $newImage->toFile($newPath, $imgProperties->type); } // Upload image hover with resize if ($path_hover) { // Create new ratio from new with of image size param $imageObj->loadFile($path_hover); $imgHoverProperties = $imageObj->getImageFileProperties($imageObj->getPath()); $newH = $imgHoverProperties->height * $size->width / $imgHoverProperties->width; $newHImage = $imageObj->resize($size->width, $newH); $newHPath = COM_TZ_PORTFOLIO_PLUS_MEDIA_ARTICLE_ROOT . DIRECTORY_SEPARATOR . $data->alias . '-' . $data->id . '-h_' . $size->image_name_prefix . '.' . JFile::getExt($path_hover); // Before generate image hover to file must delete old files if ($media && isset($media->url_hover) && !empty($media->url_hover)) { $image_url_hover = $media->url_hover; $image_url_hover = str_replace('.' . JFile::getExt($image_url_hover), '_' . $size->image_name_prefix . '.' . JFile::getExt($image_url_hover), $image_url_hover); JFile::delete(JPATH_ROOT . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $image_url_hover)); } // Generate image to file $newHImage->toFile($newHPath, $imgHoverProperties->type); } } } } if ($path && !empty($path)) { $image_data['url'] = COM_TZ_PORTFOLIO_PLUS_MEDIA_ARTICLE_BASE . '/' . $data->alias . '-' . $data->id . '.' . JFile::getExt($path); } if ($path_hover && !empty($path_hover)) { $image_data['url_hover'] = COM_TZ_PORTFOLIO_PLUS_MEDIA_ARTICLE_BASE . '/' . $data->alias . '-' . $data->id . '-h.' . JFile::getExt($path_hover); } unset($image_data['url_server']); unset($image_data['url_hover_server']); $this->__save($data, $image_data); // } }
/** * Method to get the list of input[type="file"] * * @return string The field input markup. * * @since 11.1 */ protected function getInput() { $output = ''; $params = JComponentHelper::getParams('com_jea'); $imgUploadNumber = $params->get('img_upload_number', 3); for ($i = 0; $i < $imgUploadNumber; $i++) { $output .= '<input type="file" name="newimages[]" value="" size="30" class="fltnone" /> <br />'; } $output .= "\n"; //alert & return if GD library for PHP is not enabled if (!extension_loaded('gd')) { $output .= '<strong>WARNING: </strong>The <a href="http://php.net/manual/en/book.image.php" target="_blank">GD library for PHP</a> was not found. Ensure to install it'; return $output; } if (is_string($this->value)) { $images = (array) json_decode($this->value); } else { $images = (array) $this->value; foreach ($images as $k => $image) { $images[$k] = (object) $image; } } $propertyId = $this->form->getValue('id'); $baseURL = JURI::root(true); $imgBaseURL = $baseURL . '/images/com_jea/images/' . $propertyId; $imgBasePath = JPATH_ROOT . DS . 'images' . DS . 'com_jea' . DS . 'images' . DS . $propertyId; if (!empty($images)) { $output .= "<ul class=\"gallery\">\n"; foreach ($images as $k => $image) { $imgPath = $imgBasePath . DS . $image->name; try { $infos = JImage::getImageFileProperties($imgPath); } catch (Exception $e) { $output .= "<li>Recorded Image " . $image->name . " cannot be accessed</li>\n"; continue; } $thumbName = 'thumb-admin-' . $image->name; // Create the thumbnail if (!file_exists($imgBasePath . DS . $thumbName)) { try { // This is where the JImage will be used, so only create it here $JImage = new JImage($imgPath); $thumb = $JImage->resize(150, 90); $thumb->crop(150, 90, 0, 0); $thumb->toFile($imgBasePath . DS . $thumbName); // To avoid memory overconsumption, destroy the JImage now that we don't need it anymore if (method_exists($JImage, 'destroy')) { $JImage->destroy(); $thumb->destroy(); } else { // There is no destroy method on Jplatform < 12.3 (Joomla 2.5) and the handle property is protected. // We have to hack the JImage class to destroy the image resource $prop = new ReflectionProperty('JImage', 'handle'); $prop->setAccessible(true); $JImageHandle = $prop->getValue($JImage); $thumbHandle = $prop->getValue($thumb); if (is_resource($JImageHandle)) { imagedestroy($JImageHandle); } if (is_resource($thumbHandle)) { imagedestroy($thumbHandle); } } } catch (Exception $e) { $output .= "<li>Thumbnail for " . $image->name . " cannot be generated</li>\n"; continue; } } $thumbUrl = $imgBaseURL . '/' . $thumbName; $url = $imgBaseURL . '/' . $image->name; $weight = round($infos->bits / 1024, 1); // Ko $output .= "<li class=\"item-{$k}\">\n" . "<a href=\"{$url}\" title=\"Zoom\" class=\"imgLink modal\" rel=\"{handler: 'image'}\"><img src=\"{$thumbUrl}\" alt=\"{$image->name}\" /></a>\n" . "<div class=\"imgInfos\">\n" . $image->name . "<br />\n" . JText::_('COM_JEA_WIDTH') . ' : ' . $infos->width . ' px' . "<br />\n" . JText::_('COM_JEA_HEIGHT') . ' : ' . $infos->height . ' px' . "<br />\n" . "</div>\n" . "<div class=\"imgTools\">\n" . ' <a class="img-move-up" title="' . JText::_('JLIB_HTML_MOVE_UP') . '"><img src="' . $baseURL . '/media/com_jea/images/sort_asc.png' . '" alt="Move up" /></a>' . ' <a class="img-move-down" title="' . JText::_('JLIB_HTML_MOVE_DOWN') . '"><img src="' . $baseURL . '/media/com_jea/images/sort_desc.png' . '" alt="Move down" /></a>' . ' <a class="delete-img" title="' . JText::_('JACTION_DELETE') . '"><img src="' . $baseURL . '/media/com_jea/images/media_trash.png' . '" alt="Delete" /></a>' . "</div>\n" . "<div class=\"clr\"></div>\n" . '<label for="' . $this->id . $k . 'title">' . JText::_('JGLOBAL_TITLE') . '</label><input id="' . $this->id . $k . 'title" type="text" name="' . $this->name . '[' . $k . '][title]" value="' . $image->title . '" size="20"/><br />' . '<label for="' . $this->id . $k . 'desc">' . JText::_('JGLOBAL_DESCRIPTION') . '</label><input id="' . $this->id . $k . 'desc" type="text" name="' . $this->name . '[' . $k . '][description]" value="' . $image->description . '" size="40"/>' . '<input type="hidden" name="' . $this->name . '[' . $k . '][name]" value="' . $image->name . '" />' . "<div class=\"clr\"></div>\n" . "</li>\n"; } $output .= "</ul>\n"; // Add javascript behavior JHtml::_('behavior.modal'); JFactory::getDocument()->addScriptDeclaration("\n window.addEvent('domready', function() {\n var sortOptions = {\n transition: Fx.Transitions.Back.easeInOut,\n duration: 700,\n mode: 'vertical',\n onComplete: function() {\n mySort.rearrangeDOM()\n }\n };\n\n var mySort = new Fx.Sort(\$\$('ul.gallery li'), sortOptions);\n\n \$\$('a.delete-img').each(function(item) {\n item.addEvent('click', function() {\n this.getParent('li').destroy();\n mySort = new Fx.Sort(\$\$('ul.gallery li'), sortOptions);\n });\n });\n\n \$\$('a.img-move-up').each(function(item) {\n item.addEvent('click', function() {\n var activeLi = this.getParent('li');\n if (activeLi.getPrevious()) {\n mySort.swap(activeLi, activeLi.getPrevious());\n } else if (this.getParent('ul').getChildren().length > 1 ) {\n // Swap with the last element\n \tmySort.swap(activeLi, this.getParent('ul').getLast('li'));\n }\n });\n });\n\n \$\$('a.img-move-down').each(function(item) {\n item.addEvent('click', function() {\n var activeLi = this.getParent('li');\n if (activeLi.getNext()) {\n mySort.swap(activeLi, activeLi.getNext());\n } else if (this.getParent('ul').getChildren().length > 1 ) {\n // Swap with the first element\n \tmySort.swap(activeLi, this.getParent('ul').getFirst('li'));\n }\n });\n });\n\n })"); } return $output; }
/** * Validate image type and extension. * * <code> * $myFile = "/tmp/myfile.jpg"; * $fileName = "myfile.jpg"; * * $validator = new Prism\File\Validator\Image($myFile, $fileName); * * if (!$validator->isValid()) { * echo $validator->getMessage(); * } * </code> * * @return bool */ public function isValid() { if (!\JFile::exists($this->file)) { $this->message = \JText::sprintf('LIB_PRISM_ERROR_FILE_DOES_NOT_EXISTS', $this->file); return false; } $imageProperties = \JImage::getImageFileProperties($this->file); // Check mime type of the file if (false === array_search($imageProperties->mime, $this->mimeTypes)) { $this->message = \JText::_('LIB_PRISM_ERROR_FILE_TYPE'); return false; } // Check file extension $ext = \JString::strtolower(\JFile::getExt($this->fileName)); if (false === array_search($ext, $this->imageExtensions)) { $this->message = \JText::sprintf('LIB_PRISM_ERROR_FILE_EXTENSIONS', $ext); return false; } return true; }
public function validateInput() { jimport('joomla.filesystem.file'); jimport('joomla.image.image'); $app = JFactory::getApplication(); $fm = JRequest::getVar('jform', NULL, 'post'); if (!empty($fm)) { $site_name = $fm['site_name']; $site_metadesc = $fm['site_metadesc']; $site_metakeys = $fm['site_metakeys']; $site_offline = $fm['site_offline']; $admin_email = $fm['admin_email']; $admin_user = $fm['admin_user']; $admin_password = $fm['admin_password']; $admin_password2 = $fm['admin_password2']; } else { $site_name = NULL; $site_metadesc = NULL; $site_metakeys = NULL; $site_offline = NULL; $admin_email = NULL; $admin_user = NULL; $admin_password = NULL; $admin_password2 = NULL; } $files = JRequest::getVar('jform', NULL, 'files'); $msg = "Usted debe corregir y completar los siguientes datos antes de continuar:<br />"; $disc = true; if (empty($site_name)) { $msg .= "- Debe ingresar un nombre para el sitio.<br />"; $disc &= false; } if (empty($admin_email)) { $msg .= "- Debe ingresar el correo del administrador.<br />"; $disc &= false; } if (empty($admin_user)) { $msg .= "- Debe ingresar un nombre de usuario.<br />"; $disc &= false; } if (empty($admin_password)) { $msg .= "- Debe ingresar una contraseña.<br />"; $disc &= false; } if (empty($admin_password2)) { $msg .= "- Debe confirmar la contraseña.<br />"; $disc &= false; } if ($admin_password != $admin_password2) { $msg .= "- Las contraseñas no coinciden.<br />"; $disc &= false; } if ($disc) { if (!empty($files['tmp_name']['site_logo'])) { // Set the path to the file $file = $files['tmp_name']['site_logo']; // Instantiate our JImage object $image = new JImage($file); // Get the file's properties $properties = $image->getImageFileProperties($file); // Resize the file as a new object $logo1 = $image->resize('200px', '74px', true); $logo3 = $image->resize('250px', '30px', true); // Determine the MIME of the original file to get the proper type for output $mime = $properties->mime; if ($mime == 'image/jpeg') { $type = IMAGETYPE_JPEG; } elseif ($mime == 'image/png') { $type = IMAGETYPE_PNG; } elseif ($mime == 'image/gif') { $type = IMAGETYPE_GIF; } // Store the resized image to a new file $logo1->toFile(JPATH_ROOT . DS . 'images' . DS . 'logos' . DS . 'jokte-logo-front.png', $type); $logo3->toFile(JPATH_ROOT . DS . 'administrator' . DS . 'templates' . DS . 'storkantu' . DS . 'images' . DS . 'logo.png', $type); } $site_name = $fm['site_name']; $site_metadesc = $fm['site_metadesc']; $site_metakeys = $fm['site_metakeys']; $site_offline = $fm['site_offline']; $admin_email = $fm['admin_email']; $admin_user = $fm['admin_user']; $admin_password = $fm['admin_password']; $admin_password2 = $fm['admin_password2']; $fmData = '&jform[site_name]=' . $site_name; $fmData .= '&jform[site_metadesc]=' . $site_metadesc; $fmData .= '&jform[site_metakeys]=' . $site_metakeys; $fmData .= '&jform[site_offline]=' . $site_offline; $fmData .= '&jform[admin_email]=' . $admin_email; $fmData .= '&jform[admin_user]=' . $admin_user; $fmData .= '&jform[admin_password]=' . $admin_password; $fmData .= '&jform[admin_password2]=' . $admin_password2; JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN')); $app->redirect('?view=remove&task=setup.saveconfig' . $fmData . "&{$tkn}=1"); } elseif (!empty($fm)) { JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN')); $app->redirect('?view=site', $msg, 'warning'); } }