public static function create(User $user, $uploaded_filename, $name) { // Create a new row in the database for the image $query = Database::connection()->prepare('INSERT INTO attachment (size, name, created_by, created_at) VALUES (?, ?, ?, ?)'); $query->bindValue(1, filesize($uploaded_filename), PDO::PARAM_INT); $query->bindValue(2, $name, PDO::PARAM_INT); $query->bindValue(3, $user->getUserId(), PDO::PARAM_INT); $query->bindValue(4, time(), PDO::PARAM_INT); if (!$query->execute()) { throw new Exception('Unable to create attachment.'); } // Get the image that we just created $attachment = Attachment::fromId(Database::connection()->lastInsertId()); if (is_uploaded_file($uploaded_filename)) { if (!move_uploaded_file($uploaded_filename, self::getStoragePath($attachment->getAttachmentId()))) { $attachment->delete(); throw new Exception('Unable to move attachment into place.'); } } else { if (!rename($uploaded_filename, self::getStoragePath($attachment->getAttachmentId()))) { $attachment->delete(); throw new Exception('Unable to move attachment into place.'); } } //$imagick = new Imagick(); //$imagick->readimage(self::getStoragePath($image->getId())); //$imagick->setimagetype('png'); //$imagick->writeimage(); return $attachment; }
function delete_attachment() { Auth::checkLoggedIn(); $entry = Entry::fromId(Input::get('entryid')); $attachment = Attachment::fromId(Input::get('attachmentid')); // Make sure this user can edit the entry if (!$entry->canEdit(Auth::getUser())) { throw new Exception('You cannot edit this entry.'); } // Make sure the attachment belongs to the entry $attachments = $entry->getAttachments(); $found = false; foreach ($attachments as $curAttachment) { if ($curAttachment->getAttachmentId() == $attachment->getAttachmentId()) { $found = true; break; } } if (!$found) { throw new Exception('The requested attachment does not belong to the entry given.'); } // Now delete the attachment $attachment->delete(); // We have to manually sync since the attachment isn't part of the entry technically $entry->changed(); }
/** * Sets the avatar for this user given the attachment it is. * @param Attachment $attachment */ public function setAvatar(Attachment $attachment) { // Make sure the attachment is an image if ($attachment->getAttachmentType() != Attachment::ATTACHMENT_TYPE_IMAGE) { throw new Exception('Avatars must be images.'); } // Turn the image into a png $attachment->convertToPNG(); // Turn the new attachment into a thubmnail $attachment->convertIntoThubmnail(); // See if we need to delete an old one if ($this->hasAvatar()) { $oldAvatar = Attachment::fromId($this->getAvatarAttachmentId()); $oldAvatar->delete(); } // Update the database $query = Database::connection()->prepare('UPDATE user SET avatar_attachmentid = ? WHERE userid = ?'); $query->bindValue(1, $attachment->getAttachmentId(), PDO::PARAM_INT); $query->bindValue(2, $this->getUserId(), PDO::PARAM_INT); $query->execute(); // Update the local value $this->row['avatar_attachmentid'] = $attachment->getAttachmentId(); }