<?php

namespace Illuminate\Http\Testing;

class FileFactory
{
    /**
     * Create a new fake file.
     *
     * @param  string  $name
     * @param  int  $mebibytes
     * @return \Illuminate\Http\Testing\File
     */
    public function create($name, $mebibytes = 0)
    {
        return tap(new File($name, tmpfile()), function ($file) use ($mebibytes) {
            $file->sizeToReport = $mebibytes * 1024;
        });
    }

    /**
     * Create a new fake image.
     *
     * @param  string  $name
     * @param  int  $width
     * @param  int  $height
     * @return \Illuminate\Http\Testing\File
     */
    public function image($name, $width = 10, $height = 10)
    {
        return new File($name, $this->generateImage($width, $height));
    }

    /**
     * Generate a dummy image of the given width and height.
     *
     * @param  int  $width
     * @param  int  $height
     * @return resource
     */
    protected function generateImage($width, $height)
    {
        return tap(tmpfile(), function ($temp) use ($width, $height) {
            ob_start();

            imagepng(imagecreatetruecolor($width, $height));

            fwrite($temp, ob_get_clean());
        });
    }
}
