/** * createThumb * * @param string $src * * @return bool */ public static function createThumb($src, $width = 400, $height = 400) { $dest = new \SplFileInfo(WINDWALKER_CACHE . '/image/temp/' . md5($src)); Folder::create($dest->getPath()); $image = new Image(); $image->loadFile($src); $image->cropResize($width, $height, false); $image->toFile($dest->getPathname() . '.jpg'); return $dest->getPathname() . '.jpg'; }
/** * Save user avatars * @return [type] [description] */ public function saveAvatar() { //this is the name of the field in the html form, filedata is the default name for swfupload //so we will leave it as that $fieldName = 'avatar'; //any errors the server registered on uploading $fileError = $_FILES[$fieldName]['error']; if ($fileError > 0) { switch ($fileError) { case 1: echo TextHelper::_('FILE TO LARGE THAN PHP INI ALLOWS'); return false; case 2: echo TextHelper::_('FILE TO LARGE THAN HTML FORM ALLOWS'); return false; case 3: echo TextHelper::_('ERROR PARTIAL UPLOAD'); return false; case 4: echo TextHelper::_('ERROR NO FILE'); return false; } } //check the file extension is ok $fileName = $_FILES[$fieldName]['name']; $fileTemp = $_FILES[$fieldName]['tmp_name']; $uploadedFileNameParts = explode('.', $fileName); $uploadedFileExtension = array_pop($uploadedFileNameParts); $validFileExts = explode(',', 'jpeg,jpg,png,gif,bmp'); //assume the extension is false until we know its ok $extOk = false; //go through every ok extension, if the ok extension matches the file extension (case insensitive) //then the file extension is ok foreach ($validFileExts as $key => $value) { if (preg_match("/{$value}/i", $uploadedFileExtension)) { $extOk = true; } } if ($extOk == false) { echo TextHelper::_('INVALID EXTENSION'); return false; } //data generation $date = DateHelper::formatDBDate(date('Y-m-d H:i:s')); $hashFilename = md5($fileName . $date) . "." . $uploadedFileExtension; //lose any special characters in the filename //$fileName = preg_replace("[^A-Za-z0-9.]", "-", $fileName); //always use constants when making file paths, to avoid the possibilty of remote file inclusion $uploadPath = JPATH_SITE . '/src/Cobalt/media/avatars/' . $hashFilename; if (!File::upload($fileTemp, $uploadPath)) { echo TextHelper::_('ERROR MOVING FILE'); return false; } $image = new Image(); $image->loadFile($uploadPath); $image->resize(50, 50, false); $image->toFile($uploadPath); $data = array('id' => $this->state->get('item_id'), 'avatar' => $hashFilename); $item_type = $this->state->get('item_type'); $this->deleteOldAvatar($data['id'], $item_type); switch ($item_type) { case "people": $model_name = "people"; break; case "companies": $model_name = "company"; break; } $modelClass = "Cobalt\\Model\\" . ucwords($model_name); $model = new $modelClass($this->db); $model->store($data); return JUri::base() . 'src/Cobalt/media/avatars/' . $hashFilename; }
/** * Allows public access to protected method. * * @param mixed $width The input width value to sanitize. * @param mixed $height The input height value for reference. * * @return integer * * @since 1.0 */ public function sanitizeWidth($width, $height) { return parent::sanitizeWidth($width, $height); }
/** * Class constructor. * * @param mixed $source Either a file path for a source image or a GD resource handler for an image. * * @since 11.3 * @throws RuntimeException */ public function __construct($source = null) { // Inject the PSR-3 compatible logger in for forward compatibility $this->setLogger(JLog::createDelegatedLogger()); parent::__construct($source); }
/** * Tests the Image::destory method * * @return void * * @since 1.0 */ public function testDestroy() { // Create an image handle $imageHandle = imagecreatetruecolor(100, 100); // Pass created handle to Image $image = new Image($imageHandle); // Destroying the image should return boolean true $this->assertTrue($image->destroy()); }