Skip to content

Commit

Permalink
Merged develop into master
Browse files Browse the repository at this point in the history
  • Loading branch information
morrelinko committed Jun 22, 2014
2 parents 1319184 + be1fbe3 commit 4861cc1
Show file tree
Hide file tree
Showing 33 changed files with 144 additions and 76 deletions.
4 changes: 3 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
* text eol=lf
* text eol=lf
*.png binary
*.jpg binary
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ php:
- 5.3
- 5.4
- 5.5
- 5.6
- hhvm

before_script:
- composer self-update
- composer install --dev --prefer-source --no-interaction
- composer self-update --profile
- composer install --dev

script: phpunit --coverage-text
script:
- phpunit --coverage-text

matrix:
allow_failures:
- php: hhvm
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
SimplePhoto
---------------------
Photo uploading and management made easy.

Handling photos in your web application has never been so *simple*.

[![Build Status](https://travis-ci.org/morrelinko/simple-photo.png?branch=master)](https://travis-ci.org/morrelinko/simple-photo)

Expand Down Expand Up @@ -35,7 +36,7 @@ With support for accepting uploads from different sources.
$photoId = $simplePhoto->upload(new YourUploadSource($imageData));
```

The two upload methods shown above actually are just like aliases/shortcuts for doing this
The two upload methods shown above actually are aliases/shortcuts for doing this

```php
$photoId = $simplePhoto->upload(new PhpFileUploadSource($_FILES["image"]));
Expand Down Expand Up @@ -186,23 +187,23 @@ var_dump($users);

## Supported Photo Sources

* [FilePath Source](https://github.com/morrelinko/simple-photo/blob/develop/src/SimplePhoto/Source/FilePathSource.php)
* [PhpFileUpload Source](https://github.com/morrelinko/simple-photo/blob/develop/src/SimplePhoto/Source/PhpFileUploadSource.php)
* [Url Source](https://github.com/morrelinko/simple-photo/blob/develop/src/SimplePhoto/Source/UrlSource.php)
* [SymfonyFileUploadSource](https://github.com/morrelinko/simple-photo/blob/develop/src/SimplePhoto/Source/SymfonyFileUploadSource.php)
* [FilePath Source](https://github.com/morrelinko/simple-photo/blob/develop/src/Source/FilePathSource.php)
* [PhpFileUpload Source](https://github.com/morrelinko/simple-photo/blob/develop/src/Source/PhpFileUploadSource.php)
* [Url Source](https://github.com/morrelinko/simple-photo/blob/develop/src/Source/UrlSource.php)
* [SymfonyFileUploadSource](https://github.com/morrelinko/simple-photo/blob/develop/src/Source/SymfonyFileUploadSource.php)

## Supported Data Stores

* [MySql Data Store](https://github.com/morrelinko/simple-photo/blob/develop/src/SimplePhoto/DataStore/MySqlDataStore.php)
* [Sqlite Data Store](https://github.com/morrelinko/simple-photo/blob/develop/src/SimplePhoto/DataStore/SqliteDataStore.php)
* [Memory Data Store](https://github.com/morrelinko/simple-photo/blob/develop/src/SimplePhoto/DataStore/MemoryDataStore.php)
* [MySql Data Store](https://github.com/morrelinko/simple-photo/blob/develop/src/DataStore/MySqlDataStore.php)
* [Sqlite Data Store](https://github.com/morrelinko/simple-photo/blob/develop/src/DataStore/SqliteDataStore.php)
* [Memory Data Store](https://github.com/morrelinko/simple-photo/blob/develop/src/DataStore/MemoryDataStore.php)

## Supported Storage

* [Local Storage](https://github.com/morrelinko/simple-photo/blob/develop/src/SimplePhoto/Storage/LocalStorage.php)
* [Remote Host Storage](https://github.com/morrelinko/simple-photo/blob/develop/src/SimplePhoto/Storage/RemoteHostStorage.php)
* [Memory Storage](https://github.com/morrelinko/simple-photo/blob/develop/src/SimplePhoto/Storage/MemoryStorage.php)
* [AwsS3 Storage](https://github.com/morrelinko/simple-photo/blob/develop/src/SimplePhoto/Storage/AwsS3Storage.php)
* [Local Storage](https://github.com/morrelinko/simple-photo/blob/develop/src/Storage/LocalStorage.php)
* [Remote Host Storage](https://github.com/morrelinko/simple-photo/blob/develop/src/Storage/RemoteHostStorage.php)
* [Memory Storage](https://github.com/morrelinko/simple-photo/blob/develop/src/Storage/MemoryStorage.php)
* [AwsS3 Storage](https://github.com/morrelinko/simple-photo/blob/develop/src/Storage/AwsS3Storage.php)

## TODO

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "morrelinko/simple-photo",
"description": "Photo uploading and management made easy.",
"keywords": ["photo", "simple-photo", "upload", "storage", "image", "transform"],
"license": "MIT",
"authors": [
{
Expand All @@ -22,6 +23,7 @@
},
"require-dev": {
"aws/aws-sdk-php": "~2",
"mockery/mockery": "dev-master@dev"
"mockery/mockery": "dev-master@dev",
"phpunit/phpunit": "~4.0"
}
}
6 changes: 3 additions & 3 deletions samples/collection-push.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@
'cover_photo_id' => 2
);

$simplePhoto->push($data, array('photo_id', 'cover_photo_id'), function (&$item, PhotoResult $photo, $index, $name) {
if ($index == 'photo_id') {
$simplePhoto->push($data, array('photo_id', 'cover_photo_id'), function (&$item, PhotoResult $photo, $key, $find) {
if ($find == 'photo_id') {
// Adds an element to the array 'photo_url' => 'http://xxxxxxxxxxxx'
$item['photo_url'] = $photo->url();
} else if ($index == 'cover_photo_id') {
} else if ($find == 'cover_photo_id') {
// Adds an element to the array 'cover_photo_url' => 'http://xxxxxxxxxxxx'
$item['cover_photo_url'] = $photo->url();
}
Expand Down
2 changes: 1 addition & 1 deletion samples/manipulations.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// Get a photo, resize + rotate it...
$resizePhoto = $simplePhoto->get(1, array(
'transform' => array(
'size' => array(100, 100),
'resize' => array(100, 100),
'rotate' => array(180)
)
));
Expand Down
45 changes: 37 additions & 8 deletions src/SimplePhoto.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ class SimplePhoto
*
* @param StorageManager $storageManager
* @param DataStoreInterface $dataStore
* @param array $options
*/
public function __construct(
StorageManager $storageManager = null,
DataStoreInterface $dataStore = null
DataStoreInterface $dataStore = null,
array $options = array()
) {
if ($storageManager != null) {
$this->setStorageManager($storageManager);
Expand All @@ -66,6 +68,8 @@ public function __construct(
if ($dataStore != null) {
$this->setDataStore($dataStore);
}

$this->setOptions($options);
}

/**
Expand Down Expand Up @@ -120,6 +124,25 @@ public function setTransformer(TransformerInterface $transformer)
$this->transformer = $transformer;
}

/**
* @param $options
*/
public function setOptions($options)
{
$this->options = array_merge(array(
'tmp_dir' => sys_get_temp_dir()
), $options);
}

/**
* @param $option
* @return mixed
*/
public function getOption($option)
{
return $this->options[$option];
}

/**
* Upload file from native file upload shortcut
* <p>
Expand Down Expand Up @@ -180,7 +203,7 @@ public function uploadFromUrl($url, array $options = array())
*/
public function upload(PhotoSourceInterface $photoSource, array $options = array())
{
$photoSource->process();
$photoSource->process($this->options);
if ($photoSource->isValid() == false) {
// No need to go further if source is invalid
return false;
Expand All @@ -201,7 +224,10 @@ public function upload(PhotoSourceInterface $photoSource, array $options = array
// as the original image
list($uploadPath, $fileSize) = $this->transformPhoto(
$storage,
FileUtils::createTempFile($photoSource->getFile()),
FileUtils::createTempFile(
$photoSource->getFile(),
$this->options['tmp_dir']
),
$saveName,
$fileMime,
$options['transform']
Expand Down Expand Up @@ -378,7 +404,7 @@ public function push(&$haystack, array $keys = array(), \Closure $callback = nul
* </pre>
* @return bool|PhotoResult
*/
public function build(array $photo, array $options = array())
public function build($photo, array $options = array())
{
$options = array_merge(array(
'transform' => array(),
Expand Down Expand Up @@ -426,7 +452,10 @@ public function build(array $photo, array $options = array())
// (ie if file does not exists)
list($modifiedFileName, $info['file_size']) = $this->transformPhoto(
$storage,
$storage->getPhotoResource($photoResult->originalFilePath()),
$storage->getPhotoResource(
$photoResult->originalFilePath(),
tempnam($this->options['tmp_dir'], 'sp')
),
$modifiedFileName,
$photo['file_mime'],
$options['transform']
Expand All @@ -452,7 +481,7 @@ public function build(array $photo, array $options = array())
* @param array $transform
* @return string|bool Modified file if successful or false otherwise
*/
private function transformPhoto(
protected function transformPhoto(
StorageInterface $storage,
$tmpFile,
$modifiedFile,
Expand Down Expand Up @@ -482,7 +511,7 @@ private function transformPhoto(
* @param string $file
* @return string
*/
private function generateOriginalSaveName($file)
protected function generateOriginalSaveName($file)
{
$fileName = uniqid(time() . substr(str_shuffle('abcdefABCDEF012345'), 0, 8));
$extension = pathinfo($file, PATHINFO_EXTENSION);
Expand All @@ -496,7 +525,7 @@ private function generateOriginalSaveName($file)
* @param array $transform
* @return string
*/
private function generateModifiedSaveName($oldName, $transform)
protected function generateModifiedSaveName($oldName, $transform)
{
$name = $this->getTransformer()->generateName($transform);
// Extract information from original file
Expand Down
2 changes: 1 addition & 1 deletion src/Source/FilePathSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct($file = null)
/**
* {@inheritDoc}
*/
public function process()
public function process(array $options = array())
{
return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Source/PhotoSourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface PhotoSourceInterface
*
* @return PhotoSourceInterface
*/
public function process();
public function process(array $options = array());

/**
* Name of file
Expand Down
2 changes: 1 addition & 1 deletion src/Source/PhpFileUploadSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct($fileData)
/**
* {@inheritDoc}
*/
public function process()
public function process(array $options = array())
{
return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Source/SymfonyFileUploadSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(UploadedFile $file)
/**
* {@inheritDoc}
*/
public function process()
public function process(array $options = array())
{
return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Source/UrlSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public function __construct($url)
/**
* {@inheritDoc}
*/
public function process()
public function process(array $options = array())
{
$this->name = basename($this->url);
$this->path = tempnam(sys_get_temp_dir(), 'spu');
$this->path = tempnam($options['tmp_dir'], 'sp');
$fp = fopen($this->path, 'w+');

$ch = curl_init($this->url);
Expand Down
7 changes: 3 additions & 4 deletions src/Storage/AwsS3Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,13 @@ public function getPhotoUrl($file)
/**
* {@inheritDoc}
*/
public function getPhotoResource($file)
public function getPhotoResource($file, $tmpFile)
{
$tmpName = tempnam(sys_get_temp_dir(), 'temp');
$this->client->getObject($this->getOptions($file), array(
'SaveAs' => $tmpName
'SaveAs' => $tmpFile
));

return $tmpName;
return $tmpFile;
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/Storage/LocalStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,11 @@ public function getPhotoUrl($file)
/**
* {@inheritDoc}
*/
public function getPhotoResource($file)
public function getPhotoResource($file, $tmpFile)
{
$tmpName = tempnam(sys_get_temp_dir(), 'temp');
copy($this->normalizePath($file, true), $tmpName);
copy($this->normalizePath($file, true), $tmpFile);

return $tmpName;
return $tmpFile;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Storage/MemoryStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,20 @@ public function getPhotoUrl($file)
/**
* {@inheritDoc}
*/
public function getPhotoResource($file)
public function getPhotoResource($file, $tmpFile)
{
$tmpName = tempnam(sys_get_temp_dir(), 'temp');
if ($this->exists($file)) {
file_put_contents($tmpName, $this->storage[$file]['content']);
file_put_contents($tmpFile, $this->storage[$file]['content']);
}

return $tmpName;
return $tmpFile;
}

/**
* {@inheritDoc}
*/
public function exists($file)
public
function exists($file)
{
return isset($this->storage[$file]);
}
Expand Down
7 changes: 3 additions & 4 deletions src/Storage/RemoteHostStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,16 @@ public function getPhotoUrl($file)
/**
* {@inheritDoc}
*/
public function getPhotoResource($file)
public function getPhotoResource($file, $tmpFile)
{
$tmpName = tempnam(sys_get_temp_dir(), 'temp');
ftp_get(
$this->connection(),
$tmpName,
$tmpFile,
$this->normalizePath($file, true),
FTP_ASCII
);

return $tmpName;
return $tmpFile;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ public function getPhotoUrl($file);

/**
* @param string $file
* @param string $tmpFile temp file photo is saved locally during manipulation
* @return mixed
*/
public function getPhotoResource($file);
public function getPhotoResource($file, $tmpFile);

/**
* Checks if photo exists
Expand Down
5 changes: 3 additions & 2 deletions src/Toolbox/FileUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ public static function isAbsolute($path)

/**
* @param string $file
* @param string|null $dir
* @return string
*/
public static function createTempFile($file)
public static function createTempFile($file, $dir = null)
{
$tmpFile = tempnam(sys_get_temp_dir(), 'up');
$tmpFile = tempnam($dir ? $dir : sys_get_temp_dir(), 'sp');
copy($file, $tmpFile);

return $tmpFile;
Expand Down

0 comments on commit 4861cc1

Please sign in to comment.