/** * @param Attachment $attachment * @returns AttachmentDto */ public static function createFromAttachment(Attachment $attachment) { $dto = new self(); $dto->setFilename($attachment->getFilename()); $dto->setFilePath($attachment->getFile()->getPathname()); return $dto; }
public static function createFromArray($input) { if (false == isset($input['filename']) || false == isset($input['content'])) { throw new \InvalidArgumentException('Not all required fields exists in array.'); } $dto = new self(); $dto->setFilename($input['filename']); $dto->setContent($input['content']); return $dto; }
/** * * @param string $tmp_name path to file * @param string $type mime type * @return self * @throws Exception */ public static function createFromFile($tmp_name, $type) { $mime_type = ElggFile::detectMimeType($tmp_name, $type); if (false == in_array($mime_type, array("image/jpeg", "image/jpg"))) { register_error(elgg_echo("gallery_field:only_jpg")); return null; } $ext = "jpg"; $file = new self(); $thumb_file = new ElggFile(); $random_guid = self::genGUID(); $file->setFilename($random_guid . "." . $ext); $thumb_file->setFilename($random_guid . "_thumb." . $ext); $file->setMimeType($mime_type); $thumb_file->setMimeType($mime_type); $imgsizearray = getimagesize($tmp_name); if ($imgsizearray == false) { register_error("bad file"); return null; } $width = $imgsizearray[0]; $height = $imgsizearray[1]; $file->open("write"); $file->write(self::cropImage($tmp_name, $width, $height, 760, 580)); $file->close(); $file->access_id = 2; $thumb_file->open("write"); $thumb_file->write(self::cropImage($tmp_name, $width, $height, 200, 140)); $thumb_file->close(); $thumb_file->access_id = 2; $thumb_file->save(); $file->thumb_file_guid = $thumb_file->guid; $file->save(); return $file; }