示例#1
0
 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";
     }
 }
示例#2
0
 /**
  * Crop an image.
  *
  * @param   string  $file  The name and location of the file
  * @param   string  $w     width.
  * @param   string  $h     height.
  * @param   string  $x     x-coordinate.
  * @param   string  $y     y-coordinate.
  *
  * @return  boolean     true if image cropped successfully, false otherwise.
  *
  * @since   3.2
  */
 public function cropImage($file, $w, $h, $x, $y)
 {
     if ($template = $this->getTemplate()) {
         $app = JFactory::getApplication();
         $client = JApplicationHelper::getClientInfo($template->client_id);
         $relPath = base64_decode($file);
         $path = JPath::clean($client->path . '/templates/' . $template->element . '/' . $relPath);
         $JImage = new JImage($path);
         try {
             $image = $JImage->crop($w, $h, $x, $y, true);
             $image->toFile($path);
             return true;
         } catch (Exception $e) {
             $app->enqueueMessage($e->getMessage(), 'error');
         }
     }
 }
示例#3
0
 /**
  * Crop the image and generates smaller ones.
  *
  * @param string $file
  * @param array $options
  *
  * @throws Exception
  *
  * @return array
  */
 public function cropImage($file, $options)
 {
     // Resize image
     $image = new JImage();
     $image->loadFile($file);
     if (!$image->isLoaded()) {
         throw new Exception(JText::sprintf('COM_CROWDFUNDING_ERROR_FILE_NOT_FOUND', $file));
     }
     $destinationFolder = Joomla\Utilities\ArrayHelper::getValue($options, "destination");
     // Generate temporary file name
     $generatedName = new Prism\String();
     $generatedName->generateRandomString(32);
     $imageName = $generatedName . "_image.png";
     $smallName = $generatedName . "_small.png";
     $squareName = $generatedName . "_square.png";
     $imageFile = $destinationFolder . DIRECTORY_SEPARATOR . $imageName;
     $smallFile = $destinationFolder . DIRECTORY_SEPARATOR . $smallName;
     $squareFile = $destinationFolder . DIRECTORY_SEPARATOR . $squareName;
     // Create main image
     $width = Joomla\Utilities\ArrayHelper::getValue($options, "width", 200);
     $width = $width < 25 ? 50 : $width;
     $height = Joomla\Utilities\ArrayHelper::getValue($options, "height", 200);
     $height = $height < 25 ? 50 : $height;
     $left = Joomla\Utilities\ArrayHelper::getValue($options, "x", 0);
     $top = Joomla\Utilities\ArrayHelper::getValue($options, "y", 0);
     $image->crop($width, $height, $left, $top, false);
     // Resize to general size.
     $width = Joomla\Utilities\ArrayHelper::getValue($options, "resize_width", 200);
     $width = $width < 25 ? 50 : $width;
     $height = Joomla\Utilities\ArrayHelper::getValue($options, "resize_height", 200);
     $height = $height < 25 ? 50 : $height;
     $image->resize($width, $height, false);
     // Store to file.
     $image->toFile($imageFile, IMAGETYPE_PNG);
     // Load parameters.
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     // Create small image
     $width = $params->get("image_small_width", 100);
     $height = $params->get("image_small_height", 100);
     $image->resize($width, $height, false);
     $image->toFile($smallFile, IMAGETYPE_PNG);
     // Create square image
     $width = $params->get("image_square_width", 50);
     $height = $params->get("image_square_height", 50);
     $image->resize($width, $height, false);
     $image->toFile($squareFile, IMAGETYPE_PNG);
     $names = array("image" => $imageName, "image_small" => $smallName, "image_square" => $squareName);
     // Remove the temporary file.
     if (is_file($file)) {
         JFile::delete($file);
     }
     return $names;
 }
示例#4
0
 function onUserAfterSave($data, $isNew, $result, $error)
 {
     $userId = JArrayHelper::getValue($data, 'id', 0, 'int');
     $files = JRequest::getVar('jform', null, 'files');
     $post = JRequest::getVar('jform', null);
     /*var_dump($_GET["task"]);
     		var_dump($_POST["task"]);
     		die;*/
     if ($_GET["task"] != "registration.register" && $_POST["task"] != "register") {
         $savedNewProfilePicture = false;
         // Save original picture, resized pictures and save them
         if ($files['error']['profilepicture']['file'] == 0) {
             $profilepicture = new JImage($files['tmp_name']['profilepicture']['file']);
             $sourceWidth = $profilepicture->getWidth();
             $sourceHeight = $profilepicture->getHeight();
             if ($sourceWidth < PROFILEPICTURE_SIZE_200 || $sourceHeight < PROFILEPICTURE_SIZE_200) {
                 throw new Exception(JText::_('PLG_USER_PROFILEPICTURE_ERROR_TOO_SMALL'));
             }
             $pp_filename = sha1($userId . uniqid()) . '.' . plgUserProfilePicture::FILE_EXTENSION;
             foreach ($this->sizes as $size) {
                 if ($size == PROFILEPICTURE_SIZE_ORIGINAL) {
                     $profilepicture->toFile(PROFILEPICTURE_PATH_ORIGINAL . $pp_filename);
                     $savedNewProfilePicture = true;
                 } else {
                     $ratio = max($sourceWidth, $sourceHeight) / $size;
                     $ratio = max($ratio, 1.0);
                     $resizedWidth = (int) ($sourceWidth / $ratio);
                     $resizedHeight = (int) ($sourceHeight / $ratio);
                     $left = 0;
                     $top = 0;
                     if ($this->square && $sourceWidth > $size && $sourceHeight > $size) {
                         if ($sourceWidth > $sourceHeight) {
                             $left = (int) ($sourceWidth - $sourceHeight) / 2;
                             $top = 0;
                             $croppedWidth = $sourceHeight;
                             $croppedHeight = $sourceHeight;
                             $resizedHeight = $resizedWidth;
                         } elseif ($sourceHeight > $sourceWidth) {
                             $left = 0;
                             $top = (int) (($sourceHeight - $sourceWidth) / 2);
                             $croppedWidth = $sourceWidth;
                             $croppedHeight = $sourceWidth;
                             $resizedWidth = $resizedHeight;
                         }
                         $cropped = $profilepicture->crop($croppedWidth, $croppedHeight, $left, $top, true);
                         $resized = $cropped->resize($resizedWidth, $resizedHeight, true, JImage::SCALE_OUTSIDE);
                         $resized->toFile(constant('PROFILEPICTURE_PATH_' . $size) . $pp_filename);
                         $savedNewProfilePicture = true;
                     } else {
                         $resized = $profilepicture->resize($size, $size, true, JImage::SCALE_INSIDE);
                         $resized->toFile(constant('PROFILEPICTURE_PATH_' . $size) . $pp_filename);
                         $savedNewProfilePicture = true;
                     }
                 }
             }
         }
         // Remove profile picture if an existing profile picture is
         // checked for removal or a new picture has been uploaded
         // replacing the existing picture.
         if (isset($userId) && (!empty($post['profilepicture']['file']['remove']) || $savedNewProfilePicture)) {
             $this->removeProfilePicture($userId);
         }
         if ($userId && $savedNewProfilePicture) {
             try {
                 $db = JFactory::getDbo();
                 $query = $db->getQuery(true);
                 $query = $db->getQuery(true);
                 $query->insert('#__user_profiles')->columns('user_id, profile_key, profile_value, ordering')->values($userId . ', ' . $db->quote(plgUserProfilePicture::PROFILE_KEY) . ', ' . $db->quote($pp_filename) . ', ' . ' 1');
                 $db->setQuery($query);
                 if (!$db->query()) {
                     throw new Exception($db->getErrorMsg());
                 }
             } catch (JException $e) {
                 $this->_subject->setError($e->getMessage());
                 return false;
             }
         }
         return true;
     } else {
         return true;
     }
 }
示例#5
0
 /**
  * Test the JImage::crop method without a loaded image.
  *
  * @return  void
  *
  * @expectedException  LogicException
  * @since   11.3
  */
 public function testCropWithoutLoadedImage()
 {
     // Create a new JImage object without loading an image.
     $image = new JImage();
     $image->crop(10, 10, 5, 5);
 }
示例#6
0
 function onUserAfterSave($data, $isNew, $result, $error)
 {
     $userId = JArrayHelper::getValue($data, 'id', 0, 'int');
     $files = JRequest::getVar('jform', null, 'files');
     $post = JRequest::getVar('jform', null);
     $savedNewProfileCover = false;
     // Save original cover, resized covers and save them
     if ($files['error']['profilecover']['file'] == 0 && !empty($files['tmp_name']['profilecover']['file'])) {
         // Throw new exception if the uploaded file exceed the maximum allowed file size.
         if ($this->doesExceedFileSizeLimit($files['size']['profilecover']['file'])) {
             throw new Exception(JText::sprintf('PLG_USER_PROFILECOVER_ERROR_FILE_SIZE_TOO_BIG', $this->maxUploadSizeInBytes() / 1000));
         }
         $profilecover = new JImage($files['tmp_name']['profilecover']['file']);
         $sourceWidth = $profilecover->getWidth();
         $sourceHeight = $profilecover->getHeight();
         if ($sourceWidth < PROFILECOVER_SIZE_200 || $sourceHeight < PROFILECOVER_SIZE_200) {
             throw new Exception(JText::_('PLG_USER_PROFILECOVER_ERROR_TOO_SMALL'));
         }
         $pp_filename = sha1($userId . uniqid()) . '.' . plgUserProfileCover::FILE_EXTENSION;
         foreach ($this->sizes as $size) {
             if ($size == PROFILECOVER_SIZE_ORIGINAL) {
                 $profilecover->toFile(PROFILECOVER_PATH_ORIGINAL . $pp_filename);
                 $savedNewProfileCover = true;
             } else {
                 $ratio = max($sourceWidth, $sourceHeight) / $size;
                 $ratio = max($ratio, 1.0);
                 $resizedWidth = (int) ($sourceWidth / $ratio);
                 $resizedHeight = (int) ($sourceHeight / $ratio);
                 $left = 0;
                 $top = 0;
                 if ($this->square && $sourceWidth > $size && $sourceHeight > $size) {
                     if ($sourceWidth > $sourceHeight) {
                         $left = (int) ($sourceWidth - $sourceHeight) / 2;
                         $top = 0;
                         $croppedWidth = $sourceHeight;
                         $croppedHeight = $sourceHeight;
                         $resizedHeight = $resizedWidth;
                     } elseif ($sourceHeight >= $sourceWidth) {
                         $left = 0;
                         $top = (int) (($sourceHeight - $sourceWidth) / 2);
                         $croppedWidth = $sourceWidth;
                         $croppedHeight = $sourceWidth;
                         $resizedWidth = $resizedHeight;
                     }
                     $cropped = $profilecover->crop($croppedWidth, $croppedHeight, $left, $top, true);
                     $resized = $cropped->resize($resizedWidth, $resizedHeight, true, JImage::SCALE_OUTSIDE);
                     $resized->toFile(constant('PROFILECOVER_PATH_' . $size) . $pp_filename);
                     $savedNewProfileCover = true;
                 } else {
                     $resized = $profilecover->resize($size, $size, true, JImage::SCALE_INSIDE);
                     $resized->toFile(constant('PROFILECOVER_PATH_' . $size) . $pp_filename);
                     $savedNewProfileCover = true;
                 }
             }
         }
     }
     // Remove profile cover if an existing profile cover is
     // checked for removal or a new cover has been uploaded
     // replacing the existing cover.
     if (isset($userId) && (!empty($post['profilecover']['file']['remove']) || $savedNewProfileCover)) {
         $this->removeProfileCover($userId);
     }
     if ($userId && $savedNewProfileCover) {
         try {
             $db = JFactory::getDbo();
             $query = $db->getQuery(true);
             $query = $db->getQuery(true);
             $query->insert('#__user_profiles')->columns('user_id, profile_key, profile_value, ordering')->values($userId . ', ' . $db->quote(plgUserProfileCover::PROFILE_KEY) . ', ' . $db->quote($pp_filename) . ', ' . ' 1');
             $db->setQuery($query);
             if (!$db->query()) {
                 throw new Exception($db->getErrorMsg());
             }
         } catch (JException $e) {
             $this->_subject->setError($e->getMessage());
             return false;
         }
     }
     return true;
 }
 /**
  * Crop the image and generates smaller ones.
  *
  * @param string $file
  * @param array $options
  * @param Joomla\Registry\Registry $params
  *
  * @throws Exception
  *
  * @return array
  */
 public function cropImage($file, $options, $params)
 {
     // Resize image
     $image = new JImage();
     $image->loadFile($file);
     if (!$image->isLoaded()) {
         throw new Exception(JText::sprintf('COM_SOCIALCOMMUNITY_ERROR_FILE_NOT_FOUND', $file));
     }
     $destinationFolder = Joomla\Utilities\ArrayHelper::getValue($options, 'destination');
     // Generate temporary file name
     $generatedName = Prism\Utilities\StringHelper::generateRandomString(24);
     $profileName = $generatedName . '_profile.png';
     $smallName = $generatedName . '_small.png';
     $squareName = $generatedName . '_square.png';
     $iconName = $generatedName . '_icon.png';
     $imageFile = JPath::clean($destinationFolder . DIRECTORY_SEPARATOR . $profileName);
     $smallFile = JPath::clean($destinationFolder . DIRECTORY_SEPARATOR . $smallName);
     $squareFile = JPath::clean($destinationFolder . DIRECTORY_SEPARATOR . $squareName);
     $iconFile = JPath::clean($destinationFolder . DIRECTORY_SEPARATOR . $iconName);
     // Create profile image.
     $width = Joomla\Utilities\ArrayHelper::getValue($options, 'width', 200);
     $width = $width < 25 ? 50 : $width;
     $height = Joomla\Utilities\ArrayHelper::getValue($options, 'height', 200);
     $height = $height < 25 ? 50 : $height;
     $left = Joomla\Utilities\ArrayHelper::getValue($options, 'x', 0);
     $top = Joomla\Utilities\ArrayHelper::getValue($options, 'y', 0);
     $image->crop($width, $height, $left, $top, false);
     // Resize to general size.
     $width = $params->get('image_width', 200);
     $width = $width < 25 ? 50 : $width;
     $height = $params->get('image_height', 200);
     $height = $height < 25 ? 50 : $height;
     $image->resize($width, $height, false);
     // Store to file.
     $image->toFile($imageFile, IMAGETYPE_PNG);
     // Create small image.
     $width = $params->get('image_small_width', 100);
     $height = $params->get('image_small_height', 100);
     $image->resize($width, $height, false);
     $image->toFile($smallFile, IMAGETYPE_PNG);
     // Create square image.
     $width = $params->get('image_square_width', 50);
     $height = $params->get('image_square_height', 50);
     $image->resize($width, $height, false);
     $image->toFile($squareFile, IMAGETYPE_PNG);
     // Create icon image.
     $width = $params->get('image_icon_width', 25);
     $height = $params->get('image_icon_height', 25);
     $image->resize($width, $height, false);
     $image->toFile($iconFile, IMAGETYPE_PNG);
     $names = array('image_profile' => $profileName, 'image_small' => $smallName, 'image_square' => $squareName, 'image_icon' => $iconName);
     // Remove the temporary file.
     if (JFile::exists($file)) {
         JFile::delete($file);
     }
     return $names;
 }
示例#8
0
 public static function crop($srcPath, $destPath, $cropWidth, $cropHeight, $sourceX, $sourceY, $cropMaxWidth = 64, $cropMaxHeight = 64)
 {
     $jImage = new JImage($srcPath);
     $imageInfo = JImage::getImageFileProperties($srcPath);
     try {
         if ($cropWidth == 0 && $cropHeight == 0) {
             $cropThumb = $jImage->resize($cropMaxWidth, $cropMaxHeight);
         } else {
             $cropThumb = $jImage->crop($cropWidth, $cropHeight, $sourceX, $sourceY, true);
             if ($cropMaxWidth <= $cropWidth || $cropMaxHeight <= $cropHeight) {
                 $cropThumb = $cropThumb->resize($cropMaxWidth, $cropMaxHeight);
             }
         }
         $option = $imageInfo->type == IMAGETYPE_PNG ? self::$pngOption : self::$otherOption;
         $cropThumb->toFile($destPath, $imageInfo->type, $option);
     } catch (Exception $ex) {
         return false;
     }
     return true;
 }
示例#9
0
 /**
  * Create a cropped and resized image from the uploaded original
  *
  * @return bool
  *
  * @since   1.0.0.3
  */
 protected function createImage($src, $dest, $width, $height)
 {
     $original = new JImage($src);
     $org_width = $original->getWidth();
     $org_height = $original->getHeight();
     if ($org_width / $width < $org_height / $height) {
         $original->resize($width, 0, false);
     } else {
         $original->resize(0, $height, false);
     }
     $thumb = $original->crop($width, $height, null, null, true);
     $filename = pathinfo($original->getPath(), PATHINFO_FILENAME);
     $extension = pathinfo($original->getPath(), PATHINFO_EXTENSION);
     if (!$thumb->toFile(JPATH_ROOT . $dest . $filename . "." . $extension)) {
         return false;
     }
     $original->destroy();
     $thumb->destroy();
     return true;
 }