/** * Sets a new profile image by given temp file * * @param CUploadedFile $file */ public function setNew($file) { $this->delete(); ImageConverter::TransformToJpeg($file->tempName, $this->getPath('_org')); ImageConverter::Resize($this->getPath('_org'), $this->getPath('_org'), array('width' => 1134, 'mode' => 'max')); ImageConverter::Resize($this->getPath('_org'), $this->getPath(''), array('width' => $this->width, 'height' => $this->height)); }
/** * Sets a new logo image by given temp file * * @param CUploadedFile $file */ public function setNew(UploadedFile $file) { $this->delete(); move_uploaded_file($file->tempName, $this->getPath()); ImageConverter::Resize($this->getPath(), $this->getPath(), array('height' => $this->height, 'width' => 0, 'mode' => 'max', 'transparent' => $file->getExtension() == 'png' && ImageConverter::checkTransparent($this->getPath()))); }
/** * Create a cfile model and create and connect it with its basefile File model from a given data file, connect it with its parent folder. * TODO: This method has a lot in common with BrowseController/actionUpload, common logic needs to be extracted and reused * * @param Response $response * the response errors will be parsed to. * @param int $parentFolderId * the files pid. * @param string $folderPath * the path of the folder the file data lies in. * @param string $filename * the files name. */ protected function generateModelFromFile(&$response, $parentFolderId, $folderPath, $filename) { $filepath = $folderPath . DIRECTORY_SEPARATOR . $filename; // check if the file already exists in this dir $filesQuery = File::find()->joinWith('baseFile')->readable()->andWhere(['title' => File::sanitizeFilename($filename), 'parent_folder_id' => $parentFolderId]); $file = $filesQuery->one(); // if not, initialize new File if (empty($file)) { $file = new File(); $humhubFile = new \humhub\modules\file\models\File(); } else { // else replace the existing file $humhubFile = $file->baseFile; // logging file replacement $response['infomessages'][] = Yii::t('CfilesModule.base', '%title% was replaced by a newer version.', ['%title%' => $file->title]); $response['log'] = true; } // populate the file $humhubFile->mime_type = FileHelper::getMimeType($filepath); if ($humhubFile->mime_type == 'image/jpeg') { ImageConverter::TransformToJpeg($filepath, $filepath); } $humhubFile->size = filesize($filepath); $humhubFile->file_name = $filename; $humhubFile->newFileContent = stream_get_contents(fopen($filepath, 'r')); if ($humhubFile->save()) { $file->content->container = $this->contentContainer; $file->parent_folder_id = $parentFolderId; if ($file->save()) { $humhubFile->object_model = $file->className(); $humhubFile->object_id = $file->id; $humhubFile->save(); $this->files[] = array_merge($humhubFile->getInfoArray(), ['fileList' => $this->renderFileList()]); } else { $count = 0; $messages = ""; // show multiple occurred errors foreach ($file->errors as $key => $message) { $messages .= ($count++ ? ' | ' : '') . $message[0]; } $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $file->title]) . $messages; $response['log'] = true; } } else { $count = 0; $messages = ""; // show multiple occurred errors foreach ($humhubFile->errors as $key => $message) { $messages .= ($count++ ? ' | ' : '') . $message[0]; } $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $humhubFile->filename]) . $messages; $response['log'] = true; } }
public function getPreviewImageUrl($maxWidth = 1000, $maxHeight = 1000) { $suffix = 'pi_' . $maxWidth . "x" . $maxHeight; $originalFilename = $this->getStoredFilePath(); $previewFilename = $this->getStoredFilePath($suffix); // already generated if (is_file($previewFilename)) { return $this->getUrl($suffix); } // Check file exists & has valid mime type if ($this->getMimeBaseType() != "image" || !is_file($originalFilename)) { return ""; } $imageInfo = @getimagesize($originalFilename); // Check if we got any dimensions - invalid image if (!isset($imageInfo[0]) || !isset($imageInfo[1])) { return ""; } // Check if image type is supported if ($imageInfo[2] != IMAGETYPE_PNG && $imageInfo[2] != IMAGETYPE_JPEG && $imageInfo[2] != IMAGETYPE_GIF) { return ""; } ImageConverter::Resize($originalFilename, $previewFilename, array('mode' => 'max', 'width' => $maxWidth, 'height' => $maxHeight)); return $this->getUrl($suffix); }
public function validateSize($attribute, $params) { if ($this->size > Yii::$app->getModule('file')->settings->get('maxFileSize')) { $this->addError($attribute, Yii::t('FileModule.models_File', 'Maximum file size ({maxFileSize}) has been exceeded!', array("{maxFileSize}" => Yii::$app->formatter->asSize(Yii::$app->getModule('file')->settings->get('maxFileSize'))))); } // check if the file can be processed with php image manipulation tools in case it is an image if (isset($this->uploadedFile) && in_array($this->uploadedFile->type, [image_type_to_mime_type(IMAGETYPE_PNG), image_type_to_mime_type(IMAGETYPE_GIF), image_type_to_mime_type(IMAGETYPE_JPEG)]) && !ImageConverter::allocateMemory($this->uploadedFile->tempName, true)) { $this->addError($attribute, Yii::t('FileModule.models_File', 'Image dimensions are too big to be processed with current server memory limit!')); } }
/** * Sets a new profile image by given temp file * * @param mixed $file CUploadedFile or file path */ public function setNew($file) { if ($file instanceof \yii\web\UploadedFile) { $file = $file->tempName; } $this->delete(); ImageConverter::TransformToJpeg($file, $this->getPath('_org')); ImageConverter::Resize($this->getPath('_org'), $this->getPath('_org'), array('width' => 400, 'mode' => 'max')); ImageConverter::Resize($this->getPath('_org'), $this->getPath(''), array('width' => $this->width, 'height' => $this->height)); }
/** * Crop an image file to a square thumbnail. * The thumbnail will be saved with the suffix "<width>_thumb_square" * @param File $basefile the file to crop. * @param number $maxDimension limit maximum with/height. * @return string the thumbnail's url or null if an error occured. */ public static function getSquareThumbnailUrlFromFile($basefile = null, $maxDimension = 1000) { if ($basefile === null) { return; } $suffix = $maxDimension . '_thumb_square'; $originalFilename = $basefile->getStoredFilePath(); $previewFilename = $basefile->getStoredFilePath($suffix); // already generated if (is_file($previewFilename)) { return $basefile->getUrl($suffix); } // Check file exists & has valid mime type if ($basefile->getMimeBaseType() != "image" || !is_file($originalFilename)) { return ""; } $imageInfo = @getimagesize($originalFilename); // check valid image dimesions if (!isset($imageInfo[0]) || !isset($imageInfo[1])) { return ""; } // Check if image type is supported if ($imageInfo[2] != IMAGETYPE_PNG && $imageInfo[2] != IMAGETYPE_JPEG && $imageInfo[2] != IMAGETYPE_GIF) { return ""; } $dim = min($imageInfo[0], $imageInfo[1], $maxDimension); ImageConverter::Resize($originalFilename, $previewFilename, array('mode' => 'force', 'width' => $dim, 'height' => $dim)); return $basefile->getUrl($suffix); }