diff --git a/app/Customer.php b/app/Customer.php index 4ad1e666..322e4b90 100644 --- a/app/Customer.php +++ b/app/Customer.php @@ -3,6 +3,8 @@ namespace App; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Storage; use Watson\Rememberable\Rememberable; class Customer extends Model @@ -11,6 +13,10 @@ class Customer extends Model // This is obligatory. public $rememberCacheDriver = 'array'; + const PHOTO_DIRECTORY = 'customers'; + const PHOTO_SIZE = 50; // px + const PHOTO_QUALITY = 77; + /** * Genders. */ @@ -433,7 +439,7 @@ class Customer extends Model * * @var [type] */ - protected $fillable = ['first_name', 'last_name', 'company', 'job_title', 'address', 'city', 'state', 'zip', 'country']; + protected $fillable = ['first_name', 'last_name', 'company', 'job_title', 'address', 'city', 'state', 'zip', 'country', 'photo_url']; /** * Fields stored as JSON. @@ -852,11 +858,6 @@ class Customer extends Model return $date->format($format); } - public function getPhotoUrl() - { - return asset('/img/default-avatar.png'); - } - /** * Get full representation of customer. */ @@ -952,4 +953,57 @@ class Customer extends Model return ''; } + + public function getPhotoUrl($default_if_empty = true) + { + if (!empty($this->photo_url) || !$default_if_empty) { + if (!empty($this->photo_url)) { + return Storage::url(self::PHOTO_DIRECTORY.DIRECTORY_SEPARATOR.$this->photo_url); + } else { + return ''; + } + } else { + return asset('/img/default-avatar.png'); + } + } + + /** + * Resize and save photo. + */ + public function savePhoto($real_path, $mime_type) + { + $resized_image = \App\Misc\Helper::resizeImage($real_path, $mime_type, self::PHOTO_SIZE, self::PHOTO_SIZE); + + if (!$resized_image) { + return false; + } + + $file_name = md5(Hash::make($this->id)).'.jpg'; + $dest_path = Storage::path(self::PHOTO_DIRECTORY.DIRECTORY_SEPARATOR.$file_name); + + $dest_dir = pathinfo($dest_path, PATHINFO_DIRNAME); + if (!file_exists($dest_dir)) { + \File::makeDirectory($dest_dir, 0755); + } + + // Remove current photo + if ($this->photo_url) { + Storage::delete(self::PHOTO_DIRECTORY.DIRECTORY_SEPARATOR.$this->photo_url); + } + + imagejpeg($resized_image, $dest_path, self::PHOTO_QUALITY); + + return $file_name; + } + + /** + * Remove user photo. + */ + public function removePhoto() + { + if ($this->photo_url) { + Storage::delete(self::PHOTO_DIRECTORY.DIRECTORY_SEPARATOR.$this->photo_url); + } + $this->photo_url = ''; + } } diff --git a/app/Misc/Helper.php b/app/Misc/Helper.php index efccd60c..f4bf77a6 100644 --- a/app/Misc/Helper.php +++ b/app/Misc/Helper.php @@ -582,8 +582,14 @@ class Helper if (preg_match('/png/i', $mime_type)) { $src = imagecreatefrompng($file); + + $kek = imagecolorallocate($src, 255, 255, 255); + imagefill($src, 0, 0, $kek); } elseif (preg_match('/gif/i', $mime_type)) { $src = imagecreatefromgif($file); + + $kek = imagecolorallocate($src, 255, 255, 255); + imagefill($src, 0, 0, $kek); } elseif (preg_match('/bmp/i', $mime_type)) { $src = imagecreatefrombmp($file); } else { diff --git a/app/Observers/EmailObserver.php b/app/Observers/EmailObserver.php new file mode 100644 index 00000000..62414fb3 --- /dev/null +++ b/app/Observers/EmailObserver.php @@ -0,0 +1,18 @@ +