File width and height is now properly set for File

This commit is contained in:
Mikael Nordfeldth 2014-04-22 12:09:24 +02:00
parent 5fbb668855
commit e526909bd8
4 changed files with 98 additions and 62 deletions

View File

@ -498,36 +498,13 @@ class File extends Managed_DataObject
*/
protected function generateThumbnail($width, $height, $crop)
{
$imgPath = null;
$media = common_get_mime_media($this->mimetype);
$width = intval($width);
if ($height === null) {
$height = $width;
$crop = true;
}
if (Event::handle('CreateFileImageThumbnailSource', array($this, &$imgPath, $media))) {
switch ($media) {
case 'image':
$imgPath = $this->getPath();
break;
default:
throw new UnsupportedMediaException(_('Unsupported media format.'), $this->getPath());
}
}
if (!file_exists($imgPath)) {
throw new ServerException(sprintf('Thumbnail source is not stored locally: %s', $imgPath));
}
try {
$image = new ImageFile($this->id, $imgPath);
} catch (UnsupportedMediaException $e) {
// Avoid deleting the original
if ($image->getPath() != $this->getPath()) {
$image->unlink();
}
throw $e;
}
$image = ImageFile::fromFileObject($this);
list($width, $height, $x, $y, $w2, $h2) =
$image->scaleToFit($width, $height, $crop);

View File

@ -76,6 +76,36 @@ class ImageFile
$this->height = ($info) ? $info[1]:$height;
}
public static function fromFileObject(File $file)
{
$imgPath = null;
$media = common_get_mime_media($file->mimetype);
if (Event::handle('CreateFileImageThumbnailSource', array($file, &$imgPath, $media))) {
switch ($media) {
case 'image':
$imgPath = $file->getPath();
break;
default:
throw new UnsupportedMediaException(_('Unsupported media format.'), $file->getPath());
}
}
if (!file_exists($imgPath)) {
throw new ServerException(sprintf('Image not available locally: %s', $imgPath));
}
try {
$image = new ImageFile($file->id, $imgPath);
} catch (UnsupportedMediaException $e) {
// Avoid deleting the original
if ($imgPath != $file->getPath()) {
unlink($imgPath);
}
throw $e;
}
return $image;
}
public function getPath()
{
if (!file_exists($this->filepath)) {

View File

@ -81,7 +81,8 @@ class MediaFile
@unlink($filepath);
}
function storeFile() {
protected function storeFile()
{
$file = new File;
@ -94,12 +95,33 @@ class MediaFile
$file_id = $file->insert();
if (!$file_id) {
if ($file_id===false) {
common_log_db_error($file, "INSERT", __FILE__);
// TRANS: Client exception thrown when a database error was thrown during a file upload operation.
throw new ClientException(_('There was a database error while saving your file. Please try again.'));
}
// Set file geometrical properties if available
try {
$image = ImageFile::fromFileObject($file);
$orig = clone($file);
$file->width = $image->width;
$file->height = $image->height;
$file->update($orig);
// We have to cleanup after ImageFile, since it
// may have generated a temporary file from a
// video support plugin or something.
// FIXME: Do this more automagically.
if ($image->getPath() != $file->getPath()) {
$image->unlink();
}
} catch (ServerException $e) {
// We just couldn't make out an image from the file. This
// does not have to be UnsupportedMediaException, as we can
// also get ServerException from files not existing etc.
}
return $file;
}

View File

@ -428,16 +428,23 @@ function fixupFileGeometry()
if ($file->find()) {
while ($file->fetch()) {
// Add support for video sizes too
// Set file geometrical properties if available
try {
$image = new ImageFile($file->id, $file->getPath());
} catch (UnsupportedMediaException $e) {
$image = ImageFile::fromFileObject($file);
} catch (ServerException $e) {
// We couldn't make out an image from the file.
continue;
}
$orig = clone($file);
$file->width = $image->width;
$file->height = $image->height;
$file->update($orig);
// FIXME: Do this more automagically inside ImageFile or so.
if ($image->getPath() != $file->getPath()) {
$image->unlink();
}
unset($image);
}
}