Create a new photo. The user needs to connect their account and authorize the write_photo permission scope.

@param  string $filePath Path of the file to upload
@throws Exception - if filePath does not exist
@return Photo
18654
public static function create($filePath)
    {
        if (!file_exists($filePath)) {
            throw new Exception(["{$filePath} has not been found"]);
        }

        $file = fopen($filePath, 'r');

        $photo = json_decode(
            self::post(
                "photos",
                [
                    'multipart' => [['name' => 'photo', 'contents' => $file]],
                    'headers' => ['Content-Length' => filesize($filePath)]
                ]
            )->getBody(),
            true
        );

        return new self($photo);
    }
1327686
public function addPDF($filepath, $pages = 'all', $orientation = 'vertical') {
        if (file_exists($filepath)) {
            $file = new PdfObject;
            
            if (strtolower($pages) != 'all') {
                $file->pages = $this->_rewritepages($pages);
            }
            
            $file->orientation = $orientation;
            $file->path = $filepath;

            $this->_files[] = $file;
        } else {
            throw new Exception("Could not locate PDF on '$filepath'");
        }

        return $this;
    }
1372470
static public function createFromFile($filename) {
		if (!file_exists($filename))
			throw new Exception('Invalid (filename) parameter! File does not exist!');

		$image_info = getimagesize($filename);
		if ($image_info === false)
			throw new Exception('Invalid (filename) parameter! File is corrupted!');

		switch ($image_info[2]) {
			case IMAGETYPE_JPEG:
			case IMAGETYPE_JPEG2000:
				$image = imagecreatefromjpeg($filename);
				break;
			case IMAGETYPE_PNG:
				$image = imagecreatefrompng($filename);
				break;
			case IMAGETYPE_GIF:
				$image = imagecreatefromgif($filename);
				break;
			case IMAGETYPE_WBMP:
				$image = imagecreatefromwbmp($filename);
				break;
			case IMAGETYPE_BMP:
				$image = imagecreatefrombmp($filename);
				break;
			default:
				throw new Exception('Unknown image format!');
				break;
		}

		return new self($image);
	}
707948
public function saveToFile($filePath)
    {
        $handle = @fopen($filePath, 'w');
        if (false === $handle) {
            throw new Exception('Cannot open file "' . $filePath . '" for writing.');
        }

        $bytesWritten = @fwrite($handle, (string)$this);
        if (false === $bytesWritten) {
            fclose($handle);
            throw new Exception('Cannot write into file "' . $filePath . '".');
        }

        $closed = @fclose($handle);
        if (false === $closed) {
            throw new Exception('Cannot close file handle for "' . $filePath . '".');
        }
    }
312767
protected function createDir($path) {
        if (!is_dir($path)) {
            $this->log->info('Create directory %s', $path);

            $status = mkdir($path, 0775, true);

            if ($status === false) {
                throw new Exception(sprintf(
                    'Unable to create directory %s',
                    $path
                ), ExitApp::RUNTIME_ERROR);
            }
        }

        return $this;
    }
861517
public function addImage ($filePath, $fileName = NULL)
    {

        // Check Image Exists

        if (!file_exists ($filePath))
        {

            // Add error and return false

            new \Sonic\Message ('error', $filePath . ' doesnt exist!');
            return FALSE;

        }

        // Get MIME Type

        if (!$imgData = getimagesize ($filePath))
        {

            // Add error and return false

            new \Sonic\Message ('error', 'Cannot get MIME type!');
            return FALSE;

        }

        // Check Image is allowed

        if (!in_array ($imgData[2], $this->_imagesAllowed))
        {

            // Set variables

            $allowedTypes	= NULL;
            $x				= 0;

            // Generate the allowed images string

            foreach ($this->_imagesAllowed as $type)
            {

                $x++;

                $allowedTypes .= image_type_to_extension ($type);

                if ($x < count ($this->_imagesAllowed))
                {
                    $allowedTypes	.= ', ';
                }

            }

            // Add error and return false

            new \Sonic\Message ('error', 'Invalid image type: image must be ' . $allowedTypes);
            return FALSE;

        }

        // Get Image Data

        if (!$fileData = @file_get_contents ($filePath))
        {

            // Add error and return false

            new \Sonic\Message ('error', 'Cannot get image data!');
            return FALSE;

        }

        // Set images boolean

        $this->_imagesSet	= TRUE;

        // Add Image

        $this->_images[]	= array (
            'name'				=> $fileName,
            'data'				=> $fileData,
            'type'				=> image_type_to_mime_type ($imgData[2]),
            'cid'				=> md5 (uniqid (time ()))
        );

        // Return TRUE

        return TRUE;

    }
618913
public function create() {
        switch($this->info[2]) {
            case IMAGETYPE_JPEG:
                $this->resource = imagecreatefromjpeg($this->file);
                break;
            case IMAGETYPE_GIF:
                $this->resource = imagecreatefromgif($this->file);
                break;
            case IMAGETYPE_PNG:
                $this->resource = imagecreatefrompng($this->file);
                break;
            default:
                throw new Exception('Image type is not supported');
                break;
        }
    }
1152369
public function uploadPhotoFromPath($eventId, $filepath, $albumId = null)
    {
        if (is_null($eventId) || $eventId == '') {
            throw new Exception('The eventId is required to upload a file.');
        }

        if (is_null($filepath) || $filepath == '') {
            throw new Exception('The filepath is required to upload a file.');
        }

        if (!file_exists($filepath)) {
            throw new Exception('The filepath does not point to an accessible file.');
        }

        $params = array(
            'method' => 'sp.photo.upload',
            'event_id' => $eventId,
            'album_id' => $albumId
        );

        $photos = array(
            'photo' => new CURLFile($filepath),
        );

        return $this->_makeApiRequest($params, $photos);
    }
83528
private function createDir($path)
    {
        if (!is_dir($path)) {
            $success = mkdir($path, 0775, true);
            if (!$success) {
                throw new \Exception("Cannot create folder {$path}. Check file system permissions.");
            }
        }
    }
1276972
protected function getFileAsset($filePath)
    {
        $file = new \SplFileInfo($filePath);

        if (!$file->isReadable() || $file->isDir()) {
            return null;
        }

        $filePath = $file->getRealPath();

        return new FileAsset($filePath);
    }
865786
protected function createWritableStream ($filePath)
    {
        if (is_resource($filePath)) {
            $stream = $filePath;
        } elseif (is_string($filePath)) {
            $stream = fopen($filePath, 'wb');
            if (! $stream) {
                throw new InvalidArgumentException("Can't open file `{$filePath}` for writing.");
            }
        } else {
            $stream = tmpfile();
        }

        return $stream;
    }
522095
public function saveToFile($path)
    {
        if (!file_exists($path)) {
            if (!@mkdir($path) && !is_dir($path)) {
                throw new \Exception('Cannot create directory ' . $path);
            };
        }
        $file = tempnam($path, 'mautic_api_');

        if (!is_writable($file)) {
            throw new \Exception($file.' is not writable');
        }

        if (!$handle = fopen($file, 'w')) {
            throw new \Exception('Cannot open file '.$file);
        }

        if (fwrite($handle, $this->body) === false) {
            throw new \Exception('Cannot write into file '.$file);
        }

        fclose($handle);

        return array(
            'file' => $file,
        );
    }
17804
public function create($filePath = null, $mode = 'wb')
    {
        $filePath = isset($filePath) ? $filePath : $this->filePath;
        $dir = dirname($filePath);

        if ( ! is_writable($dir)) {
            if ( ! file_exists($dir)) {
                mkdir($dir, 0777, true);
            }
        }

        if ( ! $fp = @fopen($filePath, $mode)) {
            return false;
        }

        parent::__construct($filePath);

        return $fp;
    }
723235
public function save($path, $fullPath = false)
    {
        if ( !$fullPath ) {

            // var_dump( [ $this->getWidth(), $this->getHeight() ] );
            $path = sprintf( '%s/%s', $this->cropsFolder, basename( $path ) );

        }

        if ($path == $this->originalImagePath) {
            throw new \Exception( 'You are not allowed to overwrite original image. Select another save path.' );
        }

        // var_dump( $path );
        $returnValue = parent::save( $path );

        // Reset
        $this->reset();

        // Return $this-> from parent
        return $returnValue;
    }
375772
public function writeToFile($filePath)
    {
        $out = @fopen($filePath,"w");

        if (!$out){
            throw new AssetException($this->token,
                "The asset '".$this->token."' could not be written to path '$filePath'. Check the path is valid and the directory exists.");
        }

        $stream = $this->getStream();

        if (!$stream){
            throw new AssetException($this->token,
                "The asset '".$this->token."' could not provide a valid stream.'");
        }

        while(!feof($stream)){
            $bytes = fread($stream, 8192);
            fwrite($out, $bytes);
        }

        fclose($stream);
        fclose($out);
    }
421878
protected function createThumbnail($imagePath, $thumbnailPath) {
        if ($this->config['images']['thumbnail']['enabled'] === true) {
            Log::info('generating thumbnail "' . $thumbnailPath . '"');

            // create folder if it does not exist
            if (!file_exists(dirname($thumbnailPath))) {
                mkdir(dirname($thumbnailPath), 0755, true);
            }

            $this->initUploader([
                'upload_dir' => dirname($imagePath) . '/',
            ])->create_thumbnail_image($imagePath);
        }
    }
1072666
public function checkCreateFile($filePath)
    {
        $defaultPermissions = $this->getPermissionUtils()->getDefaultPermissions();

        if (file_exists($filePath)) {
            if (!is_writable($filePath) && !in_array($this->getPermissionUtils()->getCurrentPermission($filePath), array($defaultPermissions['file'], $defaultPermissions['dir']))) {
                return $this->getPermissionUtils()->setDefaultPermissions($filePath, true);
            }
            return true;
        }

        $pathParts = pathinfo($filePath);
        if (!file_exists($pathParts['dirname'])) {
            $dirPermission = $defaultPermissions['dir'];
            $dirPermission = is_string($dirPermission) ? base_convert($dirPermission,8,10) : $dirPermission;

            if (!$this->mkdir($pathParts['dirname'], $dirPermission, true)) {
                throw new Error('Permission denied: unable to create a folder on the server - ' . $pathParts['dirname']);
            }
        }

        if (touch($filePath)) {
            return $this->getPermissionUtils()->setDefaultPermissions($filePath, true);
        }

        return false;
    }